MCPcopy Index your code
hub / github.com/labstack/echo / TestDefaultBinder_BindToStructFromMixedSources

Function TestDefaultBinder_BindToStructFromMixedSources

bind_test.go:761–930  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

759}
760
761func TestDefaultBinder_BindToStructFromMixedSources(t *testing.T) {
762 // tests to check binding behaviour when multiple sources (path params, query params and request body) are in use
763 // binding is done in steps and one source could overwrite previous source bound data
764 // these tests are to document this behaviour and detect further possible regressions when bind implementation is changed
765
766 type Opts struct {
767 Node string `json:"node" form:"node" query:"node" param:"node"`
768 Lang string
769 ID int `json:"id" form:"id" query:"id"`
770 }
771
772 var testCases = []struct {
773 givenContent io.Reader
774 whenBindTarget any
775 expect any
776 name string
777 givenURL string
778 givenMethod string
779 expectError string
780 whenNoPathValues bool
781 }{
782 {
783 name: "ok, POST bind to struct with: path param + query param + body",
784 givenMethod: http.MethodPost,
785 givenURL: "/api/real_node/endpoint?node=xxx",
786 givenContent: strings.NewReader(`{"id": 1}`),
787 expect: &Opts{ID: 1, Node: "node_from_path"}, // query params are not used, node is filled from path
788 },
789 {
790 name: "ok, PUT bind to struct with: path param + query param + body",
791 givenMethod: http.MethodPut,
792 givenURL: "/api/real_node/endpoint?node=xxx",
793 givenContent: strings.NewReader(`{"id": 1}`),
794 expect: &Opts{ID: 1, Node: "node_from_path"}, // query params are not used
795 },
796 {
797 name: "ok, GET bind to struct with: path param + query param + body",
798 givenMethod: http.MethodGet,
799 givenURL: "/api/real_node/endpoint?node=xxx",
800 givenContent: strings.NewReader(`{"id": 1}`),
801 expect: &Opts{ID: 1, Node: "xxx"}, // query overwrites previous path value
802 },
803 {
804 name: "ok, GET bind to struct with: path param + query param + body",
805 givenMethod: http.MethodGet,
806 givenURL: "/api/real_node/endpoint?node=xxx",
807 givenContent: strings.NewReader(`{"id": 1, "node": "zzz"}`),
808 expect: &Opts{ID: 1, Node: "zzz"}, // body is bound last and overwrites previous (path,query) values
809 },
810 {
811 name: "ok, DELETE bind to struct with: path param + query param + body",
812 givenMethod: http.MethodDelete,
813 givenURL: "/api/real_node/endpoint?node=xxx",
814 givenContent: strings.NewReader(`{"id": 1, "node": "zzz"}`),
815 expect: &Opts{ID: 1, Node: "zzz"}, // for DELETE body is bound after query params
816 },
817 {
818 name: "ok, POST bind to struct with: path param + body",

Callers

nothing calls this directly

Calls 5

SetPathValuesMethod · 0.95
NewFunction · 0.85
SetMethod · 0.80
NewContextMethod · 0.80
BindMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…