test_asgi.py
This file
Path
tests/test_asgi.py
101 lines · 3.2 KiB · Python
Last changed by
ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
History of this fileTake it
The walk stopped after 200 commits: lines older than c033ed1 point at it.
TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 1–43why, one step back1import pytest23import httpx456async def hello_world(scope, receive, send):7 status = 2008 output = b"Hello, World!"9 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]1011 await send({"type": "http.response.start", "status": status, "headers": headers})12 await send({"type": "http.response.body", "body": output})131415async def echo_body(scope, receive, send):16 status = 20017 headers = [(b"content-type", "text/plain")]1819 await send({"type": "http.response.start", "status": status, "headers": headers})20 more_body = True21 while more_body:22 message = await receive()23 body = message.get("body", b"")24 more_body = message.get("more_body", False)25 await send({"type": "http.response.body", "body": body, "more_body": more_body})262728async def raise_exc(scope, receive, send):29 raise ValueError()303132async def raise_exc_after_response(scope, receive, send):33 status = 20034 output = b"Hello, World!"35 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]3637 await send({"type": "http.response.start", "status": status, "headers": headers})38 await send({"type": "http.response.body", "body": output})39 raise ValueError()404142@pytest.mark.asyncio43async def test_asgi():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 44why, one step back44 client = httpx.AsyncClient(app=hello_world)TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 45–51why, one step back45 response = await client.get("http://www.example.org/")46 assert response.status_code == 20047 assert response.text == "Hello, World!"484950@pytest.mark.asyncio51async def test_asgi_upload():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 52why, one step back52 client = httpx.AsyncClient(app=echo_body)TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 53–59why, one step back53 response = await client.post("http://www.example.org/", data=b"example")54 assert response.status_code == 20055 assert response.text == "example"565758@pytest.mark.asyncio59async def test_asgi_exc():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 60why, one step back60 client = httpx.AsyncClient(app=raise_exc)TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 61–66why, one step back61 with pytest.raises(ValueError):62 await client.get("http://www.example.org/")636465@pytest.mark.asyncio66async def test_asgi_exc_after_response():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 67why, one step back67 client = httpx.AsyncClient(app=raise_exc_after_response)TimeoutConfig -> Timeout (#591)Tom Christiebefore adoptionc033ed1
lines 68–69why, one step back68 with pytest.raises(ValueError):69 await client.get("http://www.example.org/")asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 70–71why, one step back7071ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 72why, one step back72async def test_asgi_disconnect_after_response_complete(async_environment):asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 73–101why, one step back73 disconnect = False7475 async def read_body(scope, receive, send):76 nonlocal disconnect7778 status = 20079 headers = [(b"content-type", "text/plain")]8081 await send(82 {"type": "http.response.start", "status": status, "headers": headers}83 )84 more_body = True85 while more_body:86 message = await receive()87 more_body = message.get("more_body", False)8889 await send({"type": "http.response.body", "body": b"", "more_body": False})9091 # The ASGI spec says of the Disconnect message:92 # "Sent to the application when a HTTP connection is closed or if receive is93 # called after a response has been sent."94 # So if receive() is called again, the disconnect message should be received95 message = await receive()96 disconnect = message.get("type") == "http.disconnect"9798 client = httpx.AsyncClient(app=read_body)99 response = await client.post("http://www.example.org/", data=b"example")100 assert response.status_code == 200101 assert disconnect