Skip to content

Commit 026e8d9

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

File tree

4 files changed

+59
-35
lines changed

4 files changed

+59
-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: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# type: ignore
22
import asyncio
3+
import gzip
34
import pathlib
45
import socket
56
import zlib
6-
from typing import Any, Iterable
7+
from typing import Any, Iterable, Optional
78

89
import pytest
910

@@ -16,6 +17,24 @@
1617
ssl = None
1718

1819

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

199218

219+
@pytest.mark.parametrize("hello_txt", ["gzip"], indirect=True)
200220
async def test_static_file_custom_content_type(
201-
aiohttp_client: Any, sender: Any
221+
hello_txt: pathlib.Path, aiohttp_client: Any, sender: Any
202222
) -> None:
203-
filepath = pathlib.Path(__file__).parent / "hello.txt.gz"
223+
"""Test that custom type without encoding is returned for encoded request."""
204224

205225
async def handler(request):
206-
resp = sender(filepath, chunk_size=16)
226+
resp = sender(hello_txt, chunk_size=16)
207227
resp.content_type = "application/pdf"
208228
return resp
209229

@@ -213,24 +233,21 @@ async def handler(request):
213233

214234
resp = await client.get("/")
215235
assert resp.status == 200
216-
body = await resp.read()
217-
with filepath.open("rb") as f:
218-
content = f.read()
219-
assert content == body
220-
assert resp.headers["Content-Type"] == "application/pdf"
221236
assert resp.headers.get("Content-Encoding") is None
237+
assert resp.headers["Content-Type"] == "application/pdf"
238+
assert await resp.read() == hello_txt.read_bytes()
222239
resp.close()
223240
await resp.release()
224241
await client.close()
225242

226243

227244
async def test_static_file_custom_content_type_compress(
228-
aiohttp_client: Any, sender: Any
245+
hello_txt: pathlib.Path, aiohttp_client: Any, sender: Any
229246
):
230-
filepath = pathlib.Path(__file__).parent / "hello.txt"
247+
"""Test that custom type with encoding is returned for unencoded requests."""
231248

232249
async def handler(request):
233-
resp = sender(filepath, chunk_size=16)
250+
resp = sender(hello_txt, chunk_size=16)
234251
resp.content_type = "application/pdf"
235252
return resp
236253

@@ -240,24 +257,26 @@ async def handler(request):
240257

241258
resp = await client.get("/")
242259
assert resp.status == 200
243-
body = await resp.read()
244-
assert b"hello aiohttp\n" == body
245-
assert resp.headers["Content-Type"] == "application/pdf"
246260
assert resp.headers.get("Content-Encoding") == "gzip"
261+
assert resp.headers["Content-Type"] == "application/pdf"
262+
assert await resp.read() == HELLO_AIOHTTP
247263
resp.close()
248264
await resp.release()
249265
await client.close()
250266

251267

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

258277
async def handler(request):
259-
resp = sender(filepath)
260-
resp.enable_compression()
278+
resp = sender(hello_txt)
279+
resp.enable_compression(forced_compression)
261280
return resp
262281

263282
app = web.Application()
@@ -266,35 +285,34 @@ async def handler(request):
266285

267286
resp = await client.get("/")
268287
assert resp.status == 200
269-
body = await resp.read()
270-
assert body == b"hello aiohttp\n"
271-
assert resp.headers["Content-Type"] == "text/plain"
272288
assert resp.headers.get("Content-Encoding") == "gzip"
289+
assert resp.headers["Content-Type"] == "text/plain"
290+
assert await resp.read() == HELLO_AIOHTTP
273291
resp.close()
274292
await resp.release()
275293
await client.close()
276294

277295

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

283304
async def handler(request):
284-
return sender(filepath)
305+
return sender(hello_txt)
285306

286307
app = web.Application()
287308
app.router.add_get("/", handler)
288309
client = await aiohttp_client(app)
289310

290311
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
312+
assert resp.status == 200
313+
assert resp.headers.get("Content-Encoding") == expect_encoding
314+
assert resp.headers["Content-Type"] == "text/plain"
315+
assert await resp.read() == HELLO_AIOHTTP
298316
resp.close()
299317

300318
await resp.release()

0 commit comments

Comments
 (0)