fix(security): update security header names to lowercase and add tests

This commit is contained in:
Abhimanyu Saharan
2026-02-25 03:39:49 +05:30
parent f358e4cd3e
commit 58db8be117
2 changed files with 30 additions and 4 deletions

View File

@@ -24,6 +24,32 @@ async def test_security_headers_middleware_passes_through_non_http_scope() -> No
assert called is True
@pytest.mark.asyncio
async def test_security_headers_middleware_appends_lowercase_raw_header_names() -> None:
sent_messages: list[dict[str, object]] = []
async def app(scope, receive, send): # type: ignore[no-untyped-def]
_ = scope
_ = receive
await send({"type": "http.response.start", "status": 200, "headers": []})
await send({"type": "http.response.body", "body": b"", "more_body": False})
async def capture(message): # type: ignore[no-untyped-def]
sent_messages.append(message)
middleware = SecurityHeadersMiddleware(app, x_frame_options="SAMEORIGIN")
await middleware({"type": "http", "method": "GET", "path": "/", "headers": []}, lambda: None, capture)
response_start = next(
message for message in sent_messages if message.get("type") == "http.response.start"
)
headers = response_start.get("headers")
assert isinstance(headers, list)
header_names = {name for name, _value in headers}
assert b"x-frame-options" in header_names
assert b"X-Frame-Options" not in header_names
def test_security_headers_middleware_injects_configured_headers() -> None:
app = FastAPI()
app.add_middleware(