MCPcopy
hub / github.com/gofiber/fiber / Test_AuthSources

Function Test_AuthSources

middleware/keyauth/keyauth_test.go:40–195  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

38)
39
40func Test_AuthSources(t *testing.T) {
41 // define test cases
42 testSources := []string{headerExtractorName, authHeaderExtractorName, cookieExtractorName, queryExtractorName, paramExtractorName, formExtractorName}
43
44 tests := []struct {
45 route string
46 authTokenName string
47 description string
48 APIKey string
49 expectedBody string
50 expectedCode int
51 }{
52 {
53 route: "/",
54 authTokenName: "access_token",
55 description: "auth with correct key",
56 APIKey: CorrectKey,
57 expectedCode: 200,
58 expectedBody: "Success!",
59 },
60 {
61 route: "/",
62 authTokenName: "access_token",
63 description: "auth with no key",
64 APIKey: "",
65 expectedCode: 401, // 404 in case of param authentication
66 expectedBody: ErrMissingOrMalformedAPIKey.Error(),
67 },
68 {
69 route: "/",
70 authTokenName: "access_token",
71 description: "auth with wrong key",
72 APIKey: "WRONGKEY",
73 expectedCode: 401,
74 expectedBody: ErrMissingOrMalformedAPIKey.Error(),
75 },
76 }
77
78 for _, authSource := range testSources {
79 t.Run(authSource, func(t *testing.T) {
80 for _, test := range tests {
81 app := fiber.New(fiber.Config{UnescapePath: true})
82
83 testKey := test.APIKey
84 correctKey := CorrectKey
85
86 // Use a simple key for param and cookie to avoid encoding issues in the test setup
87 if authSource == paramExtractorName || authSource == cookieExtractorName {
88 if test.APIKey != "" && test.APIKey != "WRONGKEY" {
89 testKey = "simple-key"
90 correctKey = "simple-key"
91 }
92 }
93
94 authMiddleware := New(Config{
95 Extractor: func() extractors.Extractor {
96 switch authSource {
97 case headerExtractorName:

Callers

nothing calls this directly

Calls 15

FromHeaderFunction · 0.92
FromAuthHeaderFunction · 0.92
FromCookieFunction · 0.92
FromQueryFunction · 0.92
FromParamFunction · 0.92
FromFormFunction · 0.92
TestMethod · 0.80
NewFunction · 0.70
ErrorMethod · 0.65
NewMethod · 0.65
SendStringMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected