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 fileTake it
1import json23import 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_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})242526async 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})373839async def raise_exc(scope, receive, send):40 raise RuntimeError()414243async 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})50 raise RuntimeError()515253@pytest.mark.usefixtures("async_environment")54async def test_asgi():55 async with httpx.AsyncClient(app=hello_world) as client:56 response = await client.get("http://www.example.org/")5758 assert response.status_code == 20059 assert response.text == "Hello, World!"606162@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"}707172@pytest.mark.usefixtures("async_environment")73async def test_asgi_upload():74 async with httpx.AsyncClient(app=echo_body) as client:75 response = await client.post("http://www.example.org/", content=b"example")7677 assert response.status_code == 20078 assert response.text == "example"798081@pytest.mark.usefixtures("async_environment")82async def test_asgi_exc():83 async with httpx.AsyncClient(app=raise_exc) as client:84 with pytest.raises(RuntimeError):85 await client.get("http://www.example.org/")868788@pytest.mark.usefixtures("async_environment")89async def test_asgi_exc_after_response():90 async with httpx.AsyncClient(app=raise_exc_after_response) as client:91 with pytest.raises(RuntimeError):92 await client.get("http://www.example.org/")939495@pytest.mark.usefixtures("async_environment")96async def test_asgi_disconnect_after_response_complete():97 disconnect = False9899 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"121122 async with httpx.AsyncClient(app=read_body) as client:123 response = await client.post("http://www.example.org/", content=b"example")124125 assert response.status_code == 200126 assert disconnect