Skip to content

Commit 30989e0

Browse files
committed
add test for ASGI pathsend
1 parent 88f4835 commit 30989e0

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tests/test_responses.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,38 @@ def test_file_response_with_method_warns(tmpdir, test_client_factory):
329329
FileResponse(path=tmpdir, filename="example.png", method="GET")
330330

331331

332+
@pytest.mark.anyio
333+
async def test_file_response_with_pathsend(tmpdir: Path):
334+
path = os.path.join(tmpdir, "xyz")
335+
content = b"<file content>" * 1000
336+
with open(path, "wb") as file:
337+
file.write(content)
338+
339+
app = FileResponse(path=path, filename="example.png")
340+
341+
async def receive() -> Message: # type: ignore[empty-body]
342+
... # pragma: no cover
343+
344+
async def send(message: Message) -> None:
345+
if message["type"] == "http.response.start":
346+
assert message["status"] == status.HTTP_200_OK
347+
headers = Headers(raw=message["headers"])
348+
assert headers["content-type"] == "image/png"
349+
assert "content-length" in headers
350+
assert "content-disposition" in headers
351+
assert "last-modified" in headers
352+
assert "etag" in headers
353+
elif message["type"] == "http.response.pathsend":
354+
assert message["path"] == str(path)
355+
356+
# Since the TestClient doesn't support `pathsend`, we need to test this directly.
357+
await app(
358+
{"type": "http", "method": "get", "extensions": {"http.response.pathsend": {}}},
359+
receive,
360+
send,
361+
)
362+
363+
332364
def test_set_cookie(test_client_factory, monkeypatch):
333365
# Mock time used as a reference for `Expires` by stdlib `SimpleCookie`.
334366
mocked_now = dt.datetime(2037, 1, 22, 12, 0, 0, tzinfo=dt.timezone.utc)

0 commit comments

Comments
 (0)