mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
inject_cassette kwarg.
This commit is contained in:
@@ -70,10 +70,11 @@ def test_fixtures_with_use_cassette(random_fixture):
|
|||||||
# problems if the decorator does not preserve the signature of the original
|
# problems if the decorator does not preserve the signature of the original
|
||||||
# test function.
|
# test function.
|
||||||
|
|
||||||
# This test ensures that use_cassette preserves the signature of the original
|
# This test ensures that use_cassette preserves the signature of
|
||||||
# test function, and thus that use_cassette is compatible with py.test
|
# the original test function, and thus that use_cassette is
|
||||||
# fixtures. It is admittedly a bit strange because the test would never even
|
# compatible with py.test fixtures. It is admittedly a bit strange
|
||||||
# run if the relevant feature were broken.
|
# because the test would never even run if the relevant feature
|
||||||
|
# were broken.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@@ -90,3 +91,17 @@ def test_custom_patchers():
|
|||||||
assert issubclass(Test.attribute, VCRHTTPSConnection)
|
assert issubclass(Test.attribute, VCRHTTPSConnection)
|
||||||
assert VCRHTTPSConnection is not Test.attribute
|
assert VCRHTTPSConnection is not Test.attribute
|
||||||
assert Test.attribute is Test.attribute2
|
assert Test.attribute is Test.attribute2
|
||||||
|
|
||||||
|
|
||||||
|
def test_inject_cassette():
|
||||||
|
vcr = VCR(inject_cassette=True)
|
||||||
|
@vcr.use_cassette('test', record_mode='once')
|
||||||
|
def with_cassette_injected(cassette):
|
||||||
|
assert cassette.record_mode == 'once'
|
||||||
|
|
||||||
|
@vcr.use_cassette('test', record_mode='once', inject_cassette=False)
|
||||||
|
def without_cassette_injected():
|
||||||
|
pass
|
||||||
|
|
||||||
|
with_cassette_injected()
|
||||||
|
without_cassette_injected()
|
||||||
|
|||||||
@@ -61,8 +61,11 @@ class CassetteContextDecorator(object):
|
|||||||
|
|
||||||
@wrapt.decorator
|
@wrapt.decorator
|
||||||
def __call__(self, function, instance, args, kwargs):
|
def __call__(self, function, instance, args, kwargs):
|
||||||
with self:
|
with self as cassette:
|
||||||
return function(*args, **kwargs)
|
if cassette.inject:
|
||||||
|
return function(cassette, *args, **kwargs)
|
||||||
|
else:
|
||||||
|
return function(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class Cassette(object):
|
class Cassette(object):
|
||||||
@@ -85,13 +88,15 @@ class Cassette(object):
|
|||||||
|
|
||||||
def __init__(self, path, serializer=yamlserializer, record_mode='once',
|
def __init__(self, path, serializer=yamlserializer, record_mode='once',
|
||||||
match_on=(uri, method), before_record_request=None,
|
match_on=(uri, method), before_record_request=None,
|
||||||
before_record_response=None, custom_patches=()):
|
before_record_response=None, custom_patches=(),
|
||||||
|
inject=False):
|
||||||
|
|
||||||
self._path = path
|
self._path = path
|
||||||
self._serializer = serializer
|
self._serializer = serializer
|
||||||
self._match_on = match_on
|
self._match_on = match_on
|
||||||
self._before_record_request = before_record_request or (lambda x: x)
|
self._before_record_request = before_record_request or (lambda x: x)
|
||||||
self._before_record_response = before_record_response or (lambda x: x)
|
self._before_record_response = before_record_response or (lambda x: x)
|
||||||
|
self.inject = inject
|
||||||
self.record_mode = record_mode
|
self.record_mode = record_mode
|
||||||
self.custom_patches = custom_patches
|
self.custom_patches = custom_patches
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,12 @@ from . import filters
|
|||||||
class VCR(object):
|
class VCR(object):
|
||||||
|
|
||||||
def __init__(self, serializer='yaml', cassette_library_dir=None,
|
def __init__(self, serializer='yaml', cassette_library_dir=None,
|
||||||
record_mode="once", filter_headers=(), custom_patches=(),
|
record_mode="once", filter_headers=(), ignore_localhost=False,
|
||||||
filter_query_parameters=(), filter_post_data_parameters=(),
|
custom_patches=(), filter_query_parameters=(),
|
||||||
before_record_request=None, before_record_response=None,
|
filter_post_data_parameters=(), before_record_request=None,
|
||||||
ignore_hosts=(), ignore_localhost=False, before_record=None,
|
before_record_response=None, ignore_hosts=(),
|
||||||
match_on=('method', 'scheme', 'host', 'port', 'path', 'query')):
|
match_on=('method', 'scheme', 'host', 'port', 'path', 'query'),
|
||||||
|
before_record=None, inject_cassette=False):
|
||||||
self.serializer = serializer
|
self.serializer = serializer
|
||||||
self.match_on = match_on
|
self.match_on = match_on
|
||||||
self.cassette_library_dir = cassette_library_dir
|
self.cassette_library_dir = cassette_library_dir
|
||||||
@@ -44,6 +45,7 @@ class VCR(object):
|
|||||||
self.before_record_response = before_record_response
|
self.before_record_response = before_record_response
|
||||||
self.ignore_hosts = ignore_hosts
|
self.ignore_hosts = ignore_hosts
|
||||||
self.ignore_localhost = ignore_localhost
|
self.ignore_localhost = ignore_localhost
|
||||||
|
self.inject_cassette = inject_cassette
|
||||||
self._custom_patches = tuple(custom_patches)
|
self._custom_patches = tuple(custom_patches)
|
||||||
|
|
||||||
def _get_serializer(self, serializer_name):
|
def _get_serializer(self, serializer_name):
|
||||||
@@ -99,7 +101,8 @@ class VCR(object):
|
|||||||
),
|
),
|
||||||
'custom_patches': self._custom_patches + kwargs.get(
|
'custom_patches': self._custom_patches + kwargs.get(
|
||||||
'custom_patches', ()
|
'custom_patches', ()
|
||||||
)
|
),
|
||||||
|
'inject': kwargs.get('inject_cassette', self.inject_cassette)
|
||||||
}
|
}
|
||||||
return path, merged_config
|
return path, merged_config
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user