1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

[Tornado] Fix unsupported features exception not being raised.

Add tests for that exception being raisd correctly and for
CannotOverwriteCassetteException.
This commit is contained in:
Abhinav Gupta
2015-07-03 12:32:07 -07:00
parent aae4ae255b
commit 3846a4ccef
2 changed files with 76 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ import json
import pytest import pytest
import vcr import vcr
from vcr.errors import CannotOverwriteExistingCassetteException
from assertions import assert_cassette_empty, assert_is_json 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: with vcr.use_cassette(cass_path) as cass:
yield get(get_client(), 'https://httpbin.org', validate_cert=False) yield get(get_client(), 'https://httpbin.org', validate_cert=False)
assert 1 == cass.play_count 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)

View File

@@ -65,6 +65,7 @@ class _VCRAsyncClient(object):
"request outside a VCR.py context." % repr(request) "request outside a VCR.py context." % repr(request)
), ),
) )
return callback(response)
vcr_request = Request( vcr_request = Request(
request.method, request.method,
@@ -90,7 +91,7 @@ class _VCRAsyncClient(object):
headers=headers, headers=headers,
buffer=BytesIO(vcr_response['body']['string']), buffer=BytesIO(vcr_response['body']['string']),
) )
callback(response) return callback(response)
else: else:
if self.cassette.write_protected and self.cassette.filter_request( if self.cassette.write_protected and self.cassette.filter_request(
vcr_request vcr_request
@@ -106,7 +107,7 @@ class _VCRAsyncClient(object):
self.cassette.record_mode) self.cassette.record_mode)
), ),
) )
callback(response) return callback(response)
def new_callback(response): def new_callback(response):
headers = [ headers = [
@@ -123,7 +124,7 @@ class _VCRAsyncClient(object):
'body': {'string': response.body}, 'body': {'string': response.body},
} }
self.cassette.append(vcr_request, vcr_response) self.cassette.append(vcr_request, vcr_response)
callback(response) return callback(response)
from vcr.patch import force_reset from vcr.patch import force_reset
with force_reset(): with force_reset():