From 2fcaefa5749494f6f85dd63d74a9df6109f83c22 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 23 Sep 2020 09:25:47 +0100 Subject: [PATCH 1/3] Fix ASGITransport path escaping --- httpx/_transports/asgi.py | 3 ++- tests/test_asgi.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/httpx/_transports/asgi.py b/httpx/_transports/asgi.py index 94976f3aad..b47f68b547 100644 --- a/httpx/_transports/asgi.py +++ b/httpx/_transports/asgi.py @@ -1,4 +1,5 @@ from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union +from urllib.parse import unquote import httpcore import sniffio @@ -88,7 +89,7 @@ async def arequest( "method": method.decode(), "headers": headers, "scheme": scheme.decode("ascii"), - "path": path.decode("ascii"), + "path": unquote(path.decode("ascii")), "query_string": query, "server": (host.decode("ascii"), port), "client": self.client, diff --git a/tests/test_asgi.py b/tests/test_asgi.py index 70da3c54ad..4aac735a53 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -1,3 +1,5 @@ +import json + import pytest import httpx @@ -12,6 +14,15 @@ async def hello_world(scope, receive, send): await send({"type": "http.response.body", "body": output}) +async def echo_path(scope, receive, send): + status = 200 + output = json.dumps({"path": scope["path"]}).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")] @@ -48,6 +59,16 @@ async def test_asgi(): assert response.text == "Hello, World!" +@pytest.mark.usefixtures("async_environment") +async def test_asgi_urlencoded_path(): + async with httpx.AsyncClient(app=echo_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() == {"path": "/user@example.org"} + + @pytest.mark.usefixtures("async_environment") async def test_asgi_upload(): async with httpx.AsyncClient(app=echo_body) as client: From a036d00ac0ad248dcc023837c0a7da76d12fa18e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 23 Sep 2020 10:59:55 +0100 Subject: [PATCH 2/3] Add failing test case for auth with streaming --- tests/client/test_auth.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index e3d328c6fa..e3aa46fb8c 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -162,6 +162,20 @@ async def test_basic_auth() -> None: assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="} +@pytest.mark.asyncio +async def test_basic_auth_with_stream() -> None: + url = "https://example.org/" + auth = ("tomchristie", "password123") + app = App() + + async with httpx.AsyncClient(transport=MockTransport(app), auth=auth) as client: + async with client.stream("GET", url) as response: + response.read() + + assert response.status_code == 200 + assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="} + + @pytest.mark.asyncio async def test_basic_auth_in_url() -> None: url = "https://tomchristie:password123@example.org/" From 385519d888541d28b574949de36aa8876814b739 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 23 Sep 2020 11:04:24 +0100 Subject: [PATCH 3/3] Fix .stream setting auth=None --- httpx/_client.py | 4 ++-- tests/client/test_auth.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index 6208535f0b..4661b221c5 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -234,7 +234,7 @@ def stream( params: QueryParamTypes = None, headers: HeaderTypes = None, cookies: CookieTypes = None, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, ) -> "StreamContextManager": @@ -1786,7 +1786,7 @@ def __init__( client: BaseClient, request: Request, *, - auth: AuthTypes = None, + auth: typing.Union[AuthTypes, UnsetType] = UNSET, allow_redirects: bool = True, timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET, close_client: bool = False, diff --git a/tests/client/test_auth.py b/tests/client/test_auth.py index e3aa46fb8c..efbfcc36c4 100644 --- a/tests/client/test_auth.py +++ b/tests/client/test_auth.py @@ -164,6 +164,9 @@ async def test_basic_auth() -> None: @pytest.mark.asyncio async def test_basic_auth_with_stream() -> None: + """ + See: https://github.com/encode/httpx/pull/1312 + """ url = "https://example.org/" auth = ("tomchristie", "password123") app = App()