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

Function test_templates

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

Source from the content-addressed store, hash-verified

17
18
19def test_templates(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
20 path = os.path.join(tmpdir, "index.html")
21 with open(path, "w") as file:
22 file.write("<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>")
23
24 async def homepage(request: Request) -> Response:
25 return templates.TemplateResponse(request, "index.html")
26
27 app = Starlette(debug=True, routes=[Route("/", endpoint=homepage)])
28 templates = Jinja2Templates(directory=str(tmpdir))
29
30 client = test_client_factory(app)
31 response = client.get("/")
32 assert response.text == "<html>Hello, <a href='http://testserver/'>world</a></html>"
33 assert response.template.name == "index.html" # type: ignore
34 assert set(response.context.keys()) == {"request"} # type: ignore
35
36
37def test_templates_autoescape(tmp_path: Path) -> None:

Callers

nothing calls this directly

Calls 7

StarletteClass · 0.90
RouteClass · 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