From d780bc04dd0bd4515e154ab02bc240dbcf5844c5 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sat, 19 Dec 2015 18:18:20 -0800 Subject: [PATCH] Fix Tornado support behavior for Tornado 3. Resolves #235. --- .travis.yml | 3 +- tests/integration/test_tornado.py | 64 ++++++++++++++++++++++--------- tox.ini | 11 ++++-- vcr/stubs/tornado_stubs.py | 2 +- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index bc82acf..a40d613 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/tests/integration/test_tornado.py b/tests/integration/test_tornado.py index ba23260..9c082ef 100644 --- a/tests/integration/test_tornado.py +++ b/tests/integration/test_tornado.py @@ -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 074866e..2bf91a0 100644 --- 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] diff --git a/vcr/stubs/tornado_stubs.py b/vcr/stubs/tornado_stubs.py index a6422da..b675065 100644 --- a/vcr/stubs/tornado_stubs.py +++ b/vcr/stubs/tornado_stubs.py @@ -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 )