def test_decompress_gzip(tmpdir):
url = 'http://httpbin.org/gzip'
+ request = Request(url, headers={'Accept-Encoding': ['gzip, deflate']})
cass_file = str(tmpdir.join('gzip_response.yaml'))
with vcr.use_cassette(cass_file, decode_compressed_response=True):
- urlopen(url)
+ urlopen(request)
with vcr.use_cassette(cass_file) as cass:
decoded_response = urlopen(url).read()
assert_cassette_has_one_response(cass)
def test_decompress_deflate(tmpdir):
url = 'http://httpbin.org/deflate'
+ request = Request(url, headers={'Accept-Encoding': ['gzip, deflate']})
cass_file = str(tmpdir.join('deflate_response.yaml'))
with vcr.use_cassette(cass_file, decode_compressed_response=True):
- urlopen(url)
+ urlopen(request)
with vcr.use_cassette(cass_file) as cass:
decoded_response = urlopen(url).read()
assert_cassette_has_one_response(cass)
+from six import BytesIO
from vcr.filters import (
remove_headers, replace_headers,
remove_query_parameters, replace_query_parameters,
)
from vcr.compat import mock
from vcr.request import Request
-import cStringIO
import gzip
import json
import zlib
def test_decode_response_deflate():
- body = 'deflate message'
+ body = b'deflate message'
deflate_response = {
'body': {'string': zlib.compress(body)},
'headers': {
}
decoded_response = decode_response(deflate_response)
assert decoded_response['body']['string'] == body
- assert decoded_response['content-length'] == len(body)
+ assert decoded_response['headers']['content-length'] == [str(len(body))]
def test_decode_response_gzip():
- body = 'gzip message'
- buf = cStringIO.StringIO()
- with gzip.GzipFile('a', fileobj=buf, mode='wb') as f:
- f.write(body)
+ body = b'gzip message'
+
+ buf = BytesIO()
+ f = gzip.GzipFile('a', fileobj=buf, mode='wb')
+ f.write(body)
+ f.close()
+
compressed_body = buf.getvalue()
buf.close()
gzip_response = {
}
decoded_response = decode_response(gzip_response)
assert decoded_response['body']['string'] == body
- assert decoded_response['content-length'] == len(body)
+ assert decoded_response['headers']['content-length'] == [str(len(body))]
from six import BytesIO, text_type
from six.moves.urllib.parse import urlparse, urlencode, urlunparse
+import copy
import json
import zlib
+from .util import CaseInsensitiveDict
+
def replace_headers(request, replacements):
"""
2. delete the content-encoding header
3. update content-length header to decompressed length
"""
- def is_compressed(response):
- encoding = response['headers'].get('content-encoding', [])
+ def is_compressed(headers):
+ encoding = headers.get('content-encoding', [])
return encoding and encoding[0] in ('gzip', 'deflate')
def decompress_body(body, encoding):
else: # encoding == 'deflate'
return zlib.decompress(body)
- if is_compressed(response):
- response = response.copy()
- encoding = response['headers']['content-encoding'][0]
-
- response['headers']['content-encoding'].remove(encoding)
- if not response['headers']['content-encoding']:
- del response['headers']['content-encoding']
+ headers = CaseInsensitiveDict(response['headers'])
+ if is_compressed(headers):
+ response = copy.deepcopy(response)
+ encoding = headers['content-encoding'][0]
+ headers['content-encoding'].remove(encoding)
+ if not headers['content-encoding']:
+ del headers['content-encoding']
new_body = decompress_body(response['body']['string'], encoding)
response['body']['string'] = new_body
- response['content-length'] = len(new_body)
+ headers['content-length'] = [str(len(new_body))]
+ response['headers'] = dict(headers)
return response