Make request header lookups case-insensitive

Closes #1861
This commit is contained in:
Simon Willison 2026-09-16 10:27:35 -07:00 • committed by GitHub
commit df4c0fde0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 63 additions and 4 deletions

View file

@ -35,6 +35,51 @@ def _receive_chunks(chunks):
return receive
@pytest.mark.parametrize(
"header_name", [b"content-type", b"Content-Type", b"CONTENT-TYPE"]
)
@pytest.mark.parametrize("lookup", ["content-type", "Content-Type", "CONTENT-TYPE"])
def test_request_headers_case_insensitive(header_name, lookup):
request = Request({"headers": [(header_name, b"application/json")]}, None)
assert request.headers.get(lookup) == "application/json"
assert request.headers[lookup] == "application/json"
assert lookup in request.headers
def test_request_headers_mapping():
request = Request(
{
"headers": [
(b"Content-Type", b"application/json"),
(b"X-Title", "café".encode("latin-1")),
(b"CONTENT-TYPE", b"text/plain"),
]
},
None,
)
headers = request.headers
expected = {"content-type": "text/plain", "x-title": "café"}
assert headers == expected
assert dict(headers) == expected
assert list(headers) == list(expected)
assert list(headers.keys()) == list(expected.keys())
assert list(headers.items()) == list(expected.items())
assert json.loads(json.dumps(headers)) == expected
assert headers["Content-Type"] == "text/plain"
assert headers["X-Title"] == "café"
@pytest.mark.parametrize("scope", [{}, {"headers": None}, {"headers": []}])
def test_request_headers_missing(scope):
headers = Request(scope, None).headers
assert headers == {}
assert headers.get("Content-Type") is None
assert headers.get("Content-Type", "default") == "default"
assert "Content-Type" not in headers
with pytest.raises(KeyError):
headers["Content-Type"]
@pytest.mark.asyncio
async def test_request_post_vars():
scope = {