from __future__ import annotations
import datetime as dt
-import os
import time
from http.cookies import SimpleCookie
from pathlib import Path
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 = ""
@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")
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("/")
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:
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("/")
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("/")
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(
)
+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",
[
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)