From b0a13ba690d043ec297dd194c7cd5310386a86d0 Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Sat, 9 Nov 2013 17:33:33 -1000 Subject: [PATCH] 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. --- tests/assertions.py | 11 +++++++++++ tests/integration/test_requests.py | 22 +++++++++++++++++++++- vcr/stubs/__init__.py | 4 +++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/assertions.py b/tests/assertions.py index c763800..d107c09 100644 --- a/tests/assertions.py +++ b/tests/assertions.py @@ -1,3 +1,6 @@ +import json + + def assert_cassette_empty(cass): assert len(cass) == 0 assert cass.play_count == 0 @@ -6,3 +9,11 @@ def assert_cassette_empty(cass): def assert_cassette_has_one_response(cass): assert len(cass) == 1 assert cass.play_count == 1 + + +def assert_is_json(a_string): + try: + json.loads(a_string) + except Exception: + assert False + assert True diff --git a/tests/integration/test_requests.py b/tests/integration/test_requests.py index 3692db4..b9758b3 100644 --- a/tests/integration/test_requests.py +++ b/tests/integration/test_requests.py @@ -5,7 +5,11 @@ import os import pytest 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") @@ -117,3 +121,19 @@ def test_cross_scheme(tmpdir, scheme): requests.get('http://httpbin.org/') assert cass.play_count == 0 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) diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index 0e50d08..a97da73 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -16,6 +16,7 @@ class VCRHTTPResponse(object): self.status = recorded_response['status']['code'] self.version = None self._content = StringIO(self.recorded_response['body']['string']) + self.closed = False # We are skipping the header parsing (they have already been parsed # at this point) and directly adding the headers to the header @@ -37,12 +38,13 @@ class VCRHTTPResponse(object): return self._content.read(*args, **kwargs) def close(self): + self.closed = True return True def isclosed(self): # Urllib3 seems to call this because it actually uses # the weird chunking support in httplib - return True + return self.closed def getheaders(self): return self.recorded_response['headers'].iteritems()