allow exceptions in middleware to surface when using TestClient (#888)

This commit is contained in:
Chris Withers 2020-04-15 10:46:46 +02:00 committed by GitHub
parent 59e0574954
commit 2ded04c8fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -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",

View File

@ -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):