(t *testing.T)
| 38 | ) |
| 39 | |
| 40 | func 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: |
nothing calls this directly
no test coverage detected