]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Fix Requests so it can gunzip the request body
authorKevin McCarthy <me@kevinmccarthy.org>
Sun, 10 Nov 2013 03:33:33 +0000 (17:33 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sun, 10 Nov 2013 03:51:29 +0000 (17:51 -1000)
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
tests/integration/test_requests.py
vcr/stubs/__init__.py

index c76380032def45c352423b750b1f715064a8a163..d107c095cab57bbc2894d31f66c4b234cf349fe3 100644 (file)
@@ -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
index 3692db4bbed76660b4d5f19f87c1158cbd9436aa..b9758b393500072ae25ee09a3a286d3d151a9293 100644 (file)
@@ -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)
index 0e50d08c91ee0a0e55b2964e39c35486f140eb8e..a97da73d1d0485514940d102f822bda63205d871 100644 (file)
@@ -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()