From 3846a4ccef6a0191882f9053dc861b06e91c5c80 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Fri, 3 Jul 2015 12:32:07 -0700 Subject: [PATCH] [Tornado] Fix unsupported features exception not being raised. Add tests for that exception being raisd correctly and for CannotOverwriteCassetteException. --- tests/integration/test_tornado.py | 72 +++++++++++++++++++++++++++++++ vcr/stubs/tornado_stubs.py | 7 +-- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/tests/integration/test_tornado.py b/tests/integration/test_tornado.py index 6865924..1230077 100644 --- a/tests/integration/test_tornado.py +++ b/tests/integration/test_tornado.py @@ -5,6 +5,7 @@ import json import pytest import vcr +from vcr.errors import CannotOverwriteExistingCassetteException from assertions import assert_cassette_empty, assert_is_json @@ -203,3 +204,74 @@ def test_https_with_cert_validation_disabled(get_client, tmpdir): with vcr.use_cassette(cass_path) as cass: yield get(get_client(), 'https://httpbin.org', validate_cert=False) assert 1 == cass.play_count + + +@pytest.mark.gen_test +def test_unsupported_features_raises_in_future(get_client, tmpdir): + '''Ensure that the exception for an AsyncHTTPClient feature not being + supported is raised inside the future.''' + + def callback(chunk): + assert False, "Did not expect to be called." + + with vcr.use_cassette(str(tmpdir.join('invalid.yaml'))): + future = get( + get_client(), 'http://httpbin.org', streaming_callback=callback + ) + + with pytest.raises(Exception) as excinfo: + yield future + + assert "not yet supported by VCR" in str(excinfo) + + +@pytest.mark.gen_test +def test_unsupported_features_raise_error_disabled(get_client, tmpdir): + '''Ensure that the exception for an AsyncHTTPClient feature not being + supported is not raised if raise_error=False.''' + + def callback(chunk): + assert False, "Did not expect to be called." + + with vcr.use_cassette(str(tmpdir.join('invalid.yaml'))): + response = yield get( + get_client(), + 'http://httpbin.org', + streaming_callback=callback, + raise_error=False, + ) + + assert "not yet supported by VCR" in str(response.error) + + +@pytest.mark.gen_test +def test_cannot_overwrite_cassette_raises_in_future(get_client, tmpdir): + '''Ensure that CannotOverwriteExistingCassetteException is raised inside + the future.''' + + with vcr.use_cassette(str(tmpdir.join('overwrite.yaml'))): + yield get(get_client(), 'http://httpbin.org/get') + + with vcr.use_cassette(str(tmpdir.join('overwrite.yaml'))): + future = get(get_client(), 'http://httpbin.org/headers') + + with pytest.raises(CannotOverwriteExistingCassetteException): + yield future + + +@pytest.mark.gen_test +def test_cannot_overwrite_cassette_raise_error_disabled(get_client, tmpdir): + '''Ensure that CannotOverwriteExistingCassetteException is not raised if + raise_error=False in the fetch() call.''' + + with vcr.use_cassette(str(tmpdir.join('overwrite.yaml'))): + yield get( + get_client(), 'http://httpbin.org/get', raise_error=False + ) + + with vcr.use_cassette(str(tmpdir.join('overwrite.yaml'))): + response = yield get( + get_client(), 'http://httpbin.org/headers', raise_error=False + ) + + assert isinstance(response.error, CannotOverwriteExistingCassetteException) diff --git a/vcr/stubs/tornado_stubs.py b/vcr/stubs/tornado_stubs.py index c6f1bef..7a16cd8 100644 --- a/vcr/stubs/tornado_stubs.py +++ b/vcr/stubs/tornado_stubs.py @@ -65,6 +65,7 @@ class _VCRAsyncClient(object): "request outside a VCR.py context." % repr(request) ), ) + return callback(response) vcr_request = Request( request.method, @@ -90,7 +91,7 @@ class _VCRAsyncClient(object): headers=headers, buffer=BytesIO(vcr_response['body']['string']), ) - callback(response) + return callback(response) else: if self.cassette.write_protected and self.cassette.filter_request( vcr_request @@ -106,7 +107,7 @@ class _VCRAsyncClient(object): self.cassette.record_mode) ), ) - callback(response) + return callback(response) def new_callback(response): headers = [ @@ -123,7 +124,7 @@ class _VCRAsyncClient(object): 'body': {'string': response.body}, } self.cassette.append(vcr_request, vcr_response) - callback(response) + return callback(response) from vcr.patch import force_reset with force_reset():