|
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 |
|
@@ -165,6 +177,14 @@ async def test_asgi_exc(): |
165 | 177 | await client.get("http://www.example.org/") |
166 | 178 |
|
167 | 179 |
|
| 180 | +@pytest.mark.anyio |
| 181 | +async def test_asgi_exc_after_response_start(): |
| 182 | + transport = httpx.ASGITransport(app=raise_exc_after_response_start) |
| 183 | + async with httpx.AsyncClient(transport=transport) as client: |
| 184 | + with pytest.raises(RuntimeError): |
| 185 | + await client.get("http://www.example.org/") |
| 186 | + |
| 187 | + |
168 | 188 | @pytest.mark.anyio |
169 | 189 | async def test_asgi_exc_after_response(): |
170 | 190 | async with httpx.AsyncClient(app=raise_exc_after_response) as client: |
@@ -213,3 +233,97 @@ async def test_asgi_exc_no_raise(): |
213 | 233 | response = await client.get("http://www.example.org/") |
214 | 234 |
|
215 | 235 | assert response.status_code == 500 |
| 236 | + |
| 237 | + |
| 238 | +@pytest.mark.anyio |
| 239 | +async def test_asgi_exc_no_raise_after_response_start(): |
| 240 | + transport = httpx.ASGITransport( |
| 241 | + app=raise_exc_after_response_start, raise_app_exceptions=False |
| 242 | + ) |
| 243 | + async with httpx.AsyncClient(transport=transport) as client: |
| 244 | + response = await client.get("http://www.example.org/") |
| 245 | + |
| 246 | + assert response.status_code == 200 |
| 247 | + |
| 248 | + |
| 249 | +@pytest.mark.anyio |
| 250 | +async def test_asgi_exc_no_raise_after_response(): |
| 251 | + transport = httpx.ASGITransport( |
| 252 | + app=raise_exc_after_response, raise_app_exceptions=False |
| 253 | + ) |
| 254 | + async with httpx.AsyncClient(transport=transport) as client: |
| 255 | + response = await client.get("http://www.example.org/") |
| 256 | + |
| 257 | + assert response.status_code == 200 |
| 258 | + |
| 259 | + |
| 260 | +@pytest.mark.anyio |
| 261 | +async def test_asgi_stream_returns_before_waiting_for_body(): |
| 262 | + start_response_body = anyio.Event() |
| 263 | + |
| 264 | + async def send_response_body_after_event(scope, receive, send): |
| 265 | + status = 200 |
| 266 | + headers = [(b"content-type", b"text/plain")] |
| 267 | + await send( |
| 268 | + {"type": "http.response.start", "status": status, "headers": headers} |
| 269 | + ) |
| 270 | + await start_response_body.wait() |
| 271 | + await send({"type": "http.response.body", "body": b"body", "more_body": False}) |
| 272 | + |
| 273 | + transport = httpx.ASGITransport(app=send_response_body_after_event) |
| 274 | + async with httpx.AsyncClient(transport=transport) as client: |
| 275 | + async with client.stream("GET", "http://www.example.org/") as response: |
| 276 | + assert response.status_code == 200 |
| 277 | + start_response_body.set() |
| 278 | + await response.aread() |
| 279 | + assert response.text == "body" |
| 280 | + |
| 281 | + |
| 282 | +@pytest.mark.anyio |
| 283 | +async def test_asgi_stream_allows_iterative_streaming(): |
| 284 | + stream_events = [anyio.Event() for i in range(4)] |
| 285 | + |
| 286 | + async def send_response_body_after_event(scope, receive, send): |
| 287 | + status = 200 |
| 288 | + headers = [(b"content-type", b"text/plain")] |
| 289 | + await send( |
| 290 | + {"type": "http.response.start", "status": status, "headers": headers} |
| 291 | + ) |
| 292 | + for e in stream_events: |
| 293 | + await e.wait() |
| 294 | + await send( |
| 295 | + { |
| 296 | + "type": "http.response.body", |
| 297 | + "body": b"chunk", |
| 298 | + "more_body": e is not stream_events[-1], |
| 299 | + } |
| 300 | + ) |
| 301 | + |
| 302 | + transport = httpx.ASGITransport(app=send_response_body_after_event) |
| 303 | + async with httpx.AsyncClient(transport=transport) as client: |
| 304 | + async with client.stream("GET", "http://www.example.org/") as response: |
| 305 | + assert response.status_code == 200 |
| 306 | + iterator = response.aiter_raw() |
| 307 | + for e in stream_events: |
| 308 | + e.set() |
| 309 | + assert await iterator.__anext__() == b"chunk" |
| 310 | + with pytest.raises(StopAsyncIteration): |
| 311 | + await iterator.__anext__() |
| 312 | + |
| 313 | + |
| 314 | +@pytest.mark.anyio |
| 315 | +async def test_asgi_can_be_canceled(): |
| 316 | + # This test exists to cover transmission of the cancellation exception through |
| 317 | + # _AwaitableRunner |
| 318 | + app_started = anyio.Event() |
| 319 | + |
| 320 | + async def never_return(scope, receive, send): |
| 321 | + app_started.set() |
| 322 | + await anyio.sleep_forever() |
| 323 | + |
| 324 | + transport = httpx.ASGITransport(app=never_return) |
| 325 | + async with httpx.AsyncClient(transport=transport) as client: |
| 326 | + async with anyio.create_task_group() as task_group: |
| 327 | + task_group.start_soon(client.get, "http://www.example.org/") |
| 328 | + await app_started.wait() |
| 329 | + task_group.cancel_scope.cancel() |
0 commit comments