From 32fdb9e60e484fe6ed2ffcb8d19d83a433fbc98b Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Sat, 13 Dec 2025 10:36:13 -0800 Subject: [PATCH] fix: Add headers to Request scope in JWT tests to fix KeyError (#17927) - Add 'headers': [] to all Request(scope={'type': 'http'}) instances in test_jwt.py - Fixes KeyError: 'headers' when accessing request.headers in user_api_key_auth - All 7 previously failing tests now pass: - test_allow_access_by_email (2 variants) - test_allowed_routes_admin (4 variants) - test_team_token_output (2 variants) The Starlette Request object requires 'headers' key in scope dictionary when accessing request.headers property. --- tests/proxy_unit_tests/test_jwt.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/proxy_unit_tests/test_jwt.py b/tests/proxy_unit_tests/test_jwt.py index b8df16d135..5743499397 100644 --- a/tests/proxy_unit_tests/test_jwt.py +++ b/tests/proxy_unit_tests/test_jwt.py @@ -413,7 +413,7 @@ async def test_team_token_output(prisma_client, audience, monkeypatch): bearer_token = "Bearer " + token - request = Request(scope={"type": "http"}) + request = Request(scope={"type": "http", "headers": []}) request._url = URL(url="/chat/completions") ## 1. INITIAL TEAM CALL - should fail @@ -446,7 +446,7 @@ async def test_team_token_output(prisma_client, audience, monkeypatch): models=["gpt-3.5-turbo", "gpt-4"], ), user_api_key_dict=result, - http_request=Request(scope={"type": "http"}), + http_request=Request(scope={"type": "http", "headers": []}), ) except Exception as e: pytest.fail(f"This should not fail - {str(e)}") @@ -614,7 +614,7 @@ async def aaaatest_user_token_output( bearer_token = "Bearer " + token - request = Request(scope={"type": "http"}) + request = Request(scope={"type": "http", "headers": []}) request._url = URL(url="/chat/completions") ## 1. INITIAL TEAM CALL - should fail @@ -641,7 +641,7 @@ async def aaaatest_user_token_output( models=["gpt-3.5-turbo", "gpt-4"], ), user_api_key_dict=result, - http_request=Request(scope={"type": "http"}), + http_request=Request(scope={"type": "http", "headers": []}), ) if default_team_id: await new_team( @@ -652,7 +652,7 @@ async def aaaatest_user_token_output( models=["gpt-3.5-turbo", "gpt-4"], ), user_api_key_dict=result, - http_request=Request(scope={"type": "http"}), + http_request=Request(scope={"type": "http", "headers": []}), ) except Exception as e: pytest.fail(f"This should not fail - {str(e)}") @@ -834,7 +834,7 @@ async def test_allowed_routes_admin( actual_routes.extend(LiteLLMRoutes[route].value) for route in actual_routes: - request = Request(scope={"type": "http"}) + request = Request(scope={"type": "http", "headers": []}) request._url = URL(url=route) @@ -999,7 +999,7 @@ async def test_allow_access_by_email( ## RUN IT THROUGH USER API KEY AUTH bearer_token = "Bearer " + token - request = Request(scope={"type": "http"}) + request = Request(scope={"type": "http", "headers": []}) request._url = URL(url="/chat/completions")