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

Function test_asgi_pathsend_events

tests/middleware/test_base.py:1219–1259  ·  view source on GitHub ↗
(tmpdir: Path)

Source from the content-addressed store, hash-verified

1217
1218@pytest.mark.anyio
1219async def test_asgi_pathsend_events(tmpdir: Path) -> None:
1220 path = tmpdir / "example.txt"
1221 with path.open("w") as file:
1222 file.write("<file content>")
1223
1224 response_complete = anyio.Event()
1225 events: list[Message] = []
1226
1227 async def endpoint_with_pathsend(_: Request) -> FileResponse:
1228 return FileResponse(path)
1229
1230 async def passthrough(request: Request, call_next: RequestResponseEndpoint) -> Response:
1231 return await call_next(request)
1232
1233 app = Starlette(
1234 middleware=[Middleware(BaseHTTPMiddleware, dispatch=passthrough)],
1235 routes=[Route("/", endpoint_with_pathsend)],
1236 )
1237
1238 scope = {
1239 "type": "http",
1240 "version": "3",
1241 "method": "GET",
1242 "path": "/",
1243 "headers": [],
1244 "extensions": {"http.response.pathsend": {}},
1245 }
1246
1247 async def receive() -> Message:
1248 raise NotImplementedError("Should not be called!") # pragma: no cover
1249
1250 async def send(message: Message) -> None:
1251 events.append(message)
1252 if message["type"] == "http.response.pathsend":
1253 response_complete.set()
1254
1255 await app(scope, receive, send)
1256
1257 assert len(events) == 2
1258 assert events[0]["type"] == "http.response.start"
1259 assert events[1]["type"] == "http.response.pathsend"
1260
1261
1262def test_error_context_propagation(test_client_factory: TestClientFactory) -> None:

Callers

nothing calls this directly

Calls 5

StarletteClass · 0.90
MiddlewareClass · 0.90
RouteClass · 0.90
appFunction · 0.70
writeMethod · 0.45

Tested by

no test coverage detected