|
1 | 1 | package http |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "net/http" |
5 | | - "net/http/httptest" |
6 | 4 | "strings" |
7 | 5 | "testing" |
8 | 6 |
|
@@ -220,103 +218,3 @@ func TestJWTClaimsGetScopes(t *testing.T) { |
220 | 218 | } |
221 | 219 | }) |
222 | 220 | } |
223 | | - |
224 | | -func TestAuthorizationMiddleware(t *testing.T) { |
225 | | - // Create a mock handler |
226 | | - handlerCalled := false |
227 | | - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
228 | | - handlerCalled = true |
229 | | - w.WriteHeader(http.StatusOK) |
230 | | - }) |
231 | | - |
232 | | - t.Run("OAuth disabled - passes through", func(t *testing.T) { |
233 | | - handlerCalled = false |
234 | | - |
235 | | - // Create middleware with OAuth disabled |
236 | | - middleware := AuthorizationMiddleware(false, "", nil, nil) |
237 | | - wrappedHandler := middleware(handler) |
238 | | - |
239 | | - // Create request without authorization header |
240 | | - req := httptest.NewRequest("GET", "/test", nil) |
241 | | - w := httptest.NewRecorder() |
242 | | - |
243 | | - wrappedHandler.ServeHTTP(w, req) |
244 | | - |
245 | | - if !handlerCalled { |
246 | | - t.Error("expected handler to be called when OAuth is disabled") |
247 | | - } |
248 | | - if w.Code != http.StatusOK { |
249 | | - t.Errorf("expected status 200, got %d", w.Code) |
250 | | - } |
251 | | - }) |
252 | | - |
253 | | - t.Run("healthz endpoint - passes through", func(t *testing.T) { |
254 | | - handlerCalled = false |
255 | | - |
256 | | - // Create middleware with OAuth enabled |
257 | | - middleware := AuthorizationMiddleware(true, "", nil, nil) |
258 | | - wrappedHandler := middleware(handler) |
259 | | - |
260 | | - // Create request to healthz endpoint |
261 | | - req := httptest.NewRequest("GET", "/healthz", nil) |
262 | | - w := httptest.NewRecorder() |
263 | | - |
264 | | - wrappedHandler.ServeHTTP(w, req) |
265 | | - |
266 | | - if !handlerCalled { |
267 | | - t.Error("expected handler to be called for healthz endpoint") |
268 | | - } |
269 | | - if w.Code != http.StatusOK { |
270 | | - t.Errorf("expected status 200, got %d", w.Code) |
271 | | - } |
272 | | - }) |
273 | | - |
274 | | - t.Run("OAuth enabled - missing token", func(t *testing.T) { |
275 | | - handlerCalled = false |
276 | | - |
277 | | - // Create middleware with OAuth enabled |
278 | | - middleware := AuthorizationMiddleware(true, "", nil, nil) |
279 | | - wrappedHandler := middleware(handler) |
280 | | - |
281 | | - // Create request without authorization header |
282 | | - req := httptest.NewRequest("GET", "/test", nil) |
283 | | - w := httptest.NewRecorder() |
284 | | - |
285 | | - wrappedHandler.ServeHTTP(w, req) |
286 | | - |
287 | | - if handlerCalled { |
288 | | - t.Error("expected handler NOT to be called when token is missing") |
289 | | - } |
290 | | - if w.Code != http.StatusUnauthorized { |
291 | | - t.Errorf("expected status 401, got %d", w.Code) |
292 | | - } |
293 | | - if !strings.Contains(w.Body.String(), "Bearer token required") { |
294 | | - t.Errorf("expected bearer token error message, got %s", w.Body.String()) |
295 | | - } |
296 | | - }) |
297 | | - |
298 | | - t.Run("OAuth enabled - invalid token format", func(t *testing.T) { |
299 | | - handlerCalled = false |
300 | | - |
301 | | - // Create middleware with OAuth enabled |
302 | | - middleware := AuthorizationMiddleware(true, "", nil, nil) |
303 | | - wrappedHandler := middleware(handler) |
304 | | - |
305 | | - // Create request with invalid bearer token |
306 | | - req := httptest.NewRequest("GET", "/test", nil) |
307 | | - req.Header.Set("Authorization", "Bearer invalid-token") |
308 | | - w := httptest.NewRecorder() |
309 | | - |
310 | | - wrappedHandler.ServeHTTP(w, req) |
311 | | - |
312 | | - if handlerCalled { |
313 | | - t.Error("expected handler NOT to be called when token is invalid") |
314 | | - } |
315 | | - if w.Code != http.StatusUnauthorized { |
316 | | - t.Errorf("expected status 401, got %d", w.Code) |
317 | | - } |
318 | | - if !strings.Contains(w.Body.String(), "Invalid token") { |
319 | | - t.Errorf("expected invalid token error message, got %s", w.Body.String()) |
320 | | - } |
321 | | - }) |
322 | | -} |
0 commit comments