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.
This commit is contained in:
Alexsander Hamir
2025-12-13 10:36:13 -08:00
committed by GitHub
parent 1393c76578
commit 32fdb9e60e
+7 -7
View File
@@ -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")