Skip to content

Commit e46165a

Browse files
OrenoidKludex
andauthored
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 <[email protected]>
1 parent 0410bbc commit e46165a

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

tests/test_responses.py

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import datetime as dt
4-
import os
54
import time
65
from http.cookies import SimpleCookie
76
from pathlib import Path
@@ -215,11 +214,10 @@ def test_response_phrase(test_client_factory: TestClientFactory) -> None:
215214
assert response.reason_phrase == ""
216215

217216

218-
def test_file_response(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
219-
path = os.path.join(tmpdir, "xyz")
217+
def test_file_response(tmp_path: Path, test_client_factory: TestClientFactory) -> None:
218+
path = tmp_path / "xyz"
220219
content = b"<file content>" * 1000
221-
with open(path, "wb") as file:
222-
file.write(content)
220+
path.write_bytes(content)
223221

224222
filled_by_bg_task = ""
225223

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

259257

260258
@pytest.mark.anyio
261-
async def test_file_response_on_head_method(tmpdir: Path) -> None:
262-
path = os.path.join(tmpdir, "xyz")
259+
async def test_file_response_on_head_method(tmp_path: Path) -> None:
260+
path = tmp_path / "xyz"
263261
content = b"<file content>" * 1000
264-
with open(path, "wb") as file:
265-
file.write(content)
262+
path.write_bytes(content)
266263

267264
app = FileResponse(path=path, filename="example.png")
268265

@@ -287,20 +284,34 @@ async def send(message: Message) -> None:
287284
await app({"type": "http", "method": "head"}, receive, send)
288285

289286

287+
def test_file_response_set_media_type(
288+
tmp_path: Path, test_client_factory: TestClientFactory
289+
) -> None:
290+
path = tmp_path / "xyz"
291+
path.write_bytes(b"<file content>")
292+
293+
# By default, FileResponse will determine the `content-type` based on
294+
# the filename or path, unless a specific `media_type` is provided.
295+
app = FileResponse(path=path, filename="example.png", media_type="image/jpeg")
296+
client: TestClient = test_client_factory(app)
297+
response = client.get("/")
298+
assert response.headers["content-type"] == "image/jpeg"
299+
300+
290301
def test_file_response_with_directory_raises_error(
291-
tmpdir: Path, test_client_factory: TestClientFactory
302+
tmp_path: Path, test_client_factory: TestClientFactory
292303
) -> None:
293-
app = FileResponse(path=tmpdir, filename="example.png")
304+
app = FileResponse(path=tmp_path, filename="example.png")
294305
client = test_client_factory(app)
295306
with pytest.raises(RuntimeError) as exc_info:
296307
client.get("/")
297308
assert "is not a file" in str(exc_info.value)
298309

299310

300311
def test_file_response_with_missing_file_raises_error(
301-
tmpdir: Path, test_client_factory: TestClientFactory
312+
tmp_path: Path, test_client_factory: TestClientFactory
302313
) -> None:
303-
path = os.path.join(tmpdir, "404.txt")
314+
path = tmp_path / "404.txt"
304315
app = FileResponse(path=path, filename="404.txt")
305316
client = test_client_factory(app)
306317
with pytest.raises(RuntimeError) as exc_info:
@@ -309,13 +320,12 @@ def test_file_response_with_missing_file_raises_error(
309320

310321

311322
def test_file_response_with_chinese_filename(
312-
tmpdir: Path, test_client_factory: TestClientFactory
323+
tmp_path: Path, test_client_factory: TestClientFactory
313324
) -> None:
314325
content = b"file content"
315326
filename = "你好.txt" # probably "Hello.txt" in Chinese
316-
path = os.path.join(tmpdir, filename)
317-
with open(path, "wb") as f:
318-
f.write(content)
327+
path = tmp_path / filename
328+
path.write_bytes(content)
319329
app = FileResponse(path=path, filename=filename)
320330
client = test_client_factory(app)
321331
response = client.get("/")
@@ -326,13 +336,12 @@ def test_file_response_with_chinese_filename(
326336

327337

328338
def test_file_response_with_inline_disposition(
329-
tmpdir: Path, test_client_factory: TestClientFactory
339+
tmp_path: Path, test_client_factory: TestClientFactory
330340
) -> None:
331341
content = b"file content"
332342
filename = "hello.txt"
333-
path = os.path.join(tmpdir, filename)
334-
with open(path, "wb") as f:
335-
f.write(content)
343+
path = tmp_path / filename
344+
path.write_bytes(content)
336345
app = FileResponse(path=path, filename=filename, content_disposition_type="inline")
337346
client = test_client_factory(app)
338347
response = client.get("/")
@@ -342,11 +351,9 @@ def test_file_response_with_inline_disposition(
342351
assert response.headers["content-disposition"] == expected_disposition
343352

344353

345-
def test_file_response_with_method_warns(
346-
tmpdir: Path, test_client_factory: TestClientFactory
347-
) -> None:
354+
def test_file_response_with_method_warns(tmp_path: Path) -> None:
348355
with pytest.warns(DeprecationWarning):
349-
FileResponse(path=tmpdir, filename="example.png", method="GET")
356+
FileResponse(path=tmp_path, filename="example.png", method="GET")
350357

351358

352359
def test_set_cookie(
@@ -381,6 +388,18 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
381388
)
382389

383390

391+
def test_set_cookie_path_none(test_client_factory: TestClientFactory) -> None:
392+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
393+
response = Response("Hello, world!", media_type="text/plain")
394+
response.set_cookie("mycookie", "myvalue", path=None)
395+
await response(scope, receive, send)
396+
397+
client = test_client_factory(app)
398+
response = client.get("/")
399+
assert response.text == "Hello, world!"
400+
assert response.headers["set-cookie"] == "mycookie=myvalue; SameSite=lax"
401+
402+
384403
@pytest.mark.parametrize(
385404
"expires",
386405
[
@@ -477,12 +496,11 @@ def test_response_do_not_add_redundant_charset(
477496

478497

479498
def test_file_response_known_size(
480-
tmpdir: Path, test_client_factory: TestClientFactory
499+
tmp_path: Path, test_client_factory: TestClientFactory
481500
) -> None:
482-
path = os.path.join(tmpdir, "xyz")
501+
path = tmp_path / "xyz"
483502
content = b"<file content>" * 1000
484-
with open(path, "wb") as file:
485-
file.write(content)
503+
path.write_bytes(content)
486504

487505
app = FileResponse(path=path, filename="example.png")
488506
client: TestClient = test_client_factory(app)

0 commit comments

Comments
 (0)