test_asgi.py
This file
Path
tests/test_asgi.py
111 lines · 3.6 KiB · Python
Last changed by
Exception hierarchy (#1095)Tom Christiebefore adoption9409900
History of this fileTake it
The walk stopped after 200 commits: lines older than 2c4a1dd point at it.
Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 1–2why, one step back1from functools import partial2Drop OpenConnection (#700)Tom Christiebefore adoption2c4a1dd
lines 3–29why, one step back3import pytest45import httpx678async def hello_world(scope, receive, send):9 status = 20010 output = b"Hello, World!"11 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]1213 await send({"type": "http.response.start", "status": status, "headers": headers})14 await send({"type": "http.response.body", "body": output})151617async def echo_body(scope, receive, send):18 status = 20019 headers = [(b"content-type", "text/plain")]2021 await send({"type": "http.response.start", "status": status, "headers": headers})22 more_body = True23 while more_body:24 message = await receive()25 body = message.get("body", b"")26 more_body = message.get("more_body", False)27 await send({"type": "http.response.body", "body": body, "more_body": more_body})2829Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 30–31why, one step back30async def raise_exc(scope, receive, send, exc=ValueError):31 raise exc()Drop OpenConnection (#700)Tom Christiebefore adoption2c4a1dd
lines 32–43why, one step back323334async def raise_exc_after_response(scope, receive, send):35 status = 20036 output = b"Hello, World!"37 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]3839 await send({"type": "http.response.start", "status": status, "headers": headers})40 await send({"type": "http.response.body", "body": output})41 raise ValueError()4243Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 44why, one step back44@pytest.mark.usefixtures("async_environment")Drop OpenConnection (#700)Tom Christiebefore adoption2c4a1dd
lines 45–51why, one step back45async def test_asgi():46 client = httpx.AsyncClient(app=hello_world)47 response = await client.get("http://www.example.org/")48 assert response.status_code == 20049 assert response.text == "Hello, World!"5051Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 52why, one step back52@pytest.mark.usefixtures("async_environment")Drop OpenConnection (#700)Tom Christiebefore adoption2c4a1dd
lines 53–59why, one step back53async def test_asgi_upload():54 client = httpx.AsyncClient(app=echo_body)55 response = await client.post("http://www.example.org/", data=b"example")56 assert response.status_code == 20057 assert response.text == "example"5859Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 60why, one step back60@pytest.mark.usefixtures("async_environment")Drop OpenConnection (#700)Tom Christiebefore adoption2c4a1dd
lines 61–63why, one step back61async def test_asgi_exc():62 client = httpx.AsyncClient(app=raise_exc)63 with pytest.raises(ValueError):Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 64–66why, one step back64 await client.get("http://www.example.org/")6566Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 67why, one step back67@pytest.mark.usefixtures("async_environment")Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 68why, one step back68async def test_asgi_http_error():Exception hierarchy (#1095)Tom Christiebefore adoption9409900
lines 69–70why, one step back69 client = httpx.AsyncClient(app=partial(raise_exc, exc=RuntimeError))70 with pytest.raises(RuntimeError):Drop OpenConnection (#700)Tom Christiebefore adoption2c4a1dd
lines 71–73why, one step back71 await client.get("http://www.example.org/")7273Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 74why, one step back74@pytest.mark.usefixtures("async_environment")Drop OpenConnection (#700)Tom Christiebefore adoption2c4a1dd
lines 75–78why, one step back75async def test_asgi_exc_after_response():76 client = httpx.AsyncClient(app=raise_exc_after_response)77 with pytest.raises(ValueError):78 await client.get("http://www.example.org/")asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 79–80why, one step back7980Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
lines 81–82why, one step back81@pytest.mark.usefixtures("async_environment")82async def test_asgi_disconnect_after_response_complete():asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 83–111why, one step back83 disconnect = False8485 async def read_body(scope, receive, send):86 nonlocal disconnect8788 status = 20089 headers = [(b"content-type", "text/plain")]9091 await send(92 {"type": "http.response.start", "status": status, "headers": headers}93 )94 more_body = True95 while more_body:96 message = await receive()97 more_body = message.get("more_body", False)9899 await send({"type": "http.response.body", "body": b"", "more_body": False})100101 # The ASGI spec says of the Disconnect message:102 # "Sent to the application when a HTTP connection is closed or if receive is103 # called after a response has been sent."104 # So if receive() is called again, the disconnect message should be received105 message = await receive()106 disconnect = message.get("type") == "http.disconnect"107108 client = httpx.AsyncClient(app=read_body)109 response = await client.post("http://www.example.org/", data=b"example")110 assert response.status_code == 200111 assert disconnect