]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
map_exceptions in request.aclose() (#1465)
authorAdam Hooper <adam@adamhooper.com>
Wed, 17 Feb 2021 11:32:43 +0000 (06:32 -0500)
committerGitHub <noreply@github.com>
Wed, 17 Feb 2021 11:32:43 +0000 (11:32 +0000)
* map_exceptions in request.aclose()

httpx/_client.py
tests/client/test_async_client.py

index b300cccb1f6669486e46229455762cc653aa240a..3465a10b758830201f6b7406ffef0026a3b79758 100644 (file)
@@ -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,
index 62464865e20cf3696465d5daba51a9e3a1cdc1c3..1d3f4ccafa6faa29f65ca9eece76127f47d28896 100644 (file)
@@ -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):