From: Simon Willison Date: Fri, 9 Oct 2020 15:46:26 +0000 (-0700) Subject: Add raw_path to scope in ASGITransport (#1357) X-Git-Tag: 0.17.0~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca5f524943cb6266b42c235bcf45b2c0877a1b35;p=thirdparty%2Fhttpx.git Add raw_path to scope in ASGITransport (#1357) * Add raw_path to scope in ASGITransport, closes #1356 * Tweaked test --- diff --git a/httpx/_transports/asgi.py b/httpx/_transports/asgi.py index 5d4fac22..758d8375 100644 --- a/httpx/_transports/asgi.py +++ b/httpx/_transports/asgi.py @@ -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, diff --git a/tests/test_asgi.py b/tests/test_asgi.py index a8261e93..b16f6824 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -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: