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

Function test_template_with_middleware

tests/test_templates.py:77–100  ·  view source on GitHub ↗
(tmpdir: Path, test_client_factory: TestClientFactory)

Source from the content-addressed store, hash-verified

75
76
77def test_template_with_middleware(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
78 path = os.path.join(tmpdir, "index.html")
79 with open(path, "w") as file:
80 file.write("<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>")
81
82 async def homepage(request: Request) -> Response:
83 return templates.TemplateResponse(request, "index.html")
84
85 class CustomMiddleware(BaseHTTPMiddleware):
86 async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
87 return await call_next(request)
88
89 app = Starlette(
90 debug=True,
91 routes=[Route("/", endpoint=homepage)],
92 middleware=[Middleware(CustomMiddleware)],
93 )
94 templates = Jinja2Templates(directory=str(tmpdir))
95
96 client = test_client_factory(app)
97 response = client.get("/")
98 assert response.text == "<html>Hello, <a href='http://testserver/'>world</a></html>"
99 assert response.template.name == "index.html" # type: ignore
100 assert set(response.context.keys()) == {"request"} # type: ignore
101
102
103def test_templates_with_directories(tmp_path: Path, test_client_factory: TestClientFactory) -> None:

Callers

nothing calls this directly

Calls 8

StarletteClass · 0.90
RouteClass · 0.90
MiddlewareClass · 0.90
Jinja2TemplatesClass · 0.90
test_client_factoryFunction · 0.85
writeMethod · 0.45
getMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected