From: Adam Hooper Date: Wed, 17 Feb 2021 11:32:43 +0000 (-0500) Subject: map_exceptions in request.aclose() (#1465) X-Git-Tag: 0.17.0~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0f280af8b170ed5cc48c12a894f71a8b5762f748;p=thirdparty%2Fhttpx.git map_exceptions in request.aclose() (#1465) * map_exceptions in request.aclose() --- diff --git a/httpx/_client.py b/httpx/_client.py index b300cccb..3465a10b 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -1500,7 +1500,8 @@ class AsyncClient(BaseClient): 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, diff --git a/tests/client/test_async_client.py b/tests/client/test_async_client.py index 62464865..1d3f4cca 100644 --- a/tests/client/test_async_client.py +++ b/tests/client/test_async_client.py @@ -303,6 +303,25 @@ async def test_mounted_transport(): 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):