Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

95 lines · 2.8 KiB · Python

Last changed by

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

History of this file

Take it

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 1–2why, one step back
1import pytest
2

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 3why, one step back
3import httpx

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 4–42why, one step back
4
56async 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():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 43why, one step back
43 client = httpx.Client(app=hello_world)

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 44–48why, one step back
44 response = client.get("http://www.example.org/")
45 assert response.status_code == 20046 assert response.text == "Hello, World!"4748

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 49–56why, one step back
49@pytest.mark.asyncio
50async def test_asgi_async():51 client = httpx.AsyncClient(app=hello_world)52 response = await client.get("http://www.example.org/")53 assert response.status_code == 20054 assert response.text == "Hello, World!"5556

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

line 57why, one step back
57def test_asgi_upload():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 58why, one step back
58 client = httpx.Client(app=echo_body)

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 59–63why, one step back
59 response = client.post("http://www.example.org/", data=b"example")
60 assert response.status_code == 20061 assert response.text == "example"6263

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 64–71why, one step back
64@pytest.mark.asyncio
65async def test_asgi_upload_async():66 client = httpx.AsyncClient(app=echo_body)67 response = await client.post("http://www.example.org/", data=b"example")68 assert response.status_code == 20069 assert response.text == "example"7071

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

line 72why, one step back
72def test_asgi_exc():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 73why, one step back
73 client = httpx.Client(app=raise_exc)

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

line 74why, one step back
74 with pytest.raises(ValueError):

Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca

line 75why, one step back
75 client.get("http://www.example.org/")

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 76–77why, one step back
76
77

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 78–84why, one step back
78@pytest.mark.asyncio
79async def test_asgi_exc_async():80 client = httpx.AsyncClient(app=raise_exc)81 with pytest.raises(ValueError):82 await client.get("http://www.example.org/")8384

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

line 85why, one step back
85def test_asgi_exc_after_response():

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 86why, one step back
86 client = httpx.Client(app=raise_exc_after_response)

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

line 87why, one step back
87 with pytest.raises(ValueError):

Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca

line 88why, one step back
88 client.get("http://www.example.org/")

Add missing async test cases (#269)Florimond Mancabefore adoptiona4b93b9

lines 89–95why, one step back
89
9091@pytest.mark.asyncio92async def test_asgi_exc_after_response_async():93 client = httpx.AsyncClient(app=raise_exc_after_response)94 with pytest.raises(ValueError):95 await client.get("http://www.example.org/")