from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware
-from starlette.responses import PlainTextResponse
+from starlette.responses import PlainTextResponse, StreamingResponse
from starlette.routing import Route
raise Exception("Exc")
+@app.route("/exc-stream")
+def exc_stream(request):
+ return StreamingResponse(_generate_faulty_stream())
+
+
+def _generate_faulty_stream():
+ yield b"Ok"
+ raise Exception("Faulty Stream")
+
+
@app.route("/no-response")
class NoResponse:
def __init__(self, scope, receive, send):
response = client.get("/exc")
assert str(ctx.value) == "Exc"
+ with pytest.raises(Exception) as ctx:
+ response = client.get("/exc-stream")
+ assert str(ctx.value) == "Faulty Stream"
+
with pytest.raises(RuntimeError):
response = client.get("/no-response")
client = test_client_factory(app)
response = client.get("/does_not_exist")
assert response.text == "Custom"
+
+
+def test_exception_on_mounted_apps(test_client_factory):
+ sub_app = Starlette(routes=[Route("/", exc)])
+ app.mount("/sub", sub_app)
+
+ client = test_client_factory(app)
+ with pytest.raises(Exception) as ctx:
+ client.get("/sub/")
+ assert str(ctx.value) == "Exc"