Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

102 lines · 3.2 KiB · Python

Last changed by

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

History of this file

Take it

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

Stringify the given file name (#545)Taoufikbefore adoption1c326d5

lines 1–41why, 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})262728async def raise_exc(scope, receive, send):29 raise ValueError()303132async 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})39 raise ValueError()4041

Drop sync (#544)Tom Christiebefore adoption206c537

lines 42–43why, one step back
42@pytest.mark.asyncio
43async def test_asgi():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 44why, one step back
44 client = httpx.AsyncClient(app=hello_world)

Stringify the given file name (#545)Taoufikbefore adoption1c326d5

lines 45–49why, one step back
45 response = await client.get("http://www.example.org/")
46 assert response.status_code == 20047 assert response.text == "Hello, World!"4849

Drop sync (#544)Tom Christiebefore adoption206c537

lines 50–51why, one step back
50@pytest.mark.asyncio
51async def test_asgi_upload():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 52why, one step back
52 client = httpx.AsyncClient(app=echo_body)

Stringify the given file name (#545)Taoufikbefore adoption1c326d5

lines 53–57why, one step back
53 response = await client.post("http://www.example.org/", data=b"example")
54 assert response.status_code == 20055 assert response.text == "example"5657

Drop sync (#544)Tom Christiebefore adoption206c537

lines 58–59why, one step back
58@pytest.mark.asyncio
59async def test_asgi_exc():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 60why, one step back
60 client = httpx.AsyncClient(app=raise_exc)

Stringify the given file name (#545)Taoufikbefore adoption1c326d5

lines 61–64why, one step back
61 with pytest.raises(ValueError):
62 await client.get("http://www.example.org/")6364

Drop sync (#544)Tom Christiebefore adoption206c537

lines 65–66why, one step back
65@pytest.mark.asyncio
66async def test_asgi_exc_after_response():

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 67why, one step back
67 client = httpx.AsyncClient(app=raise_exc_after_response)

Stringify the given file name (#545)Taoufikbefore adoption1c326d5

lines 68–69why, one step back
68 with pytest.raises(ValueError):
69 await client.get("http://www.example.org/")

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

lines 70–102why, one step back
70
7172@pytest.mark.asyncio73async def test_asgi_disconnect_after_response_complete():74 disconnect = False7576 async def read_body(scope, receive, send):77 nonlocal disconnect7879 status = 20080 headers = [(b"content-type", "text/plain")]8182 await send(83 {"type": "http.response.start", "status": status, "headers": headers}84 )85 more_body = True86 while more_body:87 message = await receive()88 more_body = message.get("more_body", False)8990 await send({"type": "http.response.body", "body": b"", "more_body": False})9192 # The ASGI spec says of the Disconnect message:93 # "Sent to the application when a HTTP connection is closed or if receive is94 # called after a response has been sent."95 # So if receive() is called again, the disconnect message should be received96 message = await receive()97 disconnect = message.get("type") == "http.disconnect"9899 client = httpx.AsyncClient(app=read_body)100 response = await client.post("http://www.example.org/", data=b"example")101 assert response.status_code == 200102 assert disconnect