(t *testing.T)
| 85 | } |
| 86 | |
| 87 | func TestAuthFromAuthorizationHeader(t *testing.T) { |
| 88 | validToken := generateValidToken() |
| 89 | |
| 90 | tests := []struct { |
| 91 | name string |
| 92 | header string |
| 93 | wantStatus int |
| 94 | wantClaims map[string]interface{} |
| 95 | }{ |
| 96 | {"Valid Token", "Bearer " + validToken, http.StatusOK, map[string]interface{}{"foo": "bar"}}, |
| 97 | {"Missing Header", "", http.StatusUnauthorized, nil}, |
| 98 | {"Invalid Header Format", "Bearer", http.StatusUnauthorized, nil}, |
| 99 | {"Invalid Token", "Bearer invalidtoken", http.StatusUnauthorized, nil}, |
| 100 | } |
| 101 | |
| 102 | for _, tt := range tests { |
| 103 | t.Run(tt.name, func(t *testing.T) { |
| 104 | req, _ := http.NewRequest("GET", "/", nil) |
| 105 | if tt.header != "" { |
| 106 | req.Header.Set(authorizationKey, tt.header) |
| 107 | } |
| 108 | rr := httptest.NewRecorder() |
| 109 | handler := AuthFromAuthorizationHeader(mockKeyFunc, genericClaimsFunc(), mockSigningMethod, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 110 | claims, ok := jwtmiddleware.FromContext(r.Context()) |
| 111 | mapClaims := claims.(*jwt.MapClaims) |
| 112 | if tt.wantClaims != nil { |
| 113 | assert.True(t, ok, "claims not found in context") |
| 114 | for key, value := range tt.wantClaims { |
| 115 | claimsVal, exists := (*mapClaims)[key] |
| 116 | assert.True(t, exists, "claims missing key: %v", key) |
| 117 | assert.Equal(t, value, claimsVal, "claims value mismatch for key: %v", key) |
| 118 | } |
| 119 | } |
| 120 | w.WriteHeader(http.StatusOK) |
| 121 | })) |
| 122 | handler.ServeHTTP(rr, req) |
| 123 | |
| 124 | assert.Equal(t, tt.wantStatus, rr.Code, "handler returned wrong status code") |
| 125 | }) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // CustomClaimsA represents the first custom JWT claims type |
| 130 | type CustomClaimsA struct { |
nothing calls this directly
no test coverage detected