diff --git a/tests/client/test_async_client.py b/tests/client/test_async_client.py index 8d7eaa3c58..5c1956ccb1 100644 --- a/tests/client/test_async_client.py +++ b/tests/client/test_async_client.py @@ -373,3 +373,26 @@ async def test_server_extensions(server): response = await client.get(url) assert response.status_code == 200 assert response.extensions["http_version"] == b"HTTP/1.1" + + +@pytest.mark.anyio +async def test_aclose_when_already_closed(): + client = httpx.AsyncClient() + await client.aclose() + + # Closing an already-closed client is a no-op. + await client.aclose() + + assert client.is_closed + + +@pytest.mark.anyio +async def test_mounted_none_transport(): + # Mounts may use `None` to fall back to the default transport for a + # given URL pattern. Entering, exiting and closing the client should + # skip over these `None` entries. + async with httpx.AsyncClient(mounts={"custom://": None}): + pass + + client = httpx.AsyncClient(mounts={"custom://": None}) + await client.aclose() diff --git a/tests/client/test_client.py b/tests/client/test_client.py index 657839018a..ff7d802dfb 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -460,3 +460,38 @@ def cp1252_but_no_content_type(request): assert response.reason_phrase == "OK" assert response.encoding == "ISO-8859-1" assert response.text == text + + +def test_build_request_with_timeout_extension(): + with httpx.Client() as client: + request = client.build_request( + "GET", + "http://example.org", + extensions={"timeout": {"connect": 5.0}}, + ) + + assert request.extensions == {"timeout": {"connect": 5.0}} + + +def test_close_when_already_closed(): + client = httpx.Client() + client.close() + + # Closing an already-closed client is a no-op. + client.close() + + assert client.is_closed + + +def test_mounted_none_transport(): + # Mounts may use `None` to fall back to the default transport for a + # given URL pattern. Entering, exiting and closing the client should + # skip over these `None` entries. + client = httpx.Client(mounts={"custom://": None}) + with client: + pass + + client = httpx.Client(mounts={"custom://": None}) + client.close() + + assert client.is_closed diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py index d2972da5bd..d186aa344a 100644 --- a/tests/models/test_responses.py +++ b/tests/models/test_responses.py @@ -1038,3 +1038,15 @@ def test_response_decode_text_using_explicit_encoding(): assert response.reason_phrase == "OK" assert response.encoding == "cp1252" assert response.text == text + + +def test_encoding_with_default_encoding_callable_and_unread_stream(): + # For a streaming response that has not been read yet, a callable + # `default_encoding` cannot be applied, so the fallback is "utf-8". + response = httpx.Response( + 200, + content=streaming_body(), + default_encoding=lambda content: "utf-16", + ) + + assert response.encoding == "utf-8" diff --git a/tests/test_asgi.py b/tests/test_asgi.py index ffbc91bc00..da75c0cb4d 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -222,3 +222,44 @@ async def test_asgi_exc_no_raise(): response = await client.get("http://www.example.org/") assert response.status_code == 500 + + +async def raise_exc_after_response_start(scope, receive, send): + status = 200 + headers = [(b"content-type", "text/plain")] + + await send({"type": "http.response.start", "status": status, "headers": headers}) + raise RuntimeError() + + +@pytest.mark.anyio +async def test_asgi_exc_no_raise_after_response_start(): + transport = httpx.ASGITransport( + app=raise_exc_after_response_start, raise_app_exceptions=False + ) + async with httpx.AsyncClient(transport=transport) as client: + response = await client.get("http://www.example.org/") + + assert response.status_code == 200 + assert response.content == b"" + + +@pytest.mark.anyio +async def test_asgi_ignores_debug_messages(): + async def app(scope, receive, send): + await send({"type": "http.response.debug", "info": {"test": "example"}}) + await send( + { + "type": "http.response.start", + "status": 200, + "headers": [(b"content-type", "text/plain")], + } + ) + await send({"type": "http.response.body", "body": b"Hello, World!"}) + + transport = httpx.ASGITransport(app=app) + async with httpx.AsyncClient(transport=transport) as client: + response = await client.get("http://www.example.org/") + + assert response.status_code == 200 + assert response.text == "Hello, World!" diff --git a/tests/test_multipart.py b/tests/test_multipart.py index 764f85a253..414a13e55e 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -467,3 +467,40 @@ def test_unicode_with_control_character(self): files = {"upload": (filename, b"")} request = httpx.Request("GET", "https://www.example.com", files=files) assert expected in request.read() + + +def test_multipart_encode_filelike_without_seek() -> None: + """ + File-like objects that don't provide a `seek()` method at all are + supported, uploading with 'Transfer-Encoding: chunked'. + """ + + class ReadOnlyFile: + def __init__(self, content: bytes) -> None: + self._content = content + + def read(self, *args: typing.Any) -> bytes: + content, self._content = self._content, b"" + return content + + url = "https://www.example.com/" + headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"} + fileobj: typing.Any = ReadOnlyFile(b"Hello, World!") + files = {"file": fileobj} + + request = httpx.Request("POST", url, headers=headers, files=files) + request.read() + + assert request.headers == { + "Host": "www.example.com", + "Content-Type": "multipart/form-data; boundary=BOUNDARY", + "Transfer-Encoding": "chunked", + } + assert request.content == ( + b"--BOUNDARY\r\n" + b'Content-Disposition: form-data; name="file"; filename="upload"\r\n' + b"Content-Type: application/octet-stream\r\n" + b"\r\n" + b"Hello, World!\r\n" + b"--BOUNDARY--\r\n" + )