2018-07-18 12:04:14 +00:00
|
|
|
from starlette import Request, Response, TestClient
|
|
|
|
from starlette.debug import DebugMiddleware
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def test_debug_text():
|
|
|
|
def app(scope):
|
|
|
|
async def asgi(receive, send):
|
2018-07-18 12:08:06 +00:00
|
|
|
raise RuntimeError("Something went wrong")
|
|
|
|
|
2018-07-18 12:04:14 +00:00
|
|
|
return asgi
|
|
|
|
|
|
|
|
client = TestClient(DebugMiddleware(app))
|
|
|
|
response = client.get("/")
|
|
|
|
assert response.status_code == 500
|
2018-07-18 12:08:06 +00:00
|
|
|
assert response.headers["content-type"].startswith("text/plain")
|
|
|
|
assert "RuntimeError" in response.text
|
2018-07-18 12:04:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_debug_html():
|
|
|
|
def app(scope):
|
|
|
|
async def asgi(receive, send):
|
2018-07-18 12:08:06 +00:00
|
|
|
raise RuntimeError("Something went wrong")
|
|
|
|
|
2018-07-18 12:04:14 +00:00
|
|
|
return asgi
|
|
|
|
|
|
|
|
client = TestClient(DebugMiddleware(app))
|
2018-07-18 12:08:06 +00:00
|
|
|
response = client.get("/", headers={"Accept": "text/html, */*"})
|
2018-07-18 12:04:14 +00:00
|
|
|
assert response.status_code == 500
|
2018-07-18 12:08:06 +00:00
|
|
|
assert response.headers["content-type"].startswith("text/html")
|
|
|
|
assert "RuntimeError" in response.text
|
2018-07-18 12:04:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_debug_after_response_sent():
|
|
|
|
def app(scope):
|
|
|
|
async def asgi(receive, send):
|
2018-07-18 12:08:06 +00:00
|
|
|
response = Response(b"", status_code=204)
|
2018-07-18 12:04:14 +00:00
|
|
|
await response(receive, send)
|
2018-07-18 12:08:06 +00:00
|
|
|
raise RuntimeError("Something went wrong")
|
|
|
|
|
2018-07-18 12:04:14 +00:00
|
|
|
return asgi
|
|
|
|
|
|
|
|
client = TestClient(DebugMiddleware(app))
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
response = client.get("/")
|
2018-07-18 14:34:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_debug_error_during_scope():
|
|
|
|
def app(scope):
|
|
|
|
raise RuntimeError("Something went wrong")
|
|
|
|
|
|
|
|
app = DebugMiddleware(app)
|
|
|
|
client = TestClient(DebugMiddleware(app))
|
|
|
|
response = client.get("/", headers={"Accept": "text/html, */*"})
|
|
|
|
assert response.status_code == 500
|
|
|
|
assert response.headers["content-type"].startswith("text/html")
|
|
|
|
assert "RuntimeError" in response.text
|
|
|
|
|
|
|
|
|
|
|
|
def test_debug_not_http():
|
|
|
|
def app(scope):
|
|
|
|
raise RuntimeError("Something went wrong")
|
|
|
|
|
|
|
|
app = DebugMiddleware(app)
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
app({"type": "websocket"})
|