mirror of https://github.com/encode/starlette.git
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import os
|
|
|
|
import pytest
|
|
|
|
from starlette.applications import Starlette
|
|
from starlette.routing import Route
|
|
from starlette.templating import Jinja2Templates
|
|
|
|
|
|
def test_templates(tmpdir, test_client_factory):
|
|
path = os.path.join(tmpdir, "index.html")
|
|
with open(path, "w") as file:
|
|
file.write("<html>Hello, <a href='{{ url_for('homepage') }}'>world</a></html>")
|
|
|
|
async def homepage(request):
|
|
return templates.TemplateResponse("index.html", {"request": request})
|
|
|
|
app = Starlette(
|
|
debug=True,
|
|
routes=[Route("/", endpoint=homepage)],
|
|
)
|
|
templates = Jinja2Templates(directory=str(tmpdir))
|
|
|
|
client = test_client_factory(app)
|
|
response = client.get("/")
|
|
assert response.text == "<html>Hello, <a href='http://testserver/'>world</a></html>"
|
|
assert response.template.name == "index.html"
|
|
assert set(response.context.keys()) == {"request"}
|
|
|
|
|
|
def test_template_response_requires_request(tmpdir):
|
|
templates = Jinja2Templates(str(tmpdir))
|
|
with pytest.raises(ValueError):
|
|
templates.TemplateResponse("", {})
|