Skip to content
Myr

test_asgi.py

This file

Path

tests/test_asgi.py

203 lines · 6.7 KiB · Python

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 67c2970 point at it.

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 1–5why, one step back
1import json
23import pytest45import httpx

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

line 6why, one step back
6from httpx import ASGITransport

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 7–73why, one step back
7
89async def hello_world(scope, receive, send):10 status = 20011 output = b"Hello, World!"12 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]1314 await send({"type": "http.response.start", "status": status, "headers": headers})15 await send({"type": "http.response.body", "body": output})161718async def echo_path(scope, receive, send):19 status = 20020 output = json.dumps({"path": scope["path"]}).encode("utf-8")21 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]2223 await send({"type": "http.response.start", "status": status, "headers": headers})24 await send({"type": "http.response.body", "body": output})252627async def echo_raw_path(scope, receive, send):28 status = 20029 output = json.dumps({"raw_path": scope["raw_path"].decode("ascii")}).encode("utf-8")30 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]3132 await send({"type": "http.response.start", "status": status, "headers": headers})33 await send({"type": "http.response.body", "body": output})343536async def echo_body(scope, receive, send):37 status = 20038 headers = [(b"content-type", "text/plain")]3940 await send({"type": "http.response.start", "status": status, "headers": headers})41 more_body = True42 while more_body:43 message = await receive()44 body = message.get("body", b"")45 more_body = message.get("more_body", False)46 await send({"type": "http.response.body", "body": body, "more_body": more_body})474849async def echo_headers(scope, receive, send):50 status = 20051 output = json.dumps(52 {"headers": [[k.decode(), v.decode()] for k, v in scope["headers"]]}53 ).encode("utf-8")54 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]5556 await send({"type": "http.response.start", "status": status, "headers": headers})57 await send({"type": "http.response.body", "body": output})585960async def raise_exc(scope, receive, send):61 raise RuntimeError()626364async def raise_exc_after_response(scope, receive, send):65 status = 20066 output = b"Hello, World!"67 headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]6869 await send({"type": "http.response.start", "status": status, "headers": headers})70 await send({"type": "http.response.body", "body": output})71 raise RuntimeError()7273

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

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

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

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

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 95–102why, one step back
95async def test_asgi():
96 async with httpx.AsyncClient(app=hello_world) as client: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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

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

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

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

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 121–122why, one step back
121
122

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 124–131why, one step back
124async def test_asgi_upload():
125 async with httpx.AsyncClient(app=echo_body) as client:126 response = await client.post("http://www.example.org/", content=b"example")127128 assert response.status_code == 200129 assert response.text == "example"130131

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 133–148why, one step back
133async def test_asgi_headers():
134 async with httpx.AsyncClient(app=echo_headers) as client:135 response = await client.get("http://www.example.org/")136137 assert response.status_code == 200138 assert response.json() == {139 "headers": [140 ["host", "www.example.org"],141 ["accept", "*/*"],142 ["accept-encoding", "gzip, deflate, br"],143 ["connection", "keep-alive"],144 ["user-agent", f"python-httpx/{httpx.__version__}"],145 ]146 }147148

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 150–155why, one step back
150async def test_asgi_exc():
151 async with httpx.AsyncClient(app=raise_exc) as client:152 with pytest.raises(RuntimeError):153 await client.get("http://www.example.org/")154155

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 157–162why, one step back
157async def test_asgi_exc_after_response():
158 async with httpx.AsyncClient(app=raise_exc_after_response) as client:159 with pytest.raises(RuntimeError):160 await client.get("http://www.example.org/")161162

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

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

Drop EOL Python 3.6 (#2097)Ofek Levbefore adoption67c2970

lines 164–194why, one step back
164async def test_asgi_disconnect_after_response_complete():
165 disconnect = False166167 async def read_body(scope, receive, send):168 nonlocal disconnect169170 status = 200171 headers = [(b"content-type", "text/plain")]172173 await send(174 {"type": "http.response.start", "status": status, "headers": headers}175 )176 more_body = True177 while more_body:178 message = await receive()179 more_body = message.get("more_body", False)180181 await send({"type": "http.response.body", "body": b"", "more_body": False})182183 # The ASGI spec says of the Disconnect message:184 # "Sent to the application when a HTTP connection is closed or if receive is185 # called after a response has been sent."186 # So if receive() is called again, the disconnect message should be received187 message = await receive()188 disconnect = message.get("type") == "http.disconnect"189190 async with httpx.AsyncClient(app=read_body) as client:191 response = await client.post("http://www.example.org/", content=b"example")192193 assert response.status_code == 200194 assert disconnect

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

lines 195–203why, one step back
195
196197@pytest.mark.anyio198async def test_asgi_exc_no_raise():199 transport = ASGITransport(app=raise_exc, raise_app_exceptions=False)200 async with httpx.AsyncClient(transport=transport) as client:201 response = await client.get("http://www.example.org/")202203 assert response.status_code == 500