From e9e0f610983e4fed2195d6089a9d57fcc17da0aa Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Wed, 8 Jan 2025 16:44:41 +0100 Subject: [PATCH] DO NOT MERGE Trying to demonstrate that the test will not even fail when too many bytes are sent. --- aiohttp/http_writer.py | 1 - aiohttp/web_fileresponse.py | 2 +- tests/test_web_sendfile_functional.py | 14 +++++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/aiohttp/http_writer.py b/aiohttp/http_writer.py index 28b14f7a791..d178629c488 100644 --- a/aiohttp/http_writer.py +++ b/aiohttp/http_writer.py @@ -123,7 +123,6 @@ async def write( if self.length >= chunk_len: self.length = self.length - chunk_len else: - chunk = chunk[: self.length] self.length = 0 if not chunk: return diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 0078e1ea782..28694d33dbd 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -114,7 +114,7 @@ async def _sendfile_fallback( chunk_size = self._chunk_size loop = asyncio.get_event_loop() chunk = await loop.run_in_executor( - None, self._seek_and_read, fobj, offset, min(chunk_size, count) + None, self._seek_and_read, fobj, offset, chunk_size ) while chunk: await writer.write(chunk) diff --git a/tests/test_web_sendfile_functional.py b/tests/test_web_sendfile_functional.py index 00a8985533d..d37c280aa99 100644 --- a/tests/test_web_sendfile_functional.py +++ b/tests/test_web_sendfile_functional.py @@ -1179,9 +1179,15 @@ async def handler(request: web.Request) -> web.FileResponse: app.router.add_get("/", handler) client = await aiohttp_client(app) - resp = await client.get("/", headers={"Range": "bytes=20-40"}) - assert resp.status == 206 - body = await resp.read() + async with await client.get("/", headers={"Range": "bytes=20-40"}) as resp: + assert resp.status == 206 + assert resp.headers["content-length"] == "21" + body = await resp.read() + + async with await client.get("/", headers={"Range": "bytes=20-40"}) as resp: + assert resp.status == 206 + assert resp.headers["content-length"] == "21" + body2 = await resp.read() # 21 bytes, because http range headers are including both fenceposts. assert len(body) == 21 @@ -1191,3 +1197,5 @@ async def handler(request: web.Request) -> web.FileResponse: f.seek(20) content = f.read(21) assert content == body + + assert body == body2