1
1
import asyncio
2
+ import gzip
2
3
import pathlib
3
4
import socket
4
5
import zlib
5
- from typing import Any , Iterable
6
+ from typing import Any , Iterable , Optional
6
7
7
8
import pytest
8
9
15
16
ssl = None # type: ignore
16
17
17
18
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
+
18
37
@pytest .fixture
19
38
def loop_without_sendfile (loop ):
20
39
def sendfile (* args , ** kwargs ):
@@ -201,11 +220,14 @@ async def handler(request):
201
220
await client .close ()
202
221
203
222
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."""
206
228
207
229
async def handler (request ):
208
- resp = sender (filepath , chunk_size = 16 )
230
+ resp = sender (hello_txt , chunk_size = 16 )
209
231
resp .content_type = "application/pdf"
210
232
return resp
211
233
@@ -215,22 +237,21 @@ async def handler(request):
215
237
216
238
resp = await client .get ("/" )
217
239
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"
223
240
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 ()
224
243
resp .close ()
225
244
await resp .release ()
226
245
await client .close ()
227
246
228
247
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."""
231
252
232
253
async def handler (request ):
233
- resp = sender (filepath , chunk_size = 16 )
254
+ resp = sender (hello_txt , chunk_size = 16 )
234
255
resp .content_type = "application/pdf"
235
256
return resp
236
257
@@ -240,24 +261,26 @@ async def handler(request):
240
261
241
262
resp = await client .get ("/" )
242
263
assert resp .status == 200
243
- body = await resp .read ()
244
- assert b"hello aiohttp\n " == body
245
- assert resp .headers ["Content-Type" ] == "application/pdf"
246
264
assert resp .headers .get ("Content-Encoding" ) == "gzip"
265
+ assert resp .headers ["Content-Type" ] == "application/pdf"
266
+ assert await resp .read () == HELLO_AIOHTTP
247
267
resp .close ()
248
268
await resp .release ()
249
269
await client .close ()
250
270
251
271
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 ],
254
278
):
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."""
257
280
258
281
async def handler (request ):
259
- resp = sender (filepath )
260
- resp .enable_compression ()
282
+ resp = sender (hello_txt )
283
+ resp .enable_compression (forced_compression )
261
284
return resp
262
285
263
286
app = web .Application ()
@@ -266,35 +289,34 @@ async def handler(request):
266
289
267
290
resp = await client .get ("/" )
268
291
assert resp .status == 200
269
- body = await resp .read ()
270
- assert body == b"hello aiohttp\n "
271
- assert resp .headers ["Content-Type" ] == "text/plain"
272
292
assert resp .headers .get ("Content-Encoding" ) == "gzip"
293
+ assert resp .headers ["Content-Type" ] == "text/plain"
294
+ assert await resp .read () == HELLO_AIOHTTP
273
295
resp .close ()
274
296
await resp .release ()
275
297
await client .close ()
276
298
277
299
300
+ @pytest .mark .parametrize (
301
+ ("hello_txt" , "expect_encoding" ), [["gzip" ] * 2 ], indirect = ["hello_txt" ]
302
+ )
278
303
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
280
305
) -> None :
281
- filepath = pathlib . Path ( __file__ ). parent / "hello.txt.gz "
306
+ """Test requesting static compressed files returns the correct content type and encoding."" "
282
307
283
308
async def handler (request ):
284
- return sender (filepath )
309
+ return sender (hello_txt )
285
310
286
311
app = web .Application ()
287
312
app .router .add_get ("/" , handler )
288
313
client = await aiohttp_client (app )
289
314
290
315
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
298
320
resp .close ()
299
321
300
322
await resp .release ()
0 commit comments