test_asgi.py
This file
Path
tests/test_asgi.py
126 lines · 4.2 KiB · Python
Last changed by
Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995
History of this fileTake it
The walk stopped after 200 commits: lines older than 3046e92 point at it.
Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995
lines 1–2why, one step back1import json2Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 3–16why, 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})1516Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995
lines 17–25why, one step back17async def echo_path(scope, receive, send):18 status = 20019 output = json.dumps({"path": scope["path"]}).encode("utf-8")20 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]2122 await send({"type": "http.response.start", "status": status, "headers": headers})23 await send({"type": "http.response.body", "body": output})2425Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 26–38why, one step back26async def echo_body(scope, receive, send):27 status = 20028 headers = [(b"content-type", "text/plain")]2930 await send({"type": "http.response.start", "status": status, "headers": headers})31 more_body = True32 while more_body:33 message = await receive()34 body = message.get("body", b"")35 more_body = message.get("more_body", False)36 await send({"type": "http.response.body", "body": body, "more_body": more_body})3738Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703
lines 39–40why, one step back39async def raise_exc(scope, receive, send):40 raise RuntimeError()Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 41–49why, one step back414243async def raise_exc_after_response(scope, receive, send):44 status = 20045 output = b"Hello, World!"46 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]4748 await send({"type": "http.response.start", "status": status, "headers": headers})49 await send({"type": "http.response.body", "body": output})Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703
line 50why, one step back50 raise RuntimeError()Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 51–52why, one step back5152Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 53why, one step back53@pytest.mark.usefixtures("async_environment")Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 54why, one step back54async def test_asgi():Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 55–57why, one step back55 async with httpx.AsyncClient(app=hello_world) as client:56 response = await client.get("http://www.example.org/")57Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 58–59why, one step back58 assert response.status_code == 20059 assert response.text == "Hello, World!"Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995
lines 60–69why, one step back606162@pytest.mark.usefixtures("async_environment")63async def test_asgi_urlencoded_path():64 async with httpx.AsyncClient(app=echo_path) as client:65 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")66 response = await client.get(url)6768 assert response.status_code == 20069 assert response.json() == {"path": "/user@example.org"}Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 70–71why, one step back7071Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 72why, one step back72@pytest.mark.usefixtures("async_environment")Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 73why, one step back73async def test_asgi_upload():Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 74why, one step back74 async with httpx.AsyncClient(app=echo_body) as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 75why, one step back75 response = await client.post("http://www.example.org/", content=b"example")Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 76why, one step back76Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 77–80why, one step back77 assert response.status_code == 20078 assert response.text == "example"7980Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 81why, one step back81@pytest.mark.usefixtures("async_environment")Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 82why, one step back82async def test_asgi_exc():Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
lines 83–85why, one step back83 async with httpx.AsyncClient(app=raise_exc) as client:84 with pytest.raises(RuntimeError):85 await client.get("http://www.example.org/")Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 86–87why, one step back8687Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
line 88why, one step back88@pytest.mark.usefixtures("async_environment")Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 89why, one step back89async def test_asgi_exc_after_response():Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 90why, one step back90 async with httpx.AsyncClient(app=raise_exc_after_response) as client:Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703
line 91why, one step back91 with pytest.raises(RuntimeError):Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 92why, one step back92 await client.get("http://www.example.org/")asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 93–94why, one step back9394Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
lines 95–96why, one step back95@pytest.mark.usefixtures("async_environment")96async def test_asgi_disconnect_after_response_complete():asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 97–121why, one step back97 disconnect = False9899 async def read_body(scope, receive, send):100 nonlocal disconnect101102 status = 200103 headers = [(b"content-type", "text/plain")]104105 await send(106 {"type": "http.response.start", "status": status, "headers": headers}107 )108 more_body = True109 while more_body:110 message = await receive()111 more_body = message.get("more_body", False)112113 await send({"type": "http.response.body", "body": b"", "more_body": False})114115 # The ASGI spec says of the Disconnect message:116 # "Sent to the application when a HTTP connection is closed or if receive is117 # called after a response has been sent."118 # So if receive() is called again, the disconnect message should be received119 message = await receive()120 disconnect = message.get("type") == "http.disconnect"121Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 122why, one step back122 async with httpx.AsyncClient(app=read_body) as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 123why, one step back123 response = await client.post("http://www.example.org/", content=b"example")Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b
line 124why, one step back124asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 125–126why, one step back125 assert response.status_code == 200126 assert disconnect