MCPcopy
hub / github.com/labstack/echo / TestQueryParam

Function TestQueryParam

binder_generic_test.go:100–139  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

98}
99
100func TestQueryParam(t *testing.T) {
101 var testCases = []struct {
102 name string
103 givenURL string
104 expect bool
105 expectErr string
106 }{
107 {
108 name: "ok",
109 givenURL: "/?key=true",
110 expect: true,
111 },
112 {
113 name: "nok, non existent key",
114 givenURL: "/?different=true",
115 expect: false,
116 expectErr: ErrNonExistentKey.Error(),
117 },
118 {
119 name: "nok, invalid value",
120 givenURL: "/?key=invalidbool",
121 expect: false,
122 expectErr: `code=400, message=query param, err=failed to parse value, err: strconv.ParseBool: parsing "invalidbool": invalid syntax, field=key`,
123 },
124 }
125 for _, tc := range testCases {
126 t.Run(tc.name, func(t *testing.T) {
127 req := httptest.NewRequest(http.MethodPost, tc.givenURL, nil)
128 c := NewContext(req, nil)
129
130 v, err := QueryParam[bool](c, "key")
131 if tc.expectErr != "" {
132 assert.EqualError(t, err, tc.expectErr)
133 } else {
134 assert.NoError(t, err)
135 }
136 assert.Equal(t, tc.expect, v)
137 })
138 }
139}
140
141func TestQueryParam_UnsupportedType(t *testing.T) {
142 req := httptest.NewRequest(http.MethodPost, "/?key=bool", nil)

Callers

nothing calls this directly

Calls 2

NewContextFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…