Skip to content

Commit

Permalink
Add tests to test_responses (#2656)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Orenoid and Kludex committed Aug 6, 2024
1 parent 0410bbc commit e46165a
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = ""

Expand Down Expand Up @@ -258,11 +256,10 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:


@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")

Expand All @@ -287,20 +284,34 @@ async def send(message: Message) -> 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("/")
assert "is not a file" in str(exc_info.value)


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:
Expand All @@ -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("/")
Expand All @@ -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("/")
Expand All @@ -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(
Expand Down Expand Up @@ -381,6 +388,18 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
)


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",
[
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e46165a

Please sign in to comment.