Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force lowercase ASGI headers #1351

Merged
merged 2 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpx/_transports/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def arequest(
"asgi": {"version": "3.0"},
"http_version": "1.1",
"method": method.decode(),
"headers": headers,
"headers": [(k.lower(), v) for (k, v) in headers],
"scheme": scheme.decode("ascii"),
"path": unquote(path.decode("ascii")),
"query_string": query,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ async def echo_body(scope, receive, send):
await send({"type": "http.response.body", "body": body, "more_body": more_body})


async def echo_headers(scope, receive, send):
status = 200
output = json.dumps(
{"headers": [[k.decode(), v.decode()] for k, v in scope["headers"]]}
).encode("utf-8")
headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]

await send({"type": "http.response.start", "status": status, "headers": headers})
await send({"type": "http.response.body", "body": output})


async def raise_exc(scope, receive, send):
raise RuntimeError()

Expand Down Expand Up @@ -78,6 +89,23 @@ async def test_asgi_upload():
assert response.text == "example"


@pytest.mark.usefixtures("async_environment")
async def test_asgi_headers():
async with httpx.AsyncClient(app=echo_headers) as client:
response = await client.get("http://www.example.org/")

assert response.status_code == 200
assert response.json() == {
"headers": [
["host", "www.example.org"],
["accept", "*/*"],
["accept-encoding", "gzip, deflate, br"],
["connection", "keep-alive"],
["user-agent", f"python-httpx/{httpx.__version__}"],
]
}


@pytest.mark.usefixtures("async_environment")
async def test_asgi_exc():
async with httpx.AsyncClient(app=raise_exc) as client:
Expand Down