mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-11 10:03:00 +00:00
Fix Requests so it can gunzip the request body
I wasn't emulating the stateful file-object in my response stub, so urllib3 wasn't decompressing gzipped bodies properly. This should fix that problem. Thanks @bryanhelmig for the motivation to dig into this.
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
def assert_cassette_empty(cass):
|
def assert_cassette_empty(cass):
|
||||||
assert len(cass) == 0
|
assert len(cass) == 0
|
||||||
assert cass.play_count == 0
|
assert cass.play_count == 0
|
||||||
@@ -6,3 +9,11 @@ def assert_cassette_empty(cass):
|
|||||||
def assert_cassette_has_one_response(cass):
|
def assert_cassette_has_one_response(cass):
|
||||||
assert len(cass) == 1
|
assert len(cass) == 1
|
||||||
assert cass.play_count == 1
|
assert cass.play_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
def assert_is_json(a_string):
|
||||||
|
try:
|
||||||
|
json.loads(a_string)
|
||||||
|
except Exception:
|
||||||
|
assert False
|
||||||
|
assert True
|
||||||
|
|||||||
@@ -5,7 +5,11 @@
|
|||||||
import os
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
import vcr
|
import vcr
|
||||||
from assertions import assert_cassette_empty, assert_cassette_has_one_response
|
from assertions import (
|
||||||
|
assert_cassette_empty,
|
||||||
|
assert_cassette_has_one_response,
|
||||||
|
assert_is_json
|
||||||
|
)
|
||||||
requests = pytest.importorskip("requests")
|
requests = pytest.importorskip("requests")
|
||||||
|
|
||||||
|
|
||||||
@@ -117,3 +121,19 @@ def test_cross_scheme(tmpdir, scheme):
|
|||||||
requests.get('http://httpbin.org/')
|
requests.get('http://httpbin.org/')
|
||||||
assert cass.play_count == 0
|
assert cass.play_count == 0
|
||||||
assert len(cass) == 2
|
assert len(cass) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_gzip(tmpdir, scheme):
|
||||||
|
'''
|
||||||
|
Ensure that requests (actually urllib3) is able to automatically decompress
|
||||||
|
the response body
|
||||||
|
'''
|
||||||
|
url = scheme + '://httpbin.org/gzip'
|
||||||
|
response = requests.get(url)
|
||||||
|
|
||||||
|
with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))) as cass:
|
||||||
|
response = requests.get(url)
|
||||||
|
assert_is_json(response.content)
|
||||||
|
|
||||||
|
with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))) as cass:
|
||||||
|
assert_is_json(response.content)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class VCRHTTPResponse(object):
|
|||||||
self.status = recorded_response['status']['code']
|
self.status = recorded_response['status']['code']
|
||||||
self.version = None
|
self.version = None
|
||||||
self._content = StringIO(self.recorded_response['body']['string'])
|
self._content = StringIO(self.recorded_response['body']['string'])
|
||||||
|
self.closed = False
|
||||||
|
|
||||||
# We are skipping the header parsing (they have already been parsed
|
# We are skipping the header parsing (they have already been parsed
|
||||||
# at this point) and directly adding the headers to the header
|
# at this point) and directly adding the headers to the header
|
||||||
@@ -37,12 +38,13 @@ class VCRHTTPResponse(object):
|
|||||||
return self._content.read(*args, **kwargs)
|
return self._content.read(*args, **kwargs)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
self.closed = True
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def isclosed(self):
|
def isclosed(self):
|
||||||
# Urllib3 seems to call this because it actually uses
|
# Urllib3 seems to call this because it actually uses
|
||||||
# the weird chunking support in httplib
|
# the weird chunking support in httplib
|
||||||
return True
|
return self.closed
|
||||||
|
|
||||||
def getheaders(self):
|
def getheaders(self):
|
||||||
return self.recorded_response['headers'].iteritems()
|
return self.recorded_response['headers'].iteritems()
|
||||||
|
|||||||
Reference in New Issue
Block a user