|
1 | 1 | import json |
2 | 2 |
|
| 3 | +import anyio |
3 | 4 | import pytest |
4 | 5 |
|
5 | 6 | import httpx |
@@ -60,13 +61,24 @@ async def raise_exc(scope, receive, send): |
60 | 61 | raise RuntimeError() |
61 | 62 |
|
62 | 63 |
|
| 64 | +async def raise_exc_after_response_start(scope, receive, send): |
| 65 | + status = 200 |
| 66 | + output = b"Hello, World!" |
| 67 | + headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))] |
| 68 | + |
| 69 | + await send({"type": "http.response.start", "status": status, "headers": headers}) |
| 70 | + await anyio.sleep(0) |
| 71 | + raise RuntimeError() |
| 72 | + |
| 73 | + |
63 | 74 | async def raise_exc_after_response(scope, receive, send): |
64 | 75 | status = 200 |
65 | 76 | output = b"Hello, World!" |
66 | 77 | headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))] |
67 | 78 |
|
68 | 79 | await send({"type": "http.response.start", "status": status, "headers": headers}) |
69 | 80 | await send({"type": "http.response.body", "body": output}) |
| 81 | + await anyio.sleep(0) |
70 | 82 | raise RuntimeError() |
71 | 83 |
|
72 | 84 |
|
@@ -172,6 +184,14 @@ async def test_asgi_exc(): |
172 | 184 | await client.get("http://www.example.org/") |
173 | 185 |
|
174 | 186 |
|
| 187 | +@pytest.mark.anyio |
| 188 | +async def test_asgi_exc_after_response_start(): |
| 189 | + transport = httpx.ASGITransport(app=raise_exc_after_response_start) |
| 190 | + async with httpx.AsyncClient(transport=transport) as client: |
| 191 | + with pytest.raises(RuntimeError): |
| 192 | + await client.get("http://www.example.org/") |
| 193 | + |
| 194 | + |
175 | 195 | @pytest.mark.anyio |
176 | 196 | async def test_asgi_exc_after_response(): |
177 | 197 | transport = httpx.ASGITransport(app=raise_exc_after_response) |
@@ -222,3 +242,97 @@ async def test_asgi_exc_no_raise(): |
222 | 242 | response = await client.get("http://www.example.org/") |
223 | 243 |
|
224 | 244 | assert response.status_code == 500 |
| 245 | + |
| 246 | + |
| 247 | +@pytest.mark.anyio |
| 248 | +async def test_asgi_exc_no_raise_after_response_start(): |
| 249 | + transport = httpx.ASGITransport( |
| 250 | + app=raise_exc_after_response_start, raise_app_exceptions=False |
| 251 | + ) |
| 252 | + async with httpx.AsyncClient(transport=transport) as client: |
| 253 | + response = await client.get("http://www.example.org/") |
| 254 | + |
| 255 | + assert response.status_code == 200 |
| 256 | + |
| 257 | + |
| 258 | +@pytest.mark.anyio |
| 259 | +async def test_asgi_exc_no_raise_after_response(): |
| 260 | + transport = httpx.ASGITransport( |
| 261 | + app=raise_exc_after_response, raise_app_exceptions=False |
| 262 | + ) |
| 263 | + async with httpx.AsyncClient(transport=transport) as client: |
| 264 | + response = await client.get("http://www.example.org/") |
| 265 | + |
| 266 | + assert response.status_code == 200 |
| 267 | + |
| 268 | + |
| 269 | +@pytest.mark.anyio |
| 270 | +async def test_asgi_stream_returns_before_waiting_for_body(): |
| 271 | + start_response_body = anyio.Event() |
| 272 | + |
| 273 | + async def send_response_body_after_event(scope, receive, send): |
| 274 | + status = 200 |
| 275 | + headers = [(b"content-type", b"text/plain")] |
| 276 | + await send( |
| 277 | + {"type": "http.response.start", "status": status, "headers": headers} |
| 278 | + ) |
| 279 | + await start_response_body.wait() |
| 280 | + await send({"type": "http.response.body", "body": b"body", "more_body": False}) |
| 281 | + |
| 282 | + transport = httpx.ASGITransport(app=send_response_body_after_event) |
| 283 | + async with httpx.AsyncClient(transport=transport) as client: |
| 284 | + async with client.stream("GET", "http://www.example.org/") as response: |
| 285 | + assert response.status_code == 200 |
| 286 | + start_response_body.set() |
| 287 | + await response.aread() |
| 288 | + assert response.text == "body" |
| 289 | + |
| 290 | + |
| 291 | +@pytest.mark.anyio |
| 292 | +async def test_asgi_stream_allows_iterative_streaming(): |
| 293 | + stream_events = [anyio.Event() for i in range(4)] |
| 294 | + |
| 295 | + async def send_response_body_after_event(scope, receive, send): |
| 296 | + status = 200 |
| 297 | + headers = [(b"content-type", b"text/plain")] |
| 298 | + await send( |
| 299 | + {"type": "http.response.start", "status": status, "headers": headers} |
| 300 | + ) |
| 301 | + for e in stream_events: |
| 302 | + await e.wait() |
| 303 | + await send( |
| 304 | + { |
| 305 | + "type": "http.response.body", |
| 306 | + "body": b"chunk", |
| 307 | + "more_body": e is not stream_events[-1], |
| 308 | + } |
| 309 | + ) |
| 310 | + |
| 311 | + transport = httpx.ASGITransport(app=send_response_body_after_event) |
| 312 | + async with httpx.AsyncClient(transport=transport) as client: |
| 313 | + async with client.stream("GET", "http://www.example.org/") as response: |
| 314 | + assert response.status_code == 200 |
| 315 | + iterator = response.aiter_raw() |
| 316 | + for e in stream_events: |
| 317 | + e.set() |
| 318 | + assert await iterator.__anext__() == b"chunk" |
| 319 | + with pytest.raises(StopAsyncIteration): |
| 320 | + await iterator.__anext__() |
| 321 | + |
| 322 | + |
| 323 | +@pytest.mark.anyio |
| 324 | +async def test_asgi_can_be_canceled(): |
| 325 | + # This test exists to cover transmission of the cancellation exception through |
| 326 | + # _AwaitableRunner |
| 327 | + app_started = anyio.Event() |
| 328 | + |
| 329 | + async def never_return(scope, receive, send): |
| 330 | + app_started.set() |
| 331 | + await anyio.sleep_forever() |
| 332 | + |
| 333 | + transport = httpx.ASGITransport(app=never_return) |
| 334 | + async with httpx.AsyncClient(transport=transport) as client: |
| 335 | + async with anyio.create_task_group() as task_group: |
| 336 | + task_group.start_soon(client.get, "http://www.example.org/") |
| 337 | + await app_started.wait() |
| 338 | + task_group.cancel_scope.cancel() |
0 commit comments