1
1
import os
2
2
import tempfile
3
3
import time
4
+ import zlib
4
5
from unittest import mock
5
6
from urllib .request import urlopen
6
7
15
16
ContentRangeError ,
16
17
Downloader ,
17
18
PARTIAL_CONTENT ,
18
- DECODED_SIZE_NOTE_SUFFIX ,
19
+ DECODED_FROM_SUFFIX ,
19
20
)
20
21
from niquests .structures import CaseInsensitiveDict
21
22
from .utils import http , MockEnvironment , cd_clean_tmp_dir , DUMMY_URL
@@ -232,7 +233,6 @@ def test_download_resumed(self, mock_env, httpbin_both):
232
233
# Ensure `pre_request()` is working as expected too
233
234
headers = {}
234
235
downloader .pre_request (headers )
235
- assert headers ['Accept-Encoding' ] == 'identity'
236
236
assert headers ['Range' ] == 'bytes=3-'
237
237
238
238
downloader .start (
@@ -264,7 +264,7 @@ def test_download_gzip_content_encoding(self, httpbin):
264
264
@responses .activate
265
265
def test_incomplete_response (self ):
266
266
# We have incompleteness checks in the downloader, but it might not be needed as it’s built into (ni|req)uests.
267
- error_msg = 'peer closed connection without sending complete message body (received 2 bytes, expected 1 more)'
267
+ error_msg = 'IncompleteRead( 2 bytes read, 1 more expected )'
268
268
responses .add (
269
269
method = responses .GET ,
270
270
url = DUMMY_URL ,
@@ -281,55 +281,53 @@ def test_incomplete_response(self):
281
281
class TestDecodedDownloads :
282
282
"""Test downloading responses with `Content-Encoding`"""
283
283
284
- # todo: find an appropriate way to mock compressed bodies within those tests.
285
- # @responses.activate
286
- # def test_decoded_response_no_content_length(self):
287
- # responses.add(
288
- # method=responses.GET,
289
- # url=DUMMY_URL,
290
- # headers={
291
- # 'Content-Encoding': 'gzip, br',
292
- # },
293
- # body='123',
294
- # )
295
- # with cd_clean_tmp_dir():
296
- # r = http('--download', '--headers', DUMMY_URL)
297
- # print(r.stderr)
298
- # assert DECODED_FROM_SUFFIX.format(encodings='`gzip`, `br`') in r.stderr
299
- # assert DECODED_SIZE_NOTE_SUFFIX in r.stderr
300
- #
301
- # @responses.activate
302
- # def test_decoded_response_with_content_length(self):
303
- # responses.add(
304
- # method=responses.GET,
305
- # url=DUMMY_URL,
306
- # headers={
307
- # 'Content-Encoding': 'gzip, br',
308
- # 'Content-Length': '3',
309
- # },
310
- # body='123',
311
- # )
312
- # with cd_clean_tmp_dir():
313
- # r = http('--download', DUMMY_URL)
314
- # print(r.stderr)
315
- # assert DECODED_FROM_SUFFIX.format(encodings='`gzip`, `br`') in r.stderr
316
- # assert DECODED_SIZE_NOTE_SUFFIX in r.stderr
317
- #
318
- # @responses.activate
319
- # def test_decoded_response_without_content_length(self):
320
- # responses.add(
321
- # method=responses.GET,
322
- # url=DUMMY_URL,
323
- # headers={
324
- # 'Content-Encoding': 'gzip, br',
325
- # },
326
- # body='123',
327
- # )
328
- # with cd_clean_tmp_dir():
329
- # r = http('--download', DUMMY_URL)
330
- # print(r.stderr)
331
- # assert DECODED_FROM_SUFFIX.format(encodings='`gzip`, `br`') in r.stderr
332
- # assert DECODED_SIZE_NOTE_SUFFIX in r.stderr
284
+ @responses .activate
285
+ def test_decoded_response_no_content_length (self ):
286
+ responses .add (
287
+ method = responses .GET ,
288
+ url = DUMMY_URL ,
289
+ headers = {
290
+ 'Content-Encoding' : 'deflate' ,
291
+ },
292
+ body = zlib .compress (b"foobar" ),
293
+ )
294
+ with cd_clean_tmp_dir ():
295
+ r = http ('--download' , '--headers' , DUMMY_URL )
296
+ print (r .stderr )
297
+ assert DECODED_FROM_SUFFIX .format (encodings = '`deflate`' ) in r .stderr
298
+
299
+ @responses .activate
300
+ def test_decoded_response_with_content_length (self ):
301
+ payload = zlib .compress (b"foobar" )
302
+
303
+ responses .add (
304
+ method = responses .GET ,
305
+ url = DUMMY_URL ,
306
+ headers = {
307
+ 'Content-Encoding' : 'deflate' ,
308
+ 'Content-Length' : str (len (payload )),
309
+ },
310
+ body = payload ,
311
+ )
312
+ with cd_clean_tmp_dir ():
313
+ r = http ('--download' , DUMMY_URL )
314
+ print (r .stderr )
315
+ assert DECODED_FROM_SUFFIX .format (encodings = '`deflate`' ) in r .stderr
316
+
317
+ @responses .activate
318
+ def test_decoded_response_without_content_length (self ):
319
+ responses .add (
320
+ method = responses .GET ,
321
+ url = DUMMY_URL ,
322
+ headers = {
323
+ 'Content-Encoding' : 'deflate' ,
324
+ },
325
+ body = zlib .compress (b'foobar' ),
326
+ )
327
+ with cd_clean_tmp_dir ():
328
+ r = http ('--download' , DUMMY_URL )
329
+ print (r .stderr )
330
+ assert DECODED_FROM_SUFFIX .format (encodings = '`deflate`' ) in r .stderr
333
331
334
332
@responses .activate
335
333
def test_non_decoded_response_without_content_length (self ):
@@ -344,7 +342,6 @@ def test_non_decoded_response_without_content_length(self):
344
342
with cd_clean_tmp_dir ():
345
343
r = http ('--download' , DUMMY_URL )
346
344
print (r .stderr )
347
- assert DECODED_SIZE_NOTE_SUFFIX not in r .stderr
348
345
349
346
@responses .activate
350
347
def test_non_decoded_response_with_content_length (self ):
@@ -358,4 +355,3 @@ def test_non_decoded_response_with_content_length(self):
358
355
with cd_clean_tmp_dir ():
359
356
r = http ('--download' , DUMMY_URL )
360
357
print (r .stderr )
361
- assert DECODED_SIZE_NOTE_SUFFIX not in r .stderr
0 commit comments