Skip to content

Commit 3ac3a25

Browse files
committed
test: sse_client propagates bare HTTPStatusError on 4xx/5xx
Sibling to the ConnectError test. The connect-first refactor means raise_for_status() at L65 fires before the task group, so HTTPStatusError propagates bare (not ExceptionGroup-wrapped). The ConnectError test never reaches L65, so it wouldn't catch a regression that moves raise_for_status() back inside the task group. Uses httpx.MockTransport — no socket, no port, deterministic.
1 parent ad1fcb9 commit 3ac3a25

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/client/test_transport_stream_cleanup.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ async def test_sse_client_closes_all_streams_on_connection_error(free_tcp_port:
7070
pytest.fail("should not reach here") # pragma: no cover
7171

7272

73+
@pytest.mark.anyio
74+
async def test_sse_client_closes_all_streams_on_http_error() -> None:
75+
"""sse_client creates streams only after raise_for_status() passes, so an
76+
HTTPStatusError from a 4xx/5xx response propagates bare (not wrapped in an
77+
ExceptionGroup) with nothing to leak — the task group is never entered.
78+
"""
79+
80+
def return_403(request: httpx.Request) -> httpx.Response:
81+
return httpx.Response(403)
82+
83+
def mock_factory(
84+
headers: dict[str, str] | None = None,
85+
timeout: httpx.Timeout | None = None,
86+
auth: httpx.Auth | None = None,
87+
) -> httpx.AsyncClient:
88+
return httpx.AsyncClient(transport=httpx.MockTransport(return_403))
89+
90+
with _assert_no_memory_stream_leak():
91+
with pytest.raises(httpx.HTTPStatusError):
92+
async with sse_client("http://test/sse", httpx_client_factory=mock_factory):
93+
pytest.fail("should not reach here") # pragma: no cover
94+
95+
7396
@pytest.mark.anyio
7497
async def test_streamable_http_client_closes_all_streams_on_exit() -> None:
7598
"""streamable_http_client must close all 4 stream ends on exit.

0 commit comments

Comments
 (0)