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

Function test_calls_context_processors

tests/test_templates.py:49–74  ·  view source on GitHub ↗
(tmp_path: Path, test_client_factory: TestClientFactory)

Source from the content-addressed store, hash-verified

47
48
49def test_calls_context_processors(tmp_path: Path, test_client_factory: TestClientFactory) -> None:
50 path = tmp_path / "index.html"
51 path.write_text("<html>Hello {{ username }}</html>")
52
53 async def homepage(request: Request) -> Response:
54 return templates.TemplateResponse(request, "index.html")
55
56 def hello_world_processor(request: Request) -> dict[str, str]:
57 return {"username": "World"}
58
59 app = Starlette(
60 debug=True,
61 routes=[Route("/", endpoint=homepage)],
62 )
63 templates = Jinja2Templates(
64 directory=tmp_path,
65 context_processors=[
66 hello_world_processor,
67 ],
68 )
69
70 client = test_client_factory(app)
71 response = client.get("/")
72 assert response.text == "<html>Hello World</html>"
73 assert response.template.name == "index.html" # type: ignore
74 assert set(response.context.keys()) == {"request", "username"} # type: ignore
75
76
77def test_template_with_middleware(tmpdir: Path, test_client_factory: TestClientFactory) -> None:

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected