(t *testing.T)
| 930 | } |
| 931 | |
| 932 | func TestDefaultBinder_BindBody(t *testing.T) { |
| 933 | // tests to check binding behaviour when multiple sources (path params, query params and request body) are in use |
| 934 | // generally when binding from request body - URL and path params are ignored - unless form is being bound. |
| 935 | // these tests are to document this behaviour and detect further possible regressions when bind implementation is changed |
| 936 | |
| 937 | type Node struct { |
| 938 | Node string `json:"node" xml:"node" form:"node" query:"node" param:"node"` |
| 939 | ID int `json:"id" xml:"id" form:"id" query:"id"` |
| 940 | } |
| 941 | type Nodes struct { |
| 942 | Nodes []Node `xml:"node" form:"node"` |
| 943 | } |
| 944 | |
| 945 | var testCases = []struct { |
| 946 | givenContent io.Reader |
| 947 | whenBindTarget any |
| 948 | expect any |
| 949 | name string |
| 950 | givenURL string |
| 951 | givenMethod string |
| 952 | givenContentType string |
| 953 | expectError string |
| 954 | whenNoPathValues bool |
| 955 | whenChunkedBody bool |
| 956 | }{ |
| 957 | { |
| 958 | name: "ok, JSON POST bind to struct with: path + query + empty field in body", |
| 959 | givenURL: "/api/real_node/endpoint?node=xxx", |
| 960 | givenMethod: http.MethodPost, |
| 961 | givenContentType: MIMEApplicationJSON, |
| 962 | givenContent: strings.NewReader(`{"id": 1}`), |
| 963 | expect: &Node{ID: 1, Node: ""}, // path params or query params should not interfere with body |
| 964 | }, |
| 965 | { |
| 966 | name: "ok, JSON POST bind to struct with: path + query + body", |
| 967 | givenURL: "/api/real_node/endpoint?node=xxx", |
| 968 | givenMethod: http.MethodPost, |
| 969 | givenContentType: MIMEApplicationJSON, |
| 970 | givenContent: strings.NewReader(`{"id": 1, "node": "zzz"}`), |
| 971 | expect: &Node{ID: 1, Node: "zzz"}, // field value from content has higher priority |
| 972 | }, |
| 973 | { |
| 974 | name: "ok, JSON POST body bind json array to slice (has matching path/query params)", |
| 975 | givenURL: "/api/real_node/endpoint?node=xxx", |
| 976 | givenMethod: http.MethodPost, |
| 977 | givenContentType: MIMEApplicationJSON, |
| 978 | givenContent: strings.NewReader(`[{"id": 1}]`), |
| 979 | whenNoPathValues: true, |
| 980 | whenBindTarget: &[]Node{}, |
| 981 | expect: &[]Node{{ID: 1, Node: ""}}, |
| 982 | expectError: "", |
| 983 | }, |
| 984 | { // rare case as GET is not usually used to send request body |
| 985 | name: "ok, JSON GET bind to struct with: path + query + empty field in body", |
| 986 | givenURL: "/api/real_node/endpoint?node=xxx", |
| 987 | givenMethod: http.MethodGet, |
| 988 | givenContentType: MIMEApplicationJSON, |
| 989 | givenContent: strings.NewReader(`{"id": 1}`), |
nothing calls this directly
no test coverage detected
searching dependent graphs…