Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

234 lines · 7.8 KiB · Python

Last changed by

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

History of this file

Take it

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

line 94why, one step back
94async def test_asgi():

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 95–96why, one step back
95 transport = httpx.ASGITransport(app=hello_world)
96 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 97–102why, one step back
97 response = await client.get("http://www.example.org/")
9899 assert response.status_code == 200100 assert response.text == "Hello, World!"101102

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

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

line 104why, one step back
104async def test_asgi_urlencoded_path():

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 105–106why, one step back
105 transport = httpx.ASGITransport(app=echo_path)
106 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 107–113why, one step back
107 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")
108 response = await client.get(url)109110 assert response.status_code == 200111 assert response.json() == {"path": "/user@example.org"}112113

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

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

line 115why, one step back
115async def test_asgi_raw_path():

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 116–117why, one step back
116 transport = httpx.ASGITransport(app=echo_raw_path)
117 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 118–121why, one step back
118 url = httpx.URL("http://www.example.org/").copy_with(path="/user@example.org")
119 response = await client.get(url)120121 assert response.status_code == 200

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

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 123–124why, one step back
123
124

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

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

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

lines 126–129why, one step back
126async def test_asgi_raw_path_should_not_include_querystring_portion():
127 """128 See https://github.com/encode/httpx/issues/2810129 """

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 130–131why, one step back
130 transport = httpx.ASGITransport(app=echo_raw_path)
131 async with httpx.AsyncClient(transport=transport) as client:

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

lines 132–139why, one step back
132 url = httpx.URL("http://www.example.org/path?query")
133 response = await client.get(url)134135 assert response.status_code == 200136 assert response.json() == {"raw_path": "/path"}137138139@pytest.mark.anyio

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

line 140why, one step back
140async def test_asgi_upload():

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 141–142why, one step back
141 transport = httpx.ASGITransport(app=echo_body)
142 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 143–148why, one step back
143 response = await client.post("http://www.example.org/", content=b"example")
144145 assert response.status_code == 200146 assert response.text == "example"147148

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

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

line 150why, one step back
150async def test_asgi_headers():

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 151–152why, one step back
151 transport = httpx.ASGITransport(app=echo_headers)
152 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 153–166why, one step back
153 response = await client.get("http://www.example.org/")
154155 assert response.status_code == 200156 assert response.json() == {157 "headers": [158 ["host", "www.example.org"],159 ["accept", "*/*"],160 ["accept-encoding", "gzip, deflate, br"],161 ["connection", "keep-alive"],162 ["user-agent", f"python-httpx/{httpx.__version__}"],163 ]164 }165166

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

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

line 168why, one step back
168async def test_asgi_exc():

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 169–170why, one step back
169 transport = httpx.ASGITransport(app=raise_exc)
170 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 171–174why, one step back
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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

line 176why, one step back
176async def test_asgi_exc_after_response():

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 177–178why, one step back
177 transport = httpx.ASGITransport(app=raise_exc_after_response)
178 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 179–182why, one step back
179 with pytest.raises(RuntimeError):
180 await client.get("http://www.example.org/")181182

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

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

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 184–209why, one step back
184async def test_asgi_disconnect_after_response_complete():
185 disconnect = False186187 async def read_body(scope, receive, send):188 nonlocal disconnect189190 status = 200191 headers = [(b"content-type", "text/plain")]192193 await send(194 {"type": "http.response.start", "status": status, "headers": headers}195 )196 more_body = True197 while more_body:198 message = await receive()199 more_body = message.get("more_body", False)200201 await send({"type": "http.response.body", "body": b"", "more_body": False})202203 # The ASGI spec says of the Disconnect message:204 # "Sent to the application when a HTTP connection is closed or if receive is205 # called after a response has been sent."206 # So if receive() is called again, the disconnect message should be received207 message = await receive()208 disconnect = message.get("type") == "http.disconnect"209

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 210–211why, one step back
210 transport = httpx.ASGITransport(app=read_body)
211 async with httpx.AsyncClient(transport=transport) as client:

Add mypy flags (#2472)Adrian Garcia Badaraccobefore adoption1fc6a52

lines 212–215why, one step back
212 response = await client.post("http://www.example.org/", content=b"example")
213214 assert response.status_code == 200215 assert disconnect

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

lines 216–219why, one step back
216
217218@pytest.mark.anyio219async def test_asgi_exc_no_raise():

Tidy up import (#3020)Tom Christiebefore adoptiondd5304d

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

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

lines 221–224why, one step back
221 async with httpx.AsyncClient(transport=transport) as client:
222 response = await client.get("http://www.example.org/")223224 assert response.status_code == 500

Deprecate `app=...` in favor of explicit `WSGITransport`/`ASGITransport`. (#3050)Tom Christiebefore adoptioncabd1c0

lines 225–234why, one step back
225
226227@pytest.mark.anyio228async def test_deprecated_shortcut():229 """230 The `app=...` shortcut is now deprecated.231 Use the explicit transport style instead.232 """233 with pytest.warns(DeprecationWarning):234 httpx.AsyncClient(app=hello_world)