forked from encode/uvicorn
/
test_debug.py
73 lines (59 loc) 路 2.15 KB
/
test_debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import httpx
import pytest
from uvicorn.middleware.debug import DebugMiddleware
@pytest.mark.anyio
async def test_debug_text():
async def app(scope, receive, send):
raise RuntimeError("Something went wrong")
app = DebugMiddleware(app)
transport = httpx.ASGITransport(
app=app,
raise_app_exceptions=False,
)
async with httpx.AsyncClient(
transport=transport, base_url="http://testserver"
) as client:
response = await client.get("/")
assert response.status_code == 500
assert response.headers["content-type"].startswith("text/plain")
assert "RuntimeError" in response.text
@pytest.mark.anyio
async def test_debug_html():
async def app(scope, receive, send):
raise RuntimeError("Something went wrong")
app = DebugMiddleware(app)
transport = httpx.ASGITransport(
app=app,
raise_app_exceptions=False,
)
async with httpx.AsyncClient(
transport=transport, base_url="http://testserver"
) as client:
response = await client.get("/", headers={"Accept": "text/html, */*"})
assert response.status_code == 500
assert response.headers["content-type"].startswith("text/html")
assert "RuntimeError" in response.text
@pytest.mark.anyio
async def test_debug_after_response_sent():
async def app(scope, receive, send):
await send({"type": "http.response.start", "status": 204, "headers": []})
await send({"type": "http.response.body", "body": b"", "more_body": False})
raise RuntimeError("Something went wrong")
app = DebugMiddleware(app)
transport = httpx.ASGITransport(
app=app,
raise_app_exceptions=False,
)
async with httpx.AsyncClient(
transport=transport, base_url="http://testserver"
) as client:
response = await client.get("/")
assert response.status_code == 204
assert response.content == b""
@pytest.mark.anyio
async def test_debug_not_http():
async def app(scope, send, receive):
raise RuntimeError("Something went wrong")
app = DebugMiddleware(app)
with pytest.raises(RuntimeError):
await app({"type": "websocket"}, None, None)