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

Function TestValuesFromQuery

middleware/extractor_test.go:259–322  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

257}
258
259func TestValuesFromQuery(t *testing.T) {
260 var testCases = []struct {
261 name string
262 givenQueryPart string
263 whenName string
264 whenLimit uint
265 expectValues []string
266 expectError string
267 }{
268 {
269 name: "ok, single value",
270 givenQueryPart: "?id=123&name=test",
271 whenName: "id",
272 expectValues: []string{"123"},
273 },
274 {
275 name: "ok, multiple value",
276 givenQueryPart: "?id=123&id=456&name=test",
277 whenName: "id",
278 whenLimit: 2,
279 expectValues: []string{"123", "456"},
280 },
281 {
282 name: "nok, missing value",
283 givenQueryPart: "?id=123&name=test",
284 whenName: "nope",
285 expectError: errQueryExtractorValueMissing.Error(),
286 },
287 {
288 name: "ok, cut values over extractorLimit",
289 givenQueryPart: "?name=test" +
290 "&id=1&id=2&id=3&id=4&id=5&id=6&id=7&id=8&id=9&id=10" +
291 "&id=11&id=12&id=13&id=14&id=15&id=16&id=17&id=18&id=19&id=20" +
292 "&id=21&id=22&id=23&id=24&id=25",
293 whenName: "id",
294 whenLimit: extractorLimit,
295 expectValues: []string{
296 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
297 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
298 },
299 },
300 }
301
302 for _, tc := range testCases {
303 t.Run(tc.name, func(t *testing.T) {
304 e := echo.New()
305
306 req := httptest.NewRequest(http.MethodGet, "/"+tc.givenQueryPart, nil)
307 rec := httptest.NewRecorder()
308 c := e.NewContext(req, rec)
309
310 extractor := valuesFromQuery(tc.whenName, tc.whenLimit)
311
312 values, source, err := extractor(c)
313 assert.Equal(t, tc.expectValues, values)
314 assert.Equal(t, ExtractorSourceQuery, source)
315 if tc.expectError != "" {
316 assert.EqualError(t, err, tc.expectError)

Callers

nothing calls this directly

Calls 3

valuesFromQueryFunction · 0.85
NewContextMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…