Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

114 lines · 3.7 KiB · Python

Last changed by

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

History of this file

Take it

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 1–2why, one step back
1from functools import partial
2

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 3–29why, 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})151617async def echo_body(scope, receive, send):18 status = 20019 headers = [(b"content-type", "text/plain")]2021 await send({"type": "http.response.start", "status": status, "headers": headers})22 more_body = True23 while more_body:24 message = await receive()25 body = message.get("body", b"")26 more_body = message.get("more_body", False)27 await send({"type": "http.response.body", "body": body, "more_body": more_body})2829

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 30–31why, one step back
30async def raise_exc(scope, receive, send, exc=ValueError):
31 raise exc()

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 32–43why, one step back
32
3334async def raise_exc_after_response(scope, receive, send):35 status = 20036 output = b"Hello, World!"37 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]3839 await send({"type": "http.response.start", "status": status, "headers": headers})40 await send({"type": "http.response.body", "body": output})41 raise ValueError()4243

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

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

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

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

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

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

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 49–52why, one step back
49 assert response.status_code == 200
50 assert response.text == "Hello, World!"5152

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

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

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

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

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

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

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 58–61why, one step back
58 assert response.status_code == 200
59 assert response.text == "example"6061

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

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

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

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

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

lines 67–68why, one step back
67
68

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

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

Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067

line 70why, one step back
70async def test_asgi_http_error():

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

lines 71–73why, one step back
71 async with httpx.AsyncClient(app=partial(raise_exc, exc=RuntimeError)) as client:
72 with pytest.raises(RuntimeError):73 await client.get("http://www.example.org/")

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 74–75why, one step back
74
75

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

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

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

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

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

lines 78–80why, one step back
78 async with httpx.AsyncClient(app=raise_exc_after_response) as client:
79 with pytest.raises(ValueError):80 await client.get("http://www.example.org/")

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

lines 81–82why, one step back
81
82

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

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

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

lines 85–109why, one step back
85 disconnect = False
8687 async def read_body(scope, receive, send):88 nonlocal disconnect8990 status = 20091 headers = [(b"content-type", "text/plain")]9293 await send(94 {"type": "http.response.start", "status": status, "headers": headers}95 )96 more_body = True97 while more_body:98 message = await receive()99 more_body = message.get("more_body", False)100101 await send({"type": "http.response.body", "body": b"", "more_body": False})102103 # The ASGI spec says of the Disconnect message:104 # "Sent to the application when a HTTP connection is closed or if receive is105 # called after a response has been sent."106 # So if receive() is called again, the disconnect message should be received107 message = await receive()108 disconnect = message.get("type") == "http.disconnect"109

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

lines 110–112why, one step back
110 async with httpx.AsyncClient(app=read_body) as client:
111 response = await client.post("http://www.example.org/", data=b"example")112

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

lines 113–114why, one step back
113 assert response.status_code == 200
114 assert disconnect