]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Force lowercase ASGI headers (#1351)
authorTom Christie <tom@tomchristie.com>
Thu, 8 Oct 2020 11:04:10 +0000 (12:04 +0100)
committerGitHub <noreply@github.com>
Thu, 8 Oct 2020 11:04:10 +0000 (12:04 +0100)
Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
httpx/_transports/asgi.py
tests/test_asgi.py

index b47f68b547aa57478767749ccc5bfa94766d2710..5d4fac2253979c26fed9fd81daf6e880e54d4640 100644 (file)
@@ -87,7 +87,7 @@ class ASGITransport(httpcore.AsyncHTTPTransport):
             "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,
index 4aac735a532f71dfcf8184b6173f5a6e27288968..a8261e933178f7c3a580a177ae59ae6cd7a31dca 100644 (file)
@@ -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()
 
@@ -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: