test_asgi.py
This file
Path
tests/test_asgi.py
91 lines · 2.8 KiB · Python
Last changed by
Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
History of this fileTake it
1import pytest23import 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()404142def test_asgi():43 client = httpx.Client(app=hello_world)44 response = client.get("http://www.example.org/")45 assert response.status_code == 20046 assert response.text == "Hello, World!"474849async def test_asgi_async(backend):50 client = httpx.AsyncClient(app=hello_world, backend=backend)51 response = await client.get("http://www.example.org/")52 assert response.status_code == 20053 assert response.text == "Hello, World!"545556def test_asgi_upload():57 client = httpx.Client(app=echo_body)58 response = client.post("http://www.example.org/", data=b"example")59 assert response.status_code == 20060 assert response.text == "example"616263async def test_asgi_upload_async(backend):64 client = httpx.AsyncClient(app=echo_body, backend=backend)65 response = await client.post("http://www.example.org/", data=b"example")66 assert response.status_code == 20067 assert response.text == "example"686970def test_asgi_exc():71 client = httpx.Client(app=raise_exc)72 with pytest.raises(ValueError):73 client.get("http://www.example.org/")747576async def test_asgi_exc_async(backend):77 client = httpx.AsyncClient(app=raise_exc, backend=backend)78 with pytest.raises(ValueError):79 await client.get("http://www.example.org/")808182def test_asgi_exc_after_response():83 client = httpx.Client(app=raise_exc_after_response)84 with pytest.raises(ValueError):85 client.get("http://www.example.org/")868788async def test_asgi_exc_after_response_async(backend):89 client = httpx.AsyncClient(app=raise_exc_after_response, backend=backend)90 with pytest.raises(ValueError):91 await client.get("http://www.example.org/")