test_asgi.py
This file
Path
tests/test_asgi.py
110 lines · 3.5 KiB · Python
Last changed by
Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
History of this fileTake it
The walk stopped after 200 commits: lines older than c38fd68 point at it.
Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 1–2why, one step back1from functools import partial2Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
lines 3–29why, 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})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})2829Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 30–31why, one step back30async def raise_exc(scope, receive, send, exc=ValueError):31 raise exc()Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
lines 32–45why, one step back323334async 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()424344@pytest.mark.asyncio45async def test_asgi():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 46why, one step back46 client = httpx.AsyncClient(app=hello_world)Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
lines 47–53why, one step back47 response = await client.get("http://www.example.org/")48 assert response.status_code == 20049 assert response.text == "Hello, World!"505152@pytest.mark.asyncio53async def test_asgi_upload():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 54why, one step back54 client = httpx.AsyncClient(app=echo_body)Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
lines 55–61why, one step back55 response = await client.post("http://www.example.org/", data=b"example")56 assert response.status_code == 20057 assert response.text == "example"585960@pytest.mark.asyncio61async def test_asgi_exc():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 62why, one step back62 client = httpx.AsyncClient(app=raise_exc)Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
line 63why, one step back63 with pytest.raises(ValueError):Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 64–70why, one step back64 await client.get("http://www.example.org/")656667@pytest.mark.asyncio68async def test_asgi_http_error():69 client = httpx.AsyncClient(app=partial(raise_exc, exc=httpx.HTTPError))70 with pytest.raises(httpx.HTTPError):Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
lines 71–75why, one step back71 await client.get("http://www.example.org/")727374@pytest.mark.asyncio75async def test_asgi_exc_after_response():Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 76why, one step back76 client = httpx.AsyncClient(app=raise_exc_after_response)Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68
lines 77–78why, one step back77 with pytest.raises(ValueError):78 await client.get("http://www.example.org/")asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 79–80why, one step back7980ASGI: Wait for response to complete before sending disconnect message (#919)Jamie Hewlandbefore adoptiond568ecd
line 81why, one step back81async def test_asgi_disconnect_after_response_complete(async_environment):asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 82–110why, one step back82 disconnect = False8384 async def read_body(scope, receive, send):85 nonlocal disconnect8687 status = 20088 headers = [(b"content-type", "text/plain")]8990 await send(91 {"type": "http.response.start", "status": status, "headers": headers}92 )93 more_body = True94 while more_body:95 message = await receive()96 more_body = message.get("more_body", False)9798 await send({"type": "http.response.body", "body": b"", "more_body": False})99100 # The ASGI spec says of the Disconnect message:101 # "Sent to the application when a HTTP connection is closed or if receive is102 # called after a response has been sent."103 # So if receive() is called again, the disconnect message should be received104 message = await receive()105 disconnect = message.get("type") == "http.disconnect"106107 client = httpx.AsyncClient(app=read_body)108 response = await client.post("http://www.example.org/", data=b"example")109 assert response.status_code == 200110 assert disconnect