]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Fix Tornado support behavior for Tornado 3.
authorAbhinav Gupta <mail@abhinavg.net>
Sun, 20 Dec 2015 02:18:20 +0000 (18:18 -0800)
committerAbhinav Gupta <mail@abhinavg.net>
Sun, 20 Dec 2015 02:18:20 +0000 (18:18 -0800)
Resolves #235.

.travis.yml
tests/integration/test_tornado.py
tox.ini
vcr/stubs/tornado_stubs.py

index bc82acf5d926a0b9b2d732391a168136f3e3c095..a40d6136c1f3b0883c96053432b9155055e67360 100644 (file)
@@ -19,7 +19,8 @@ env:
     - TOX_SUFFIX="urllib317"
     - TOX_SUFFIX="urllib319"
     - TOX_SUFFIX="urllib3110"
-    - TOX_SUFFIX="tornado"
+    - TOX_SUFFIX="tornado3"
+    - TOX_SUFFIX="tornado4"
 matrix:
   allow_failures:
     - env: TOX_SUFFIX="boto"
index ba23260c5ade636c05763a2fdb32bee32183e3e3..9c082efff4378b57fc301170f56f933903f59988 100644 (file)
@@ -9,9 +9,13 @@ from vcr.errors import CannotOverwriteExistingCassetteException
 
 from assertions import assert_cassette_empty, assert_is_json
 
-
+tornado = pytest.importorskip("tornado")
 http = pytest.importorskip("tornado.httpclient")
 
+# whether the current version of Tornado supports the raise_error argument for
+# fetch().
+supports_raise_error = tornado.version_info >= (4,)
+
 
 @pytest.fixture(params=['simple', 'curl', 'default'])
 def get_client(request):
@@ -26,10 +30,13 @@ def get_client(request):
 
 
 def get(client, url, **kwargs):
-    raise_error = kwargs.pop('raise_error', True)
+    fetch_kwargs = {}
+    if supports_raise_error:
+        fetch_kwargs['raise_error'] = kwargs.pop('raise_error', True)
+
     return client.fetch(
         http.HTTPRequest(url, method='GET', **kwargs),
-        raise_error=raise_error,
+        **fetch_kwargs
     )
 
 
@@ -122,22 +129,26 @@ def test_auth_failed(get_client, tmpdir, scheme):
     with vcr.use_cassette(str(tmpdir.join('auth-failed.yaml'))) as cass:
         # Ensure that this is empty to begin with
         assert_cassette_empty(cass)
-        one = yield get(
-            get_client(),
-            url,
-            auth_username=auth[0],
-            auth_password=auth[1],
-            raise_error=False
-        )
+        with pytest.raises(http.HTTPError) as exc_info:
+            yield get(
+                get_client(),
+                url,
+                auth_username=auth[0],
+                auth_password=auth[1],
+            )
+        one = exc_info.value.response
+        assert exc_info.value.code == 401
 
     with vcr.use_cassette(str(tmpdir.join('auth-failed.yaml'))) as cass:
-        two = yield get(
-            get_client(),
-            url,
-            auth_username=auth[0],
-            auth_password=auth[1],
-            raise_error=False
-        )
+        with pytest.raises(http.HTTPError) as exc_info:
+            two = yield get(
+                get_client(),
+                url,
+                auth_username=auth[0],
+                auth_password=auth[1],
+            )
+        two = exc_info.value.response
+        assert exc_info.value.code == 401
         assert one.body == two.body
         assert one.code == two.code == 401
         assert 1 == cass.play_count
@@ -197,12 +208,19 @@ def test_gzip(get_client, tmpdir, scheme):
     '''
     url = scheme + '://httpbin.org/gzip'
 
+    # use_gzip was renamed to decompress_response in 4.0
+    kwargs = {}
+    if tornado.version_info < (4,):
+        kwargs['use_gzip'] = True
+    else:
+        kwargs['decompress_response'] = True
+
     with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))):
-        response = yield get(get_client(), url, decompress_response=True)
+        response = yield get(get_client(), url, **kwargs)
         assert_is_json(response.body)
 
     with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))) as cass:
-        response = yield get(get_client(), url, decompress_response=True)
+        response = yield get(get_client(), url, **kwargs)
         assert_is_json(response.body)
         assert 1 == cass.play_count
 
@@ -238,6 +256,10 @@ def test_unsupported_features_raises_in_future(get_client, tmpdir):
     assert "not yet supported by VCR" in str(excinfo)
 
 
+@pytest.mark.skipif(
+    not supports_raise_error,
+    reason='raise_error unavailable in tornado <= 3',
+)
 @pytest.mark.gen_test
 def test_unsupported_features_raise_error_disabled(get_client, tmpdir):
     '''Ensure that the exception for an AsyncHTTPClient feature not being
@@ -272,6 +294,10 @@ def test_cannot_overwrite_cassette_raises_in_future(get_client, tmpdir):
         yield future
 
 
+@pytest.mark.skipif(
+    not supports_raise_error,
+    reason='raise_error unavailable in tornado <= 3',
+)
 @pytest.mark.gen_test
 def test_cannot_overwrite_cassette_raise_error_disabled(get_client, tmpdir):
     '''Ensure that CannotOverwriteExistingCassetteException is not raised if
diff --git a/tox.ini b/tox.ini
index 074866ef3efd1ea8a1ee4359298370b30172b480..2bf91a03682296d892b2d8bd83f83f92ad13a0af 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist = {py26,py27,py33,py34,pypy}-{flakes,requests27,requests26,requests25,requests24,requests23,requests22,requests1,httplib2,urllib317,urllib319,urllib3110,tornado,boto}
+envlist = {py26,py27,py33,py34,pypy}-{flakes,requests27,requests26,requests25,requests24,requests23,requests22,requests1,httplib2,urllib317,urllib319,urllib3110,tornado3,tornado4,boto}
 
 [testenv:flakes]
 skipsdist = True
@@ -28,9 +28,12 @@ deps =
     urllib317: urllib3==1.7.1
     urllib319: urllib3==1.9.1
     urllib3110: urllib3==1.10.2
-    {py26,py27,py33,py34,pypy}-tornado: tornado
-    {py26,py27,py33,py34,pypy}-tornado: pytest-tornado
-    {py26,py27,py33,py34}-tornado: pycurl
+    {py26,py27,py33,py34,pypy}-tornado3: tornado>=3,<4
+    {py26,py27,py33,py34,pypy}-tornado4: tornado>=4,<5
+    {py26,py27,py33,py34,pypy}-tornado3: pytest-tornado
+    {py26,py27,py33,py34,pypy}-tornado4: pytest-tornado
+    {py26,py27,py33,py34}-tornado3: pycurl
+    {py26,py27,py33,py34}-tornado4: pycurl
     boto: boto
 
 [flake8]
index a6422da553dcb8a421941fda4a5e3a37e088ae24..b6750650fe6d145cf5321dcad1ed86c577353a9b 100644 (file)
@@ -23,7 +23,7 @@ def vcr_fetch_impl(cassette, real_fetch_impl):
         # yet supported.
 
         unsupported_call = (
-            request.body_producer is not None or
+            getattr(request, 'body_producer', None) is not None or
             request.header_callback is not None or
             request.streaming_callback is not None
         )