mirror of https://github.com/encode/starlette.git
allow exceptions in middleware to surface when using TestClient (#888)
This commit is contained in:
parent
59e0574954
commit
2ded04c8fe
|
@ -471,6 +471,8 @@ class TestClient(requests.Session):
|
|||
async def wait_startup(self) -> None:
|
||||
await self.receive_queue.put({"type": "lifespan.startup"})
|
||||
message = await self.send_queue.get()
|
||||
if message is None:
|
||||
self.task.result()
|
||||
assert message["type"] in (
|
||||
"lifespan.startup.complete",
|
||||
"lifespan.startup.failed",
|
||||
|
|
|
@ -3,6 +3,7 @@ import asyncio
|
|||
import pytest
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.middleware import Middleware
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.testclient import TestClient
|
||||
from starlette.websockets import WebSocket, WebSocketDisconnect
|
||||
|
@ -56,6 +57,25 @@ def test_error_on_startup():
|
|||
pass # pragma: no cover
|
||||
|
||||
|
||||
def test_exception_in_middleware():
|
||||
|
||||
class MiddlewareException(Exception):
|
||||
pass
|
||||
|
||||
class BrokenMiddleware:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
raise MiddlewareException()
|
||||
|
||||
broken_middleware = Starlette(middleware=[Middleware(BrokenMiddleware)])
|
||||
|
||||
with pytest.raises(MiddlewareException):
|
||||
with TestClient(broken_middleware):
|
||||
pass # pragma: no cover
|
||||
|
||||
|
||||
def test_testclient_asgi2():
|
||||
def app(scope):
|
||||
async def inner(receive, send):
|
||||
|
|
Loading…
Reference in New Issue