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

Function TestValuesFromCookie

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

Source from the content-addressed store, hash-verified

405}
406
407func TestValuesFromCookie(t *testing.T) {
408 exampleRequest := func(req *http.Request) {
409 req.Header.Set(echo.HeaderCookie, "_csrf=token")
410 }
411
412 var testCases = []struct {
413 name string
414 givenRequest func(req *http.Request)
415 whenName string
416 whenLimit uint
417 expectValues []string
418 expectError string
419 }{
420 {
421 name: "ok, single value",
422 givenRequest: exampleRequest,
423 whenName: "_csrf",
424 expectValues: []string{"token"},
425 },
426 {
427 name: "ok, multiple value",
428 givenRequest: func(req *http.Request) {
429 req.Header.Add(echo.HeaderCookie, "_csrf=token")
430 req.Header.Add(echo.HeaderCookie, "_csrf=token2")
431 },
432 whenName: "_csrf",
433 whenLimit: 2,
434 expectValues: []string{"token", "token2"},
435 },
436 {
437 name: "nok, no matching cookie",
438 givenRequest: exampleRequest,
439 whenName: "xxx",
440 expectValues: nil,
441 expectError: errCookieExtractorValueMissing.Error(),
442 },
443 {
444 name: "nok, no cookies at all",
445 givenRequest: nil,
446 whenName: "xxx",
447 expectValues: nil,
448 expectError: errCookieExtractorValueMissing.Error(),
449 },
450 {
451 name: "ok, cut values over extractorLimit",
452 givenRequest: func(req *http.Request) {
453 for i := 1; i < 25; i++ {
454 req.Header.Add(echo.HeaderCookie, fmt.Sprintf("_csrf=%v", i))
455 }
456 },
457 whenName: "_csrf",
458 whenLimit: extractorLimit,
459 expectValues: []string{
460 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
461 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
462 },
463 },
464 }

Callers

nothing calls this directly

Calls 5

valuesFromCookieFunction · 0.85
SetMethod · 0.80
NewContextMethod · 0.80
AddMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…