Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

209 lines · 7.0 KiB · Python

Last changed by

Transport API (#1522)Tom Christiebefore adoption1a6e254

History of this file

Take it

The walk stopped after 200 commits: lines older than aad8209 point at it.

Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995

lines 1–2why, one step back
1import json
2

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 3–16why, one step back
3import pytest
45import 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})1516

Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995

lines 17–25why, one step back
17async 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})2425

Add raw_path to scope in ASGITransport (#1357)Simon Willisonbefore adoptionca5f524

lines 26–34why, one step back
26async 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})3334

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 35–47why, one step back
35async 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})4647

Force lowercase ASGI headers (#1351)Tom Christiebefore adoption0123bca

lines 48–58why, one step back
48async 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})5758

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

lines 59–60why, one step back
59async def raise_exc(scope, receive, send):
60 raise RuntimeError()

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 61–69why, one step back
61
6263async 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})

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

line 70why, one step back
70 raise RuntimeError()

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 71–72why, one step back
71
72

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 73–108why, one step back
73async def empty_stream():
74 yield b""757677@pytest.mark.usefixtures("async_environment")78async def test_asgi_transport():79 async with httpx.ASGITransport(app=hello_world) as transport:80 status_code, headers, stream, ext = await transport.handle_async_request(81 method=b"GET",82 url=(b"http", b"www.example.org", 80, b"/"),83 headers=[(b"Host", b"www.example.org")],84 stream=empty_stream(),85 extensions={},86 )87 body = b"".join([part async for part in stream])8889 assert status_code == 20090 assert body == b"Hello, World!"919293@pytest.mark.usefixtures("async_environment")94async def test_asgi_transport_no_body():95 async with httpx.ASGITransport(app=echo_body) as transport:96 status_code, headers, stream, ext = await transport.handle_async_request(97 method=b"GET",98 url=(b"http", b"www.example.org", 80, b"/"),99 headers=[(b"Host", b"www.example.org")],100 stream=empty_stream(),101 extensions={},102 )103 body = b"".join([part async for part in stream])104105 assert status_code == 200106 assert body == b""107108

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 109–110why, one step back
109@pytest.mark.usefixtures("async_environment")
110async def test_asgi():

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 111–113why, one step back
111 async with httpx.AsyncClient(app=hello_world) as client:
112 response = await client.get("http://www.example.org/")113

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 114–115why, one step back
114 assert response.status_code == 200
115 assert response.text == "Hello, World!"

Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995

lines 116–125why, one step back
116
117118@pytest.mark.usefixtures("async_environment")119async def test_asgi_urlencoded_path():120 async with httpx.AsyncClient(app=echo_path) as client:121 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")122 response = await client.get(url)123124 assert response.status_code == 200125 assert response.json() == {"path": "/user@example.org"}

Add raw_path to scope in ASGITransport (#1357)Simon Willisonbefore adoptionca5f524

lines 126–135why, one step back
126
127128@pytest.mark.usefixtures("async_environment")129async def test_asgi_raw_path():130 async with httpx.AsyncClient(app=echo_raw_path) as client:131 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")132 response = await client.get(url)133134 assert response.status_code == 200135 assert response.json() == {"raw_path": "/user%40example.org"}

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 136–139why, one step back
136
137138@pytest.mark.usefixtures("async_environment")139async def test_asgi_upload():

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 140why, one step back
140 async with httpx.AsyncClient(app=echo_body) as client:

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 141why, one step back
141 response = await client.post("http://www.example.org/", content=b"example")

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 142why, one step back
142

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 143–144why, one step back
143 assert response.status_code == 200
144 assert response.text == "example"

Force lowercase ASGI headers (#1351)Tom Christiebefore adoption0123bca

lines 145–161why, one step back
145
146147@pytest.mark.usefixtures("async_environment")148async def test_asgi_headers():149 async with httpx.AsyncClient(app=echo_headers) as client:150 response = await client.get("http://www.example.org/")151152 assert response.status_code == 200153 assert response.json() == {154 "headers": [155 ["host", "www.example.org"],156 ["accept", "*/*"],157 ["accept-encoding", "gzip, deflate, br"],158 ["connection", "keep-alive"],159 ["user-agent", f"python-httpx/{httpx.__version__}"],160 ]161 }

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 162–165why, one step back
162
163164@pytest.mark.usefixtures("async_environment")165async def test_asgi_exc():

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 166–168why, one step back
166 async with httpx.AsyncClient(app=raise_exc) as client:
167 with pytest.raises(RuntimeError):168 await client.get("http://www.example.org/")

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 169–172why, one step back
169
170171@pytest.mark.usefixtures("async_environment")172async def test_asgi_exc_after_response():

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 173why, one step back
173 async with httpx.AsyncClient(app=raise_exc_after_response) as client:

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

line 174why, one step back
174 with pytest.raises(RuntimeError):

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 175why, one step back
175 await client.get("http://www.example.org/")

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 176–204why, one step back
176
177178@pytest.mark.usefixtures("async_environment")179async def test_asgi_disconnect_after_response_complete():180 disconnect = False181182 async def read_body(scope, receive, send):183 nonlocal disconnect184185 status = 200186 headers = [(b"content-type", "text/plain")]187188 await send(189 {"type": "http.response.start", "status": status, "headers": headers}190 )191 more_body = True192 while more_body:193 message = await receive()194 more_body = message.get("more_body", False)195196 await send({"type": "http.response.body", "body": b"", "more_body": False})197198 # The ASGI spec says of the Disconnect message:199 # "Sent to the application when a HTTP connection is closed or if receive is200 # called after a response has been sent."201 # So if receive() is called again, the disconnect message should be received202 message = await receive()203 disconnect = message.get("type") == "http.disconnect"204

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 205why, one step back
205 async with httpx.AsyncClient(app=read_body) as client:

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 206why, one step back
206 response = await client.post("http://www.example.org/", content=b"example")

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 207why, one step back
207

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 208–209why, one step back
208 assert response.status_code == 200
209 assert disconnect