Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_debug_not_http():
def app(scope):
raise RuntimeError("Something went wrong")
app = DebugMiddleware(app)
with pytest.raises(RuntimeError):
app({"type": "websocket"})
def test_debug_html():
def app(scope):
async def asgi(receive, send):
raise RuntimeError("Something went wrong")
return asgi
app = DebugMiddleware(app)
client = TestClient(app, raise_server_exceptions=False)
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_error_during_scope():
def app(scope):
raise RuntimeError("Something went wrong")
app = DebugMiddleware(app)
client = TestClient(app, raise_server_exceptions=False)
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_after_response_sent():
def app(scope):
async def asgi(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")
return asgi
app = DebugMiddleware(app)
client = TestClient(app, raise_server_exceptions=False)
response = client.get('/')
assert response.status_code == 204
assert response.content == b''
def test_debug_app():
config = Config(app=asgi_app, debug=True, proxy_headers=False)
config.load()
assert config.debug is True
assert isinstance(config.loaded_app, DebugMiddleware)
def test_debug_text():
def app(scope):
async def asgi(receive, send):
raise RuntimeError("Something went wrong")
return asgi
app = DebugMiddleware(app)
client = TestClient(app, raise_server_exceptions=False)
response = client.get("/")
assert response.status_code == 500
assert response.headers["content-type"].startswith("text/plain")
assert "RuntimeError" in response.text
use_asgi_3 = hasattr(self.loaded_app, "__await__")
elif inspect.isfunction(self.loaded_app):
use_asgi_3 = asyncio.iscoroutinefunction(self.loaded_app)
else:
call = getattr(self.loaded_app, "__call__", None)
use_asgi_3 = asyncio.iscoroutinefunction(call)
self.interface = "asgi3" if use_asgi_3 else "asgi2"
if self.interface == "wsgi":
self.loaded_app = WSGIMiddleware(self.loaded_app)
self.ws_protocol_class = None
elif self.interface == "asgi2":
self.loaded_app = ASGI2Middleware(self.loaded_app)
if self.debug:
self.loaded_app = DebugMiddleware(self.loaded_app)
if logger.level <= TRACE_LOG_LEVEL:
self.loaded_app = MessageLoggerMiddleware(self.loaded_app)
if self.proxy_headers:
self.loaded_app = ProxyHeadersMiddleware(
self.loaded_app, trusted_hosts=self.forwarded_allow_ips
)
self.loaded = True