(tmp_path: Path, test_client_factory: TestClientFactory)
| 47 | |
| 48 | |
| 49 | def 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 | |
| 77 | def test_template_with_middleware(tmpdir: Path, test_client_factory: TestClientFactory) -> None: |
nothing calls this directly
no test coverage detected