Skip to content

Commit 7936e86

Browse files
authored
Add support for ASGI pathsend extension (#2435)
* add support for ASGI `pathsend` extension * add test for ASGI `pathsend`
1 parent 6bfe9fe commit 7936e86

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

starlette/responses.py

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
337337
)
338338
if scope["method"].upper() == "HEAD":
339339
await send({"type": "http.response.body", "body": b"", "more_body": False})
340+
elif "http.response.pathsend" in scope["extensions"]:
341+
await send({"type": "http.response.pathsend", "path": str(self.path)})
340342
else:
341343
async with await anyio.open_file(self.path, mode="rb") as file:
342344
more_body = True

tests/test_responses.py

+32
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)