Skip to content

Commit ffe8da0

Browse files
steverepwebknjazbdraco
committed
[3.9] Create hello.txt.gz dynamically and improve related assertions (aio-libs#8136)
(cherry picked from commit 026e8d9) Co-authored-by: Steve Repsher <[email protected]> Co-authored-by: Sviatoslav Sydorenko (Святослав Сидоренко) <[email protected]> Co-authored-by: J. Nick Koston <[email protected]>
1 parent d00a32b commit ffe8da0

File tree

4 files changed

+63
-35
lines changed

4 files changed

+63
-35
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
tests/data.unknown_mime_type binary
2-
tests/hello.txt.gz binary
32
tests/sample.* binary

CHANGES/8136.contrib.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
A pytest fixture ``hello_txt`` was introduced to aid
2+
static file serving tests in
3+
:file:`test_web_sendfile_functional.py`. It dynamically
4+
provisions ``hello.txt`` file variants shared across the
5+
tests in the module.
6+
7+
-- by :user:`steverep`

tests/hello.txt.gz

-44 Bytes
Binary file not shown.

tests/test_web_sendfile_functional.py

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import asyncio
2+
import gzip
23
import pathlib
34
import socket
45
import zlib
5-
from typing import Any, Iterable
6+
from typing import Any, Iterable, Optional
67

78
import pytest
89

@@ -15,6 +16,24 @@
1516
ssl = None # type: ignore
1617

1718

19+
HELLO_AIOHTTP = b"Hello aiohttp! :-)\n"
20+
21+
22+
@pytest.fixture(scope="module")
23+
def hello_txt(request, tmp_path_factory) -> pathlib.Path:
24+
"""Create a temp path with hello.txt and compressed versions.
25+
26+
The uncompressed text file path is returned by default. Alternatively, an
27+
indirect parameter can be passed with an encoding to get a compressed path.
28+
"""
29+
txt = tmp_path_factory.mktemp("hello-") / "hello.txt"
30+
hello = {None: txt, "gzip": txt.with_suffix(f"{txt.suffix}.gz")}
31+
hello[None].write_bytes(HELLO_AIOHTTP)
32+
hello["gzip"].write_bytes(gzip.compress(HELLO_AIOHTTP))
33+
encoding = getattr(request, "param", None)
34+
return hello[encoding]
35+
36+
1837
@pytest.fixture
1938
def loop_without_sendfile(loop):
2039
def sendfile(*args, **kwargs):
@@ -201,11 +220,14 @@ async def handler(request):
201220
await client.close()
202221

203222

204-
async def test_static_file_custom_content_type(aiohttp_client, sender) -> None:
205-
filepath = pathlib.Path(__file__).parent / "hello.txt.gz"
223+
@pytest.mark.parametrize("hello_txt", ["gzip"], indirect=True)
224+
async def test_static_file_custom_content_type(
225+
hello_txt: pathlib.Path, aiohttp_client: Any, sender: Any
226+
) -> None:
227+
"""Test that custom type without encoding is returned for encoded request."""
206228

207229
async def handler(request):
208-
resp = sender(filepath, chunk_size=16)
230+
resp = sender(hello_txt, chunk_size=16)
209231
resp.content_type = "application/pdf"
210232
return resp
211233

@@ -215,22 +237,21 @@ async def handler(request):
215237

216238
resp = await client.get("/")
217239
assert resp.status == 200
218-
body = await resp.read()
219-
with filepath.open("rb") as f:
220-
content = f.read()
221-
assert content == body
222-
assert resp.headers["Content-Type"] == "application/pdf"
223240
assert resp.headers.get("Content-Encoding") is None
241+
assert resp.headers["Content-Type"] == "application/pdf"
242+
assert await resp.read() == hello_txt.read_bytes()
224243
resp.close()
225244
await resp.release()
226245
await client.close()
227246

228247

229-
async def test_static_file_custom_content_type_compress(aiohttp_client, sender):
230-
filepath = pathlib.Path(__file__).parent / "hello.txt"
248+
async def test_static_file_custom_content_type_compress(
249+
hello_txt: pathlib.Path, aiohttp_client: Any, sender: Any
250+
):
251+
"""Test that custom type with encoding is returned for unencoded requests."""
231252

232253
async def handler(request):
233-
resp = sender(filepath, chunk_size=16)
254+
resp = sender(hello_txt, chunk_size=16)
234255
resp.content_type = "application/pdf"
235256
return resp
236257

@@ -240,24 +261,26 @@ async def handler(request):
240261

241262
resp = await client.get("/")
242263
assert resp.status == 200
243-
body = await resp.read()
244-
assert b"hello aiohttp\n" == body
245-
assert resp.headers["Content-Type"] == "application/pdf"
246264
assert resp.headers.get("Content-Encoding") == "gzip"
265+
assert resp.headers["Content-Type"] == "application/pdf"
266+
assert await resp.read() == HELLO_AIOHTTP
247267
resp.close()
248268
await resp.release()
249269
await client.close()
250270

251271

252-
async def test_static_file_with_gziped_counter_part_enable_compression(
253-
aiohttp_client: Any, sender: Any
272+
@pytest.mark.parametrize("forced_compression", [None, web.ContentCoding.gzip])
273+
async def test_static_file_with_encoding_and_enable_compression(
274+
hello_txt: pathlib.Path,
275+
aiohttp_client: Any,
276+
sender: Any,
277+
forced_compression: Optional[web.ContentCoding],
254278
):
255-
"""Test that enable_compression does not double compress when a .gz file is also present."""
256-
filepath = pathlib.Path(__file__).parent / "hello.txt"
279+
"""Test that enable_compression does not double compress when an encoded file is also present."""
257280

258281
async def handler(request):
259-
resp = sender(filepath)
260-
resp.enable_compression()
282+
resp = sender(hello_txt)
283+
resp.enable_compression(forced_compression)
261284
return resp
262285

263286
app = web.Application()
@@ -266,35 +289,34 @@ async def handler(request):
266289

267290
resp = await client.get("/")
268291
assert resp.status == 200
269-
body = await resp.read()
270-
assert body == b"hello aiohttp\n"
271-
assert resp.headers["Content-Type"] == "text/plain"
272292
assert resp.headers.get("Content-Encoding") == "gzip"
293+
assert resp.headers["Content-Type"] == "text/plain"
294+
assert await resp.read() == HELLO_AIOHTTP
273295
resp.close()
274296
await resp.release()
275297
await client.close()
276298

277299

300+
@pytest.mark.parametrize(
301+
("hello_txt", "expect_encoding"), [["gzip"] * 2], indirect=["hello_txt"]
302+
)
278303
async def test_static_file_with_content_encoding(
279-
aiohttp_client: Any, sender: Any
304+
hello_txt: pathlib.Path, aiohttp_client: Any, sender: Any, expect_encoding: str
280305
) -> None:
281-
filepath = pathlib.Path(__file__).parent / "hello.txt.gz"
306+
"""Test requesting static compressed files returns the correct content type and encoding."""
282307

283308
async def handler(request):
284-
return sender(filepath)
309+
return sender(hello_txt)
285310

286311
app = web.Application()
287312
app.router.add_get("/", handler)
288313
client = await aiohttp_client(app)
289314

290315
resp = await client.get("/")
291-
assert 200 == resp.status
292-
body = await resp.read()
293-
assert b"hello aiohttp\n" == body
294-
ct = resp.headers["CONTENT-TYPE"]
295-
assert "text/plain" == ct
296-
encoding = resp.headers["CONTENT-ENCODING"]
297-
assert "gzip" == encoding
316+
assert resp.status == 200
317+
assert resp.headers.get("Content-Encoding") == expect_encoding
318+
assert resp.headers["Content-Type"] == "text/plain"
319+
assert await resp.read() == HELLO_AIOHTTP
298320
resp.close()
299321

300322
await resp.release()

0 commit comments

Comments
 (0)