mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
filters.py: Make decompression robust towards decompressed input
This commit is contained in:
@@ -298,6 +298,18 @@ def test_decode_response_deflate():
|
|||||||
assert decoded_response["headers"]["content-length"] == [str(len(body))]
|
assert decoded_response["headers"]["content-length"] == [str(len(body))]
|
||||||
|
|
||||||
|
|
||||||
|
def test_decode_response_deflate_already_decompressed():
|
||||||
|
body = b"deflate message"
|
||||||
|
gzip_response = {
|
||||||
|
"body": {"string": body},
|
||||||
|
"headers": {
|
||||||
|
"content-encoding": ["deflate"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
decoded_response = decode_response(gzip_response)
|
||||||
|
assert decoded_response["body"]["string"] == body
|
||||||
|
|
||||||
|
|
||||||
def test_decode_response_gzip():
|
def test_decode_response_gzip():
|
||||||
body = b"gzip message"
|
body = b"gzip message"
|
||||||
|
|
||||||
@@ -325,3 +337,15 @@ def test_decode_response_gzip():
|
|||||||
decoded_response = decode_response(gzip_response)
|
decoded_response = decode_response(gzip_response)
|
||||||
assert decoded_response["body"]["string"] == body
|
assert decoded_response["body"]["string"] == body
|
||||||
assert decoded_response["headers"]["content-length"] == [str(len(body))]
|
assert decoded_response["headers"]["content-length"] == [str(len(body))]
|
||||||
|
|
||||||
|
|
||||||
|
def test_decode_response_gzip_already_decompressed():
|
||||||
|
body = b"gzip message"
|
||||||
|
gzip_response = {
|
||||||
|
"body": {"string": body},
|
||||||
|
"headers": {
|
||||||
|
"content-encoding": ["gzip"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
decoded_response = decode_response(gzip_response)
|
||||||
|
assert decoded_response["body"]["string"] == body
|
||||||
|
|||||||
@@ -153,9 +153,15 @@ def decode_response(response):
|
|||||||
if not body:
|
if not body:
|
||||||
return ""
|
return ""
|
||||||
if encoding == "gzip":
|
if encoding == "gzip":
|
||||||
return zlib.decompress(body, zlib.MAX_WBITS | 16)
|
try:
|
||||||
|
return zlib.decompress(body, zlib.MAX_WBITS | 16)
|
||||||
|
except zlib.error:
|
||||||
|
return body # assumes that the data was already decompressed
|
||||||
else: # encoding == 'deflate'
|
else: # encoding == 'deflate'
|
||||||
return zlib.decompress(body)
|
try:
|
||||||
|
return zlib.decompress(body)
|
||||||
|
except zlib.error:
|
||||||
|
return body # assumes that the data was already decompressed
|
||||||
|
|
||||||
# Deepcopy here in case `headers` contain objects that could
|
# Deepcopy here in case `headers` contain objects that could
|
||||||
# be mutated by a shallow copy and corrupt the real response.
|
# be mutated by a shallow copy and corrupt the real response.
|
||||||
|
|||||||
Reference in New Issue
Block a user