From e46165a1ca0e2c5df688bdc11c05beb4822b23af Mon Sep 17 00:00:00 2001 From: Orenoid <2367058391@qq.com> Date: Tue, 6 Aug 2024 15:14:59 +0800 Subject: [PATCH] Add tests to `test_responses` (#2656) * test: add test cases for uncovered branches in starlette.responses * chore: fix format issue * Update test * Remove unused import * Update tmpdir to tmp_path --------- Co-authored-by: Marcelo Trylesinski --- tests/test_responses.py | 76 +++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/tests/test_responses.py b/tests/test_responses.py index 791e9b3a..c63c92de 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -1,7 +1,6 @@ from __future__ import annotations import datetime as dt -import os import time from http.cookies import SimpleCookie from pathlib import Path @@ -215,11 +214,10 @@ def test_response_phrase(test_client_factory: TestClientFactory) -> None: assert response.reason_phrase == "" -def test_file_response(tmpdir: Path, test_client_factory: TestClientFactory) -> None: - path = os.path.join(tmpdir, "xyz") +def test_file_response(tmp_path: Path, test_client_factory: TestClientFactory) -> None: + path = tmp_path / "xyz" content = b"" * 1000 - with open(path, "wb") as file: - file.write(content) + path.write_bytes(content) filled_by_bg_task = "" @@ -258,11 +256,10 @@ def test_file_response(tmpdir: Path, test_client_factory: TestClientFactory) -> @pytest.mark.anyio -async def test_file_response_on_head_method(tmpdir: Path) -> None: - path = os.path.join(tmpdir, "xyz") +async def test_file_response_on_head_method(tmp_path: Path) -> None: + path = tmp_path / "xyz" content = b"" * 1000 - with open(path, "wb") as file: - file.write(content) + path.write_bytes(content) app = FileResponse(path=path, filename="example.png") @@ -287,10 +284,24 @@ async def test_file_response_on_head_method(tmpdir: Path) -> None: await app({"type": "http", "method": "head"}, receive, send) +def test_file_response_set_media_type( + tmp_path: Path, test_client_factory: TestClientFactory +) -> None: + path = tmp_path / "xyz" + path.write_bytes(b"") + + # By default, FileResponse will determine the `content-type` based on + # the filename or path, unless a specific `media_type` is provided. + app = FileResponse(path=path, filename="example.png", media_type="image/jpeg") + client: TestClient = test_client_factory(app) + response = client.get("/") + assert response.headers["content-type"] == "image/jpeg" + + def test_file_response_with_directory_raises_error( - tmpdir: Path, test_client_factory: TestClientFactory + tmp_path: Path, test_client_factory: TestClientFactory ) -> None: - app = FileResponse(path=tmpdir, filename="example.png") + app = FileResponse(path=tmp_path, filename="example.png") client = test_client_factory(app) with pytest.raises(RuntimeError) as exc_info: client.get("/") @@ -298,9 +309,9 @@ def test_file_response_with_directory_raises_error( def test_file_response_with_missing_file_raises_error( - tmpdir: Path, test_client_factory: TestClientFactory + tmp_path: Path, test_client_factory: TestClientFactory ) -> None: - path = os.path.join(tmpdir, "404.txt") + path = tmp_path / "404.txt" app = FileResponse(path=path, filename="404.txt") client = test_client_factory(app) with pytest.raises(RuntimeError) as exc_info: @@ -309,13 +320,12 @@ def test_file_response_with_missing_file_raises_error( def test_file_response_with_chinese_filename( - tmpdir: Path, test_client_factory: TestClientFactory + tmp_path: Path, test_client_factory: TestClientFactory ) -> None: content = b"file content" filename = "你好.txt" # probably "Hello.txt" in Chinese - path = os.path.join(tmpdir, filename) - with open(path, "wb") as f: - f.write(content) + path = tmp_path / filename + path.write_bytes(content) app = FileResponse(path=path, filename=filename) client = test_client_factory(app) response = client.get("/") @@ -326,13 +336,12 @@ def test_file_response_with_chinese_filename( def test_file_response_with_inline_disposition( - tmpdir: Path, test_client_factory: TestClientFactory + tmp_path: Path, test_client_factory: TestClientFactory ) -> None: content = b"file content" filename = "hello.txt" - path = os.path.join(tmpdir, filename) - with open(path, "wb") as f: - f.write(content) + path = tmp_path / filename + path.write_bytes(content) app = FileResponse(path=path, filename=filename, content_disposition_type="inline") client = test_client_factory(app) response = client.get("/") @@ -342,11 +351,9 @@ def test_file_response_with_inline_disposition( assert response.headers["content-disposition"] == expected_disposition -def test_file_response_with_method_warns( - tmpdir: Path, test_client_factory: TestClientFactory -) -> None: +def test_file_response_with_method_warns(tmp_path: Path) -> None: with pytest.warns(DeprecationWarning): - FileResponse(path=tmpdir, filename="example.png", method="GET") + FileResponse(path=tmp_path, filename="example.png", method="GET") def test_set_cookie( @@ -381,6 +388,18 @@ def test_set_cookie( ) +def test_set_cookie_path_none(test_client_factory: TestClientFactory) -> None: + async def app(scope: Scope, receive: Receive, send: Send) -> None: + response = Response("Hello, world!", media_type="text/plain") + response.set_cookie("mycookie", "myvalue", path=None) + await response(scope, receive, send) + + client = test_client_factory(app) + response = client.get("/") + assert response.text == "Hello, world!" + assert response.headers["set-cookie"] == "mycookie=myvalue; SameSite=lax" + + @pytest.mark.parametrize( "expires", [ @@ -477,12 +496,11 @@ def test_response_do_not_add_redundant_charset( def test_file_response_known_size( - tmpdir: Path, test_client_factory: TestClientFactory + tmp_path: Path, test_client_factory: TestClientFactory ) -> None: - path = os.path.join(tmpdir, "xyz") + path = tmp_path / "xyz" content = b"" * 1000 - with open(path, "wb") as file: - file.write(content) + path.write_bytes(content) app = FileResponse(path=path, filename="example.png") client: TestClient = test_client_factory(app) -- 2.47.3