async def on_close(response: Response) -> None:
response.elapsed = datetime.timedelta(seconds=await timer.async_elapsed())
if hasattr(stream, "aclose"):
- await stream.aclose()
+ with map_exceptions(HTTPCORE_EXC_MAP, request=request):
+ await stream.aclose()
response = Response(
status_code,
assert response.json() == {"app": "mounted"}
+@pytest.mark.usefixtures("async_environment")
+async def test_response_aclose_map_exceptions():
+ class BrokenStream:
+ async def __aiter__(self):
+ # so we're an AsyncIterator
+ pass # pragma: nocover
+
+ async def aclose(self):
+ raise httpcore.CloseError(OSError(104, "Connection reset by peer"))
+
+ def handle(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(200, stream=BrokenStream())
+
+ async with httpx.AsyncClient(transport=httpx.MockTransport(handle)) as client:
+ async with client.stream("GET", "http://example.com") as response:
+ with pytest.raises(httpx.CloseError):
+ await response.aclose()
+
+
@pytest.mark.usefixtures("async_environment")
async def test_async_mock_transport():
async def hello_world(request):