]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add tests to `test_responses` (#2656)
authorOrenoid <2367058391@qq.com>
Tue, 6 Aug 2024 07:14:59 +0000 (15:14 +0800)
committerGitHub <noreply@github.com>
Tue, 6 Aug 2024 07:14:59 +0000 (07:14 +0000)
* 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 <marcelotryle@gmail.com>
tests/test_responses.py

index 791e9b3ac3a1bb889112bbedf9ea9db9f809ee5f..c63c92de583f7aa1dca55d123359688799177394 100644 (file)
@@ -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"<file content>" * 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"<file content>" * 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"<file content>")
+
+    # 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"<file content>" * 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)