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

Function test_request_url_starlette_context

tests/test_requests.py:646–671  ·  view source on GitHub ↗
(test_client_factory: TestClientFactory)

Source from the content-addressed store, hash-verified

644
645
646def test_request_url_starlette_context(test_client_factory: TestClientFactory) -> None:
647 from starlette.applications import Starlette
648 from starlette.middleware import Middleware
649 from starlette.routing import Route
650 from starlette.types import ASGIApp
651
652 url_for = None
653
654 async def homepage(request: Request) -> Response:
655 return PlainTextResponse("Hello, world!")
656
657 class CustomMiddleware:
658 def __init__(self, app: ASGIApp) -> None:
659 self.app = app
660
661 async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
662 nonlocal url_for
663 request = Request(scope, receive)
664 url_for = request.url_for("homepage")
665 await self.app(scope, receive, send)
666
667 app = Starlette(routes=[Route("/home", homepage)], middleware=[Middleware(CustomMiddleware)])
668
669 client = test_client_factory(app)
670 client.get("/home")
671 assert url_for == URL("http://testserver/home")

Callers

nothing calls this directly

Calls 6

StarletteClass · 0.90
RouteClass · 0.90
MiddlewareClass · 0.90
URLClass · 0.90
test_client_factoryFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected