test_asgi.py
This file
Path
tests/test_asgi.py
193 lines · 6.7 KiB · Python
Last changed by
Transport API as plain `request -> response` method. (#1840)Tom Christiebefore adoptionff9813e
History of this fileTake it
The walk stopped after 200 commits: lines older than 19b863a point at it.
Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995
lines 1–2why, one step back1import json2Header refinements (#1248)Tom Christiebefore adoption19b863a
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})2425Add raw_path to scope in ASGITransport (#1357)Simon Willisonbefore adoptionca5f524
lines 26–34why, one step back26async def echo_raw_path(scope, receive, send):27 status = 20028 output = json.dumps({"raw_path": scope["raw_path"].decode("ascii")}).encode("utf-8")29 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]3031 await send({"type": "http.response.start", "status": status, "headers": headers})32 await send({"type": "http.response.body", "body": output})3334Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 35–47why, one step back35async def echo_body(scope, receive, send):36 status = 20037 headers = [(b"content-type", "text/plain")]3839 await send({"type": "http.response.start", "status": status, "headers": headers})40 more_body = True41 while more_body:42 message = await receive()43 body = message.get("body", b"")44 more_body = message.get("more_body", False)45 await send({"type": "http.response.body", "body": body, "more_body": more_body})4647Force lowercase ASGI headers (#1351)Tom Christiebefore adoption0123bca
lines 48–58why, one step back48async def echo_headers(scope, receive, send):49 status = 20050 output = json.dumps(51 {"headers": [[k.decode(), v.decode()] for k, v in scope["headers"]]}52 ).encode("utf-8")53 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]5455 await send({"type": "http.response.start", "status": status, "headers": headers})56 await send({"type": "http.response.body", "body": output})5758Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 59–72why, one step back59async def raise_exc(scope, receive, send):60 raise RuntimeError()616263async def raise_exc_after_response(scope, receive, send):64 status = 20065 output = b"Hello, World!"66 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]6768 await send({"type": "http.response.start", "status": status, "headers": headers})69 await send({"type": "http.response.body", "body": output})70 raise RuntimeError()7172Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 73–75why, one step back73@pytest.mark.usefixtures("async_environment")74async def test_asgi_transport():75 async with httpx.ASGITransport(app=hello_world) as transport:Transport API as plain `request -> response` method. (#1840)Tom Christiebefore adoptionff9813e
lines 76–80why, one step back76 request = httpx.Request("GET", "http://www.example.com/")77 response = await transport.handle_async_request(request)78 await response.aread()79 assert response.status_code == 20080 assert response.content == b"Hello, World!"Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 81–85why, one step back818283@pytest.mark.usefixtures("async_environment")84async def test_asgi_transport_no_body():85 async with httpx.ASGITransport(app=echo_body) as transport:Transport API as plain `request -> response` method. (#1840)Tom Christiebefore adoptionff9813e
lines 86–90why, one step back86 request = httpx.Request("GET", "http://www.example.com/")87 response = await transport.handle_async_request(request)88 await response.aread()89 assert response.status_code == 20090 assert response.content == b""Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 91–92why, one step back9192Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 93–99why, one step back93@pytest.mark.usefixtures("async_environment")94async def test_asgi():95 async with httpx.AsyncClient(app=hello_world) as client:96 response = await client.get("http://www.example.org/")9798 assert response.status_code == 20099 assert response.text == "Hello, World!"Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995
lines 100–109why, one step back100101102@pytest.mark.usefixtures("async_environment")103async def test_asgi_urlencoded_path():104 async with httpx.AsyncClient(app=echo_path) as client:105 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")106 response = await client.get(url)107108 assert response.status_code == 200109 assert response.json() == {"path": "/user@example.org"}Add raw_path to scope in ASGITransport (#1357)Simon Willisonbefore adoptionca5f524
lines 110–119why, one step back110111112@pytest.mark.usefixtures("async_environment")113async def test_asgi_raw_path():114 async with httpx.AsyncClient(app=echo_raw_path) as client:115 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")116 response = await client.get(url)117118 assert response.status_code == 200119 assert response.json() == {"raw_path": "/user%40example.org"}Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 120–124why, one step back120121122@pytest.mark.usefixtures("async_environment")123async def test_asgi_upload():124 async with httpx.AsyncClient(app=echo_body) as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 125why, one step back125 response = await client.post("http://www.example.org/", content=b"example")Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 126–128why, one step back126127 assert response.status_code == 200128 assert response.text == "example"Force lowercase ASGI headers (#1351)Tom Christiebefore adoption0123bca
lines 129–145why, one step back129130131@pytest.mark.usefixtures("async_environment")132async def test_asgi_headers():133 async with httpx.AsyncClient(app=echo_headers) as client:134 response = await client.get("http://www.example.org/")135136 assert response.status_code == 200137 assert response.json() == {138 "headers": [139 ["host", "www.example.org"],140 ["accept", "*/*"],141 ["accept-encoding", "gzip, deflate, br"],142 ["connection", "keep-alive"],143 ["user-agent", f"python-httpx/{httpx.__version__}"],144 ]145 }Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 146–189why, one step back146147148@pytest.mark.usefixtures("async_environment")149async def test_asgi_exc():150 async with httpx.AsyncClient(app=raise_exc) as client:151 with pytest.raises(RuntimeError):152 await client.get("http://www.example.org/")153154155@pytest.mark.usefixtures("async_environment")156async def test_asgi_exc_after_response():157 async with httpx.AsyncClient(app=raise_exc_after_response) as client:158 with pytest.raises(RuntimeError):159 await client.get("http://www.example.org/")160161162@pytest.mark.usefixtures("async_environment")163async def test_asgi_disconnect_after_response_complete():164 disconnect = False165166 async def read_body(scope, receive, send):167 nonlocal disconnect168169 status = 200170 headers = [(b"content-type", "text/plain")]171172 await send(173 {"type": "http.response.start", "status": status, "headers": headers}174 )175 more_body = True176 while more_body:177 message = await receive()178 more_body = message.get("more_body", False)179180 await send({"type": "http.response.body", "body": b"", "more_body": False})181182 # The ASGI spec says of the Disconnect message:183 # "Sent to the application when a HTTP connection is closed or if receive is184 # called after a response has been sent."185 # So if receive() is called again, the disconnect message should be received186 message = await receive()187 disconnect = message.get("type") == "http.disconnect"188189 async with httpx.AsyncClient(app=read_body) as client:Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 190why, one step back190 response = await client.post("http://www.example.org/", content=b"example")Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 191–193why, one step back191192 assert response.status_code == 200193 assert disconnect