Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

105 lines · 3.4 KiB · Python

Last changed by

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

History of this file

Take it

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 1–27why, one step back
1import pytest
23import httpx456async def hello_world(scope, receive, send):7 status = 2008 output = b"Hello, World!"9 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]1011 await send({"type": "http.response.start", "status": status, "headers": headers})12 await send({"type": "http.response.body", "body": output})131415async def echo_body(scope, receive, send):16 status = 20017 headers = [(b"content-type", "text/plain")]1819 await send({"type": "http.response.start", "status": status, "headers": headers})20 more_body = True21 while more_body:22 message = await receive()23 body = message.get("body", b"")24 more_body = message.get("more_body", False)25 await send({"type": "http.response.body", "body": body, "more_body": more_body})2627

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 30–38why, one step back
30
3132async def raise_exc_after_response(scope, receive, send):33 status = 20034 output = b"Hello, World!"35 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]3637 await send({"type": "http.response.start", "status": status, "headers": headers})38 await send({"type": "http.response.body", "body": output})

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 40–41why, one step back
40
41

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

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

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

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

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 47–50why, one step back
47 assert response.status_code == 200
48 assert response.text == "Hello, World!"4950

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

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

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

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

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

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

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

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

line 55why, one step back
55

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 56–59why, one step back
56 assert response.status_code == 200
57 assert response.text == "example"5859

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

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

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

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

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 65–66why, one step back
65
66

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

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

Httpcore interface (#804)Tom Christiebefore adoption3046e92

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

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

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

Minor test refactoring (#1242)Tom Christiebefore adoptioncf59703

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

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

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

asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079

lines 72–73why, one step back
72
73

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

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

asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079

lines 76–100why, one step back
76 disconnect = False
7778 async def read_body(scope, receive, send):79 nonlocal disconnect8081 status = 20082 headers = [(b"content-type", "text/plain")]8384 await send(85 {"type": "http.response.start", "status": status, "headers": headers}86 )87 more_body = True88 while more_body:89 message = await receive()90 more_body = message.get("more_body", False)9192 await send({"type": "http.response.body", "body": b"", "more_body": False})9394 # The ASGI spec says of the Disconnect message:95 # "Sent to the application when a HTTP connection is closed or if receive is96 # called after a response has been sent."97 # So if receive() is called again, the disconnect message should be received98 message = await receive()99 disconnect = message.get("type") == "http.disconnect"100

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

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

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

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

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

line 103why, one step back
103

asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079

lines 104–105why, one step back
104 assert response.status_code == 200
105 assert disconnect