MCPcopy
hub / github.com/encode/starlette / test_read_request_disconnected_client

Function test_read_request_disconnected_client

tests/middleware/test_base.py:894–934  ·  view source on GitHub ↗

If we receive a disconnect message when the downstream ASGI app calls receive() the Request instance passed into the dispatch function should get marked as disconnected. The downstream ASGI app should not get a ClientDisconnect raised, instead if should just receive the disconnect me

()

Source from the content-addressed store, hash-verified

892
893@pytest.mark.anyio
894async def test_read_request_disconnected_client() -> None:
895 """If we receive a disconnect message when the downstream ASGI
896 app calls receive() the Request instance passed into the dispatch function
897 should get marked as disconnected.
898 The downstream ASGI app should not get a ClientDisconnect raised,
899 instead if should just receive the disconnect message.
900 """
901
902 async def endpoint(scope: Scope, receive: Receive, send: Send) -> None:
903 msg = await receive()
904 assert msg["type"] == "http.disconnect"
905 await Response()(scope, receive, send)
906
907 class ConsumingMiddleware(BaseHTTPMiddleware):
908 async def dispatch(
909 self,
910 request: Request,
911 call_next: RequestResponseEndpoint,
912 ) -> Response:
913 response = await call_next(request)
914 disconnected = await request.is_disconnected()
915 assert disconnected is True
916 return response
917
918 scope = {"type": "http", "method": "POST", "path": "/"}
919
920 async def receive() -> AsyncGenerator[Message, None]:
921 yield {"type": "http.disconnect"}
922 raise AssertionError("Should not be called, would hang") # pragma: no cover
923
924 async def send(msg: Message) -> None:
925 if msg["type"] == "http.response.start":
926 assert msg["status"] == 200
927
928 app: ASGIApp = ConsumingMiddleware(endpoint)
929
930 rcv = receive()
931
932 await app(scope, rcv.__anext__, send)
933
934 await rcv.aclose()
935
936
937@pytest.mark.anyio

Callers

nothing calls this directly

Calls 3

ConsumingMiddlewareClass · 0.85
receiveFunction · 0.70
appFunction · 0.70

Tested by

no test coverage detected