]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add raw_path to scope in ASGITransport (#1357)
authorSimon Willison <swillison@gmail.com>
Fri, 9 Oct 2020 15:46:26 +0000 (08:46 -0700)
committerGitHub <noreply@github.com>
Fri, 9 Oct 2020 15:46:26 +0000 (16:46 +0100)
* Add raw_path to scope in ASGITransport, closes #1356

* Tweaked test

httpx/_transports/asgi.py
tests/test_asgi.py

index 5d4fac2253979c26fed9fd81daf6e880e54d4640..758d8375b2165189662df29a37844a348facda89 100644 (file)
@@ -90,6 +90,7 @@ class ASGITransport(httpcore.AsyncHTTPTransport):
             "headers": [(k.lower(), v) for (k, v) in headers],
             "scheme": scheme.decode("ascii"),
             "path": unquote(path.decode("ascii")),
+            "raw_path": path,
             "query_string": query,
             "server": (host.decode("ascii"), port),
             "client": self.client,
index a8261e933178f7c3a580a177ae59ae6cd7a31dca..b16f68246cd5070db606a9654c7bdef689445017 100644 (file)
@@ -23,6 +23,15 @@ async def echo_path(scope, receive, send):
     await send({"type": "http.response.body", "body": output})
 
 
+async def echo_raw_path(scope, receive, send):
+    status = 200
+    output = json.dumps({"raw_path": scope["raw_path"].decode("ascii")}).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 echo_body(scope, receive, send):
     status = 200
     headers = [(b"content-type", "text/plain")]
@@ -80,6 +89,16 @@ async def test_asgi_urlencoded_path():
     assert response.json() == {"path": "/user@example.org"}
 
 
+@pytest.mark.usefixtures("async_environment")
+async def test_asgi_raw_path():
+    async with httpx.AsyncClient(app=echo_raw_path) as client:
+        url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")
+        response = await client.get(url)
+
+    assert response.status_code == 200
+    assert response.json() == {"raw_path": "/user%40example.org"}
+
+
 @pytest.mark.usefixtures("async_environment")
 async def test_asgi_upload():
     async with httpx.AsyncClient(app=echo_body) as client: