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

Function test_templates_with_environment

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

Source from the content-addressed store, hash-verified

156
157
158def 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

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