Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

215 lines · 7.1 KiB · Python

Last changed by

Tidy up import (#3020)Tom Christiebefore adoptiondd5304d

History of this file

Take it

The walk stopped after 200 commits: lines older than 9e97d7d point at it.

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 1–72why, one step back
1import json
23import 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_raw_path(scope, receive, send):27 status = 20028 output = json.dumps({"raw_path": scope["raw_path"].decode("ascii")}).encode("utf-8")29 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]3031 await send({"type": "http.response.start", "status": status, "headers": headers})32 await send({"type": "http.response.body", "body": output})333435async def echo_body(scope, receive, send):36 status = 20037 headers = [(b"content-type", "text/plain")]3839 await send({"type": "http.response.start", "status": status, "headers": headers})40 more_body = True41 while more_body:42 message = await receive()43 body = message.get("body", b"")44 more_body = message.get("more_body", False)45 await send({"type": "http.response.body", "body": body, "more_body": more_body})464748async def echo_headers(scope, receive, send):49 status = 20050 output = json.dumps(51 {"headers": [[k.decode(), v.decode()] for k, v in scope["headers"]]}52 ).encode("utf-8")53 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]5455 await send({"type": "http.response.start", "status": status, "headers": headers})56 await send({"type": "http.response.body", "body": output})575859async def raise_exc(scope, receive, send):60 raise RuntimeError()616263async def raise_exc_after_response(scope, receive, send):64 status = 20065 output = b"Hello, World!"66 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]6768 await send({"type": "http.response.start", "status": status, "headers": headers})69 await send({"type": "http.response.body", "body": output})70 raise RuntimeError()7172

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 73why, one step back
73@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 74–82why, one step back
74async def test_asgi_transport():
75 async with httpx.ASGITransport(app=hello_world) as transport:76 request = httpx.Request("GET", "http://www.example.com/")77 response = await transport.handle_async_request(request)78 await response.aread()79 assert response.status_code == 20080 assert response.content == b"Hello, World!"8182

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 83why, one step back
83@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 84–92why, one step back
84async def test_asgi_transport_no_body():
85 async with httpx.ASGITransport(app=echo_body) as transport:86 request = httpx.Request("GET", "http://www.example.com/")87 response = await transport.handle_async_request(request)88 await response.aread()89 assert response.status_code == 20090 assert response.content == b""9192

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 93why, one step back
93@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 94–101why, one step back
94async def test_asgi():
95 async with httpx.AsyncClient(app=hello_world) as client:96 response = await client.get("http://www.example.org/")9798 assert response.status_code == 20099 assert response.text == "Hello, World!"100101

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 102why, one step back
102@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 103–111why, one step back
103async def test_asgi_urlencoded_path():
104 async with httpx.AsyncClient(app=echo_path) as client:105 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")106 response = await client.get(url)107108 assert response.status_code == 200109 assert response.json() == {"path": "/user@example.org"}110111

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 112why, one step back
112@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 113–118why, one step back
113async def test_asgi_raw_path():
114 async with httpx.AsyncClient(app=echo_raw_path) as client:115 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")116 response = await client.get(url)117118 assert response.status_code == 200

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

line 119why, one step back
119 assert response.json() == {"raw_path": "/user@example.org"}

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 120–121why, one step back
120
121

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 122why, one step back
122@pytest.mark.anyio

Ensure that ASGI 'raw_path' does not include query component of URL. (#2999)Tom Christiebefore adoption90538a3

lines 123–135why, one step back
123async def test_asgi_raw_path_should_not_include_querystring_portion():
124 """125 See https://github.com/encode/httpx/issues/2810126 """127 async with httpx.AsyncClient(app=echo_raw_path) as client:128 url = httpx.URL("http://www.example.org/path?query")129 response = await client.get(url)130131 assert response.status_code == 200132 assert response.json() == {"raw_path": "/path"}133134135@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 136–143why, one step back
136async def test_asgi_upload():
137 async with httpx.AsyncClient(app=echo_body) as client:138 response = await client.post("http://www.example.org/", content=b"example")139140 assert response.status_code == 200141 assert response.text == "example"142143

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 144why, one step back
144@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 145–160why, one step back
145async def test_asgi_headers():
146 async with httpx.AsyncClient(app=echo_headers) as client:147 response = await client.get("http://www.example.org/")148149 assert response.status_code == 200150 assert response.json() == {151 "headers": [152 ["host", "www.example.org"],153 ["accept", "*/*"],154 ["accept-encoding", "gzip, deflate, br"],155 ["connection", "keep-alive"],156 ["user-agent", f"python-httpx/{httpx.__version__}"],157 ]158 }159160

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 161why, one step back
161@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 162–167why, one step back
162async def test_asgi_exc():
163 async with httpx.AsyncClient(app=raise_exc) as client:164 with pytest.raises(RuntimeError):165 await client.get("http://www.example.org/")166167

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 168why, one step back
168@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 169–174why, one step back
169async def test_asgi_exc_after_response():
170 async with httpx.AsyncClient(app=raise_exc_after_response) as client:171 with pytest.raises(RuntimeError):172 await client.get("http://www.example.org/")173174

replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8

line 175why, one step back
175@pytest.mark.anyio

Drop stalebot (#2412)Tom Christiebefore adoption9e97d7d

lines 176–206why, one step back
176async def test_asgi_disconnect_after_response_complete():
177 disconnect = False178179 async def read_body(scope, receive, send):180 nonlocal disconnect181182 status = 200183 headers = [(b"content-type", "text/plain")]184185 await send(186 {"type": "http.response.start", "status": status, "headers": headers}187 )188 more_body = True189 while more_body:190 message = await receive()191 more_body = message.get("more_body", False)192193 await send({"type": "http.response.body", "body": b"", "more_body": False})194195 # The ASGI spec says of the Disconnect message:196 # "Sent to the application when a HTTP connection is closed or if receive is197 # called after a response has been sent."198 # So if receive() is called again, the disconnect message should be received199 message = await receive()200 disconnect = message.get("type") == "http.disconnect"201202 async with httpx.AsyncClient(app=read_body) as client:203 response = await client.post("http://www.example.org/", content=b"example")204205 assert response.status_code == 200206 assert disconnect

Fix exception suppression in asgi transport (#2669)Nikbefore adoptiona682f6f

lines 207–210why, one step back
207
208209@pytest.mark.anyio210async def test_asgi_exc_no_raise():

Tidy up import (#3020)Tom Christiebefore adoptiondd5304d

line 211why, one step back
211 transport = httpx.ASGITransport(app=raise_exc, raise_app_exceptions=False)

Fix exception suppression in asgi transport (#2669)Nikbefore adoptiona682f6f

lines 212–215why, one step back
212 async with httpx.AsyncClient(transport=transport) as client:
213 response = await client.get("http://www.example.org/")214215 assert response.status_code == 500