Skip to content
Myr

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 file

Take it

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

Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995

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

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

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

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

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

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

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

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 41–49why, one step back
41
4243async 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 back
50 raise RuntimeError()

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 51–52why, one step back
51
52

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

line 53why, one step back
53@pytest.mark.usefixtures("async_environment")

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

line 54why, one step back
54async def test_asgi():

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

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

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

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

Fix ASGITransport path escaping (#1307)Tom Christiebefore adoptione53f995

lines 60–69why, one step back
60
6162@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"}

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 70–71why, one step back
70
71

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

line 72why, one step back
72@pytest.mark.usefixtures("async_environment")

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

line 73why, one step back
73async def test_asgi_upload():

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

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

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

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

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

line 76why, one step back
76

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 77–80why, one step back
77 assert response.status_code == 200
78 assert response.text == "example"7980

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

line 81why, one step back
81@pytest.mark.usefixtures("async_environment")

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

line 82why, one step back
82async def test_asgi_exc():

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

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

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 86–87why, one step back
86
87

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

line 88why, one step back
88@pytest.mark.usefixtures("async_environment")

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

line 89why, one step back
89async def test_asgi_exc_after_response():

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

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

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

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

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

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

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 93–94why, one step back
93
94

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 95–96why, one step back
95@pytest.mark.usefixtures("async_environment")
96async def test_asgi_disconnect_after_response_complete():

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 97–121why, one step back
97 disconnect = False
9899 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"121

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

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

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

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

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

line 124why, one step back
124

Version 0.13.1 (#981)Tom Christiebefore adoptionebc9c29

lines 125–126why, one step back
125 assert response.status_code == 200
126 assert disconnect