From 2dbace2431e2f15972e0865cd2bfafe8917789fe Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:04:17 +0530 Subject: [PATCH 1/7] Add client branch-coverage tests --- tests/client/test_client.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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 From a8b0f09739fdaec3a044133798f1b6cec882b4f0 Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:04:39 +0530 Subject: [PATCH 2/7] Add async client branch-coverage tests Add tests for AsyncClient closure and None transport handling --- tests/client/test_async_client.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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() From 962d3498f7d47e1d45eea162db2b75ad602699fa Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:04:55 +0530 Subject: [PATCH 3/7] Add ASGI transport branch-coverage tests --- tests/test_asgi.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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!" From 750f843b7ad4c9cc17715b429b4b32fb8138ca79 Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:05:36 +0530 Subject: [PATCH 4/7] Add multipart test for file-like without seek Add test for multipart encoding with file-like object --- tests/test_multipart.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_multipart.py b/tests/test_multipart.py index 764f85a253..d7e96d3801 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -467,3 +467,39 @@ 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"} + files = {"file": ReadOnlyFile(b"Hello, World!")} + + 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" + ) From 7d507c6da15030ade050e6de4cc8257ddeecf799 Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:06:15 +0530 Subject: [PATCH 5/7] Add encoding fallback test for unread streams Add test for encoding with default encoding callable on unread stream. --- tests/models/test_responses.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py index d2972da5bd..7d22b52af8 100644 --- a/tests/models/test_responses.py +++ b/tests/models/test_responses.py @@ -1038,3 +1038,18 @@ 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". + def autodetect(content: bytes) -> str: + return "utf-16" + + response = httpx.Response( + 200, + content=streaming_body(), + default_encoding=autodetect, + ) + + assert response.encoding == "utf-8" From 2036657b7422ddcfcb0c0be7dd20794321b33d19 Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Fri, 4 Dec 2026 17:55:23 +0530 Subject: [PATCH 6/7] Fix files argument typing in no-seek multipart test --- tests/test_multipart.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_multipart.py b/tests/test_multipart.py index d7e96d3801..414a13e55e 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -485,7 +485,8 @@ def read(self, *args: typing.Any) -> bytes: url = "https://www.example.com/" headers = {"Content-Type": "multipart/form-data; boundary=BOUNDARY"} - files = {"file": ReadOnlyFile(b"Hello, World!")} + fileobj: typing.Any = ReadOnlyFile(b"Hello, World!") + files = {"file": fileobj} request = httpx.Request("POST", url, headers=headers, files=files) request.read() From a318ee72382d6161a89e1dc03fb3e795fe51c040 Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Fri, 4 Dec 2026 17:58:07 +0530 Subject: [PATCH 7/7] Use inline lambda for never-called encoding callback Replaced autodetect function with a lambda for default encoding. --- tests/models/test_responses.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/models/test_responses.py b/tests/models/test_responses.py index 7d22b52af8..d186aa344a 100644 --- a/tests/models/test_responses.py +++ b/tests/models/test_responses.py @@ -1043,13 +1043,10 @@ def test_response_decode_text_using_explicit_encoding(): 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". - def autodetect(content: bytes) -> str: - return "utf-16" - response = httpx.Response( 200, content=streaming_body(), - default_encoding=autodetect, + default_encoding=lambda content: "utf-16", ) assert response.encoding == "utf-8"