(tmpdir: Path, test_client_factory: TestClientFactory)
| 75 | |
| 76 | |
| 77 | def 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 | |
| 103 | def test_templates_with_directories(tmp_path: Path, test_client_factory: TestClientFactory) -> None: |
nothing calls this directly
no test coverage detected