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

Function test_middleware_factory

tests/test_applications.py:529–549  ·  view source on GitHub ↗
(test_client_factory: TestClientFactory)

Source from the content-addressed store, hash-verified

527
528
529def test_middleware_factory(test_client_factory: TestClientFactory) -> None:
530 calls: list[str] = []
531
532 def _middleware_factory(app: ASGIApp, arg: str) -> ASGIApp:
533 async def _app(scope: Scope, receive: Receive, send: Send) -> None:
534 calls.append(arg)
535 await app(scope, receive, send)
536
537 return _app
538
539 def get_middleware_factory() -> Callable[[ASGIApp, str], ASGIApp]:
540 return _middleware_factory
541
542 app = Starlette()
543 app.add_middleware(_middleware_factory, arg="foo")
544 app.add_middleware(get_middleware_factory(), "bar")
545
546 with test_client_factory(app):
547 pass
548
549 assert calls == ["bar", "foo"]
550
551
552def test_lifespan_app_subclass() -> None:

Callers

nothing calls this directly

Calls 4

add_middlewareMethod · 0.95
StarletteClass · 0.90
get_middleware_factoryFunction · 0.85
test_client_factoryFunction · 0.85

Tested by

no test coverage detected