本文整理汇总了Python中vcr.VCR.use_cassette方法的典型用法代码示例。如果您正苦于以下问题:Python VCR.use_cassette方法的具体用法?Python VCR.use_cassette怎么用?Python VCR.use_cassette使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vcr.VCR
的用法示例。
在下文中一共展示了VCR.use_cassette方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_vcr_before_record_request_params
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_vcr_before_record_request_params():
base_path = 'http://httpbin.org/'
def before_record_cb(request):
if request.path != '/get':
return request
test_vcr = VCR(filter_headers=('cookie',), before_record_request=before_record_cb,
ignore_hosts=('www.test.com',), ignore_localhost=True,
filter_query_parameters=('foo',))
with test_vcr.use_cassette('test') as cassette:
assert cassette.filter_request(Request('GET', base_path + 'get', '', {})) is None
assert cassette.filter_request(Request('GET', base_path + 'get2', '', {})) is not None
assert cassette.filter_request(Request('GET', base_path + '?foo=bar', '', {})).query == []
assert cassette.filter_request(
Request('GET', base_path + '?foo=bar', '',
{'cookie': 'test', 'other': 'fun'})).headers == {'other': 'fun'}
assert cassette.filter_request(Request('GET', base_path + '?foo=bar', '',
{'cookie': 'test', 'other': 'fun'})).headers == {'other': 'fun'}
assert cassette.filter_request(Request('GET', 'http://www.test.com' + '?foo=bar', '',
{'cookie': 'test', 'other': 'fun'})) is None
with test_vcr.use_cassette('test', before_record_request=None) as cassette:
# Test that before_record can be overwritten with
assert cassette.filter_request(Request('GET', base_path + 'get', '', {})) is not None
示例2: test_custom_patchers
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_custom_patchers():
class Test(object):
attribute = None
attribute2 = None
test_vcr = VCR(custom_patches=((Test, 'attribute', VCRHTTPSConnection),))
with test_vcr.use_cassette('custom_patches'):
assert issubclass(Test.attribute, VCRHTTPSConnection)
assert VCRHTTPSConnection is not Test.attribute
with test_vcr.use_cassette('custom_patches', custom_patches=((Test, 'attribute2', VCRHTTPSConnection),)):
assert issubclass(Test.attribute, VCRHTTPSConnection)
assert VCRHTTPSConnection is not Test.attribute
assert Test.attribute is Test.attribute2
示例3: test_vcr_use_cassette
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_vcr_use_cassette():
record_mode = mock.Mock()
test_vcr = VCR(record_mode=record_mode)
with mock.patch(
'vcr.cassette.Cassette.load',
return_value=mock.MagicMock(inject=False)
) as mock_cassette_load:
@test_vcr.use_cassette('test')
def function():
pass
assert mock_cassette_load.call_count == 0
function()
assert mock_cassette_load.call_args[1]['record_mode'] is record_mode
# Make sure that calls to function now use cassettes with the
# new filter_header_settings
test_vcr.record_mode = mock.Mock()
function()
assert mock_cassette_load.call_args[1]['record_mode'] == test_vcr.record_mode
# Ensure that explicitly provided arguments still supercede
# those on the vcr.
new_record_mode = mock.Mock()
with test_vcr.use_cassette('test', record_mode=new_record_mode) as cassette:
assert cassette.record_mode == new_record_mode
示例4: test_vcr_before_record_request_params
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_vcr_before_record_request_params():
base_path = 'http://httpbin.org/'
def before_record_cb(request):
if request.path != '/get':
return request
test_vcr = VCR(filter_headers=('cookie', ('bert', 'ernie')),
before_record_request=before_record_cb,
ignore_hosts=('www.test.com',), ignore_localhost=True,
filter_query_parameters=('foo', ('tom', 'jerry')),
filter_post_data_parameters=('posted', ('no', 'trespassing')))
with test_vcr.use_cassette('test') as cassette:
# Test explicit before_record_cb
request_get = Request('GET', base_path + 'get', '', {})
assert cassette.filter_request(request_get) is None
request = Request('GET', base_path + 'get2', '', {})
assert cassette.filter_request(request) is not None
# Test filter_query_parameters
request = Request('GET', base_path + '?foo=bar', '', {})
assert cassette.filter_request(request).query == []
request = Request('GET', base_path + '?tom=nobody', '', {})
assert cassette.filter_request(request).query == [('tom', 'jerry')]
# Test filter_headers
request = Request('GET', base_path + '?foo=bar', '',
{'cookie': 'test', 'other': 'fun', 'bert': 'nobody'})
assert (cassette.filter_request(request).headers ==
{'other': 'fun', 'bert': 'ernie'})
# Test ignore_hosts
request = Request('GET', 'http://www.test.com' + '?foo=bar', '',
{'cookie': 'test', 'other': 'fun'})
assert cassette.filter_request(request) is None
# Test ignore_localhost
request = Request('GET', 'http://localhost:8000' + '?foo=bar', '',
{'cookie': 'test', 'other': 'fun'})
assert cassette.filter_request(request) is None
with test_vcr.use_cassette('test', before_record_request=None) as cassette:
# Test that before_record can be overwritten in context manager.
assert cassette.filter_request(request_get) is not None
示例5: test_vcr_path_transformer
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_vcr_path_transformer():
# Regression test for #199
# Prevent actually saving the cassette
with mock.patch('vcr.cassette.save_cassette'):
# Baseline: path should be unchanged
vcr = VCR()
with vcr.use_cassette('test') as cassette:
assert cassette._path == 'test'
# Regression test: path_transformer=None should do the same.
vcr = VCR(path_transformer=None)
with vcr.use_cassette('test') as cassette:
assert cassette._path == 'test'
# and it should still work with cassette_library_dir
vcr = VCR(cassette_library_dir='/foo')
with vcr.use_cassette('test') as cassette:
assert cassette._path == '/foo/test'
示例6: test_before_record_response_as_filter
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_before_record_response_as_filter():
request = Request('GET', '/', '', {})
response = object() # just can't be None
# Prevent actually saving the cassette
with mock.patch('vcr.cassette.FilesystemPersister.save_cassette'):
filter_all = mock.Mock(return_value=None)
vcr = VCR(before_record_response=filter_all)
with vcr.use_cassette('test') as cassette:
cassette.append(request, response)
assert cassette.data == []
assert not cassette.dirty
示例7: test_vcr_before_record_request_params
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_vcr_before_record_request_params():
base_path = "http://httpbin.org/"
def before_record_cb(request):
if request.path != "/get":
return request
test_vcr = VCR(
filter_headers=("cookie",),
before_record_request=before_record_cb,
ignore_hosts=("www.test.com",),
ignore_localhost=True,
filter_query_parameters=("foo",),
)
with test_vcr.use_cassette("test") as cassette:
assert cassette.filter_request(Request("GET", base_path + "get", "", {})) is None
assert cassette.filter_request(Request("GET", base_path + "get2", "", {})) is not None
assert cassette.filter_request(Request("GET", base_path + "?foo=bar", "", {})).query == []
assert cassette.filter_request(
Request("GET", base_path + "?foo=bar", "", {"cookie": "test", "other": "fun"})
).headers == {"other": "fun"}
assert cassette.filter_request(
Request("GET", base_path + "?foo=bar", "", {"cookie": "test", "other": "fun"})
).headers == {"other": "fun"}
assert (
cassette.filter_request(
Request("GET", "http://www.test.com" + "?foo=bar", "", {"cookie": "test", "other": "fun"})
)
is None
)
with test_vcr.use_cassette("test", before_record_request=None) as cassette:
# Test that before_record can be overwritten with
assert cassette.filter_request(Request("GET", base_path + "get", "", {})) is not None
示例8: test_vcr_before_record_response_iterable
# 需要导入模块: from vcr import VCR [as 别名]
# 或者: from vcr.VCR import use_cassette [as 别名]
def test_vcr_before_record_response_iterable():
# Regression test for #191
request = Request('GET', '/', '', {})
response = object() # just can't be None
# Prevent actually saving the cassette
with mock.patch('vcr.cassette.save_cassette'):
# Baseline: non-iterable before_record_response should work
mock_filter = mock.Mock()
vcr = VCR(before_record_response=mock_filter)
with vcr.use_cassette('test') as cassette:
assert mock_filter.call_count == 0
cassette.append(request, response)
assert mock_filter.call_count == 1
# Regression test: iterable before_record_response should work too
mock_filter = mock.Mock()
vcr = VCR(before_record_response=(mock_filter,))
with vcr.use_cassette('test') as cassette:
assert mock_filter.call_count == 0
cassette.append(request, response)
assert mock_filter.call_count == 1