spy.assert_called()
+class TestTemplatesArgsOnly:
+ # MAINTAINERS: remove after 1.0
+ def test_name_and_context(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
+ path = os.path.join(tmpdir, "index.html")
+ with open(path, "w") as file:
+ file.write("value: {{ a }}")
+ templates = Jinja2Templates(directory=str(tmpdir))
+
+ def page(request: Request) -> Response:
+ return templates.TemplateResponse(
+ "index.html",
+ {"a": "b", "request": request},
+ )
+
+ app = Starlette(routes=[Route("/", page)])
+ client = test_client_factory(app)
+ with pytest.warns(DeprecationWarning):
+ response = client.get("/")
+
+ assert response.text == "value: b" # context was rendered
+ assert response.status_code == 200
+
+ def test_status_code(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
+ path = os.path.join(tmpdir, "index.html")
+ with open(path, "w") as file:
+ file.write("value: {{ a }}")
+ templates = Jinja2Templates(directory=str(tmpdir))
+
+ def page(request: Request) -> Response:
+ return templates.TemplateResponse(
+ "index.html",
+ {"a": "b", "request": request},
+ 201,
+ )
+
+ app = Starlette(routes=[Route("/", page)])
+ client = test_client_factory(app)
+ with pytest.warns(DeprecationWarning):
+ response = client.get("/")
+
+ assert response.text == "value: b" # context was rendered
+ assert response.status_code == 201
+
+ def test_headers(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
+ path = os.path.join(tmpdir, "index.html")
+ with open(path, "w") as file:
+ file.write("value: {{ a }}")
+ templates = Jinja2Templates(directory=str(tmpdir))
+
+ def page(request: Request) -> Response:
+ return templates.TemplateResponse(
+ "index.html",
+ {"a": "b", "request": request},
+ 201,
+ {"x-key": "value"},
+ )
+
+ app = Starlette(routes=[Route("/", page)])
+ client = test_client_factory(app)
+ with pytest.warns(DeprecationWarning):
+ response = client.get("/")
+
+ assert response.text == "value: b" # context was rendered
+ assert response.status_code == 201
+ assert response.headers["x-key"] == "value"
+
+ def test_media_type(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
+ path = os.path.join(tmpdir, "index.html")
+ with open(path, "w") as file:
+ file.write("value: {{ a }}")
+ templates = Jinja2Templates(directory=str(tmpdir))
+
+ def page(request: Request) -> Response:
+ return templates.TemplateResponse(
+ "index.html",
+ {"a": "b", "request": request},
+ 201,
+ {"x-key": "value"},
+ "text/plain",
+ )
+
+ app = Starlette(routes=[Route("/", page)])
+ client = test_client_factory(app)
+ with pytest.warns(DeprecationWarning):
+ response = client.get("/")
+
+ assert response.text == "value: b" # context was rendered
+ assert response.status_code == 201
+ assert response.headers["x-key"] == "value"
+ assert response.headers["content-type"] == "text/plain; charset=utf-8"
+
+ def test_all_args(self, tmpdir: Path, test_client_factory: TestClientFactory) -> None:
+ path = os.path.join(tmpdir, "index.html")
+ with open(path, "w") as file:
+ file.write("value: {{ a }}")
+ templates = Jinja2Templates(directory=str(tmpdir))
+
+ spy = mock.MagicMock()
+
+ def page(request: Request) -> Response:
+ return templates.TemplateResponse(
+ "index.html",
+ {"a": "b", "request": request},
+ 201,
+ {"x-key": "value"},
+ "text/plain",
+ BackgroundTask(func=spy),
+ )
+
+ app = Starlette(routes=[Route("/", page)])
+ client = test_client_factory(app)
+ with pytest.warns(DeprecationWarning):
+ response = client.get("/")
+
+ assert response.text == "value: b" # context was rendered
+ assert response.status_code == 201
+ assert response.headers["x-key"] == "value"
+ assert response.headers["content-type"] == "text/plain; charset=utf-8"
+ spy.assert_called()
+
+
def test_templates_when_first_argument_is_request(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
# MAINTAINERS: remove after 1.0
path = os.path.join(tmpdir, "index.html")