]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix exception suppression in asgi transport (#2669)
authorNik <nnonexistent@gmail.com>
Sun, 21 May 2023 00:17:23 +0000 (02:17 +0200)
committerGitHub <noreply@github.com>
Sun, 21 May 2023 00:17:23 +0000 (01:17 +0100)
CHANGELOG.md
httpx/_transports/asgi.py
tests/test_asgi.py

index 71f5b02e34ef33212783081e0c3eebec7c0059d1..d19b2ab8dfbd81272c9d731cdf5361ab55de68ff 100644 (file)
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 
 ### Fixed
 
+* Return `500` error response instead of exceptions when `raise_app_exceptions=False` is set on `ASGITransport`. (#2669)
 * Ensure all WSGITransport environs have a SERVER_PROTOCOL. (#2708)
 
 ## 0.24.1 (17th May, 2023)
index bdf7f7a145f3e6e2dca71de2dfac359bc34090bc..f67f0fbd5b5fb2af1a2137554bc7e9f2539c1127 100644 (file)
@@ -161,9 +161,15 @@ class ASGITransport(AsyncBaseTransport):
         try:
             await self.app(scope, receive, send)
         except Exception:  # noqa: PIE-786
-            if self.raise_app_exceptions or not response_complete.is_set():
+            if self.raise_app_exceptions:
                 raise
 
+            response_complete.set()
+            if status_code is None:
+                status_code = 500
+            if response_headers is None:
+                response_headers = {}
+
         assert response_complete.is_set()
         assert status_code is not None
         assert response_headers is not None
index 17c8d5eb278faea6a9e8d3220104723df12ef322..14d6df6ded4dee3050345e0bfb8870505c444aa9 100644 (file)
@@ -3,6 +3,7 @@ import json
 import pytest
 
 import httpx
+from httpx import ASGITransport
 
 
 async def hello_world(scope, receive, send):
@@ -191,3 +192,12 @@ async def test_asgi_disconnect_after_response_complete():
 
     assert response.status_code == 200
     assert disconnect
+
+
+@pytest.mark.anyio
+async def test_asgi_exc_no_raise():
+    transport = ASGITransport(app=raise_exc, raise_app_exceptions=False)
+    async with httpx.AsyncClient(transport=transport) as client:
+        response = await client.get("http://www.example.org/")
+
+        assert response.status_code == 500