1
1
# type: ignore
2
2
import asyncio
3
+ import gzip
3
4
import pathlib
4
5
import socket
5
6
import zlib
6
- from typing import Any , Iterable
7
+ from typing import Any , Iterable , Optional
7
8
8
9
import pytest
9
10
16
17
ssl = None
17
18
18
19
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
+
19
38
@pytest .fixture
20
39
def loop_without_sendfile (loop : Any ):
21
40
def sendfile (* args , ** kwargs ):
@@ -197,13 +216,14 @@ async def handler(request):
197
216
await client .close ()
198
217
199
218
219
+ @pytest .mark .parametrize ("hello_txt" , ["gzip" ], indirect = True )
200
220
async def test_static_file_custom_content_type (
201
- aiohttp_client : Any , sender : Any
221
+ hello_txt : pathlib . Path , aiohttp_client : Any , sender : Any
202
222
) -> None :
203
- filepath = pathlib . Path ( __file__ ). parent / "hello.txt.gz "
223
+ """Test that custom type without encoding is returned for encoded request."" "
204
224
205
225
async def handler (request ):
206
- resp = sender (filepath , chunk_size = 16 )
226
+ resp = sender (hello_txt , chunk_size = 16 )
207
227
resp .content_type = "application/pdf"
208
228
return resp
209
229
@@ -213,24 +233,21 @@ async def handler(request):
213
233
214
234
resp = await client .get ("/" )
215
235
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"
221
236
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 ()
222
239
resp .close ()
223
240
await resp .release ()
224
241
await client .close ()
225
242
226
243
227
244
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
229
246
):
230
- filepath = pathlib . Path ( __file__ ). parent / "hello.txt "
247
+ """Test that custom type with encoding is returned for unencoded requests."" "
231
248
232
249
async def handler (request ):
233
- resp = sender (filepath , chunk_size = 16 )
250
+ resp = sender (hello_txt , chunk_size = 16 )
234
251
resp .content_type = "application/pdf"
235
252
return resp
236
253
@@ -240,24 +257,26 @@ async def handler(request):
240
257
241
258
resp = await client .get ("/" )
242
259
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
260
assert resp .headers .get ("Content-Encoding" ) == "gzip"
261
+ assert resp .headers ["Content-Type" ] == "application/pdf"
262
+ assert await resp .read () == HELLO_AIOHTTP
247
263
resp .close ()
248
264
await resp .release ()
249
265
await client .close ()
250
266
251
267
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 ],
254
274
):
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."""
257
276
258
277
async def handler (request ):
259
- resp = sender (filepath )
260
- resp .enable_compression ()
278
+ resp = sender (hello_txt )
279
+ resp .enable_compression (forced_compression )
261
280
return resp
262
281
263
282
app = web .Application ()
@@ -266,35 +285,34 @@ async def handler(request):
266
285
267
286
resp = await client .get ("/" )
268
287
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
288
assert resp .headers .get ("Content-Encoding" ) == "gzip"
289
+ assert resp .headers ["Content-Type" ] == "text/plain"
290
+ assert await resp .read () == HELLO_AIOHTTP
273
291
resp .close ()
274
292
await resp .release ()
275
293
await client .close ()
276
294
277
295
296
+ @pytest .mark .parametrize (
297
+ ("hello_txt" , "expect_encoding" ), [["gzip" ] * 2 ], indirect = ["hello_txt" ]
298
+ )
278
299
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
280
301
) -> None :
281
- filepath = pathlib . Path ( __file__ ). parent / "hello.txt.gz "
302
+ """Test requesting static compressed files returns the correct content type and encoding."" "
282
303
283
304
async def handler (request ):
284
- return sender (filepath )
305
+ return sender (hello_txt )
285
306
286
307
app = web .Application ()
287
308
app .router .add_get ("/" , handler )
288
309
client = await aiohttp_client (app )
289
310
290
311
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
298
316
resp .close ()
299
317
300
318
await resp .release ()
0 commit comments