(tmpdir: Path, test_client_factory: TestClientFactory)
| 156 | |
| 157 | |
| 158 | def test_templates_with_environment(tmpdir: Path, test_client_factory: TestClientFactory) -> None: |
| 159 | path = os.path.join(tmpdir, "index.html") |
| 160 | with open(path, "w") as file: |
| 161 | file.write("<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>") |
| 162 | |
| 163 | async def homepage(request: Request) -> Response: |
| 164 | return templates.TemplateResponse(request, "index.html") |
| 165 | |
| 166 | env = jinja2.Environment(loader=jinja2.FileSystemLoader(str(tmpdir))) |
| 167 | app = Starlette( |
| 168 | debug=True, |
| 169 | routes=[Route("/", endpoint=homepage)], |
| 170 | ) |
| 171 | templates = Jinja2Templates(env=env) |
| 172 | client = test_client_factory(app) |
| 173 | response = client.get("/") |
| 174 | assert response.text == "<html>Hello, <a href='http://testserver/'>world</a></html>" |
| 175 | assert response.template.name == "index.html" # type: ignore |
| 176 | assert set(response.context.keys()) == {"request"} # type: ignore |
nothing calls this directly
no test coverage detected