本文整理汇总了Python中vcr.cassette.Cassette类的典型用法代码示例。如果您正苦于以下问题:Python Cassette类的具体用法?Python Cassette怎么用?Python Cassette使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Cassette类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_before_record_response
def test_before_record_response():
before_record_response = mock.Mock(return_value='mutated')
cassette = Cassette('test', before_record_response=before_record_response)
cassette.append('req', 'res')
before_record_response.assert_called_once_with('res')
assert cassette.responses[0] == 'mutated'
示例2: test_serialize_cassette
def test_serialize_cassette(self):
c1 = Cassette()
c1.requests = ['a', 'b', 'c']
c1.responses = ['d', 'e', 'f']
ser = c1.serialize()
c2 = Cassette(ser)
self.assertEqual(c1.requests, c2.requests)
self.assertEqual(c1.responses, c2.responses)
示例3: test_nesting_context_managers_by_checking_references_of_http_connection
def test_nesting_context_managers_by_checking_references_of_http_connection():
original = httplib.HTTPConnection
with Cassette.use(path='test'):
first_cassette_HTTPConnection = httplib.HTTPConnection
with Cassette.use(path='test'):
second_cassette_HTTPConnection = httplib.HTTPConnection
assert second_cassette_HTTPConnection is not first_cassette_HTTPConnection
with Cassette.use(path='test'):
assert httplib.HTTPConnection is not second_cassette_HTTPConnection
with force_reset():
assert httplib.HTTPConnection is original
assert httplib.HTTPConnection is second_cassette_HTTPConnection
assert httplib.HTTPConnection is first_cassette_HTTPConnection
示例4: test_cassette_load
def test_cassette_load(tmpdir):
a_file = tmpdir.join('test_cassette.yml')
a_file.write(yaml.dump([
{'request': 'foo', 'response': 'bar'}
]))
a_cassette = Cassette.load(str(a_file))
assert len(a_cassette) == 1
示例5: test_cassette_load
def test_cassette_load(tmpdir):
a_file = tmpdir.join('test_cassette.yml')
a_file.write(yaml.dump({'interactions': [
{'request': {'body': '', 'uri': 'foo', 'method': 'GET', 'headers': {}},
'response': 'bar'}
]}))
a_cassette = Cassette.load(path=str(a_file))
assert len(a_cassette) == 1
示例6: test_custom_patchers
def test_custom_patchers():
class Test(object):
attribute = None
with Cassette.use(path="custom_patches", custom_patches=((Test, "attribute", VCRHTTPSConnection),)):
assert issubclass(Test.attribute, VCRHTTPSConnection)
assert VCRHTTPSConnection is not Test.attribute
old_attribute = Test.attribute
with Cassette.use(path="custom_patches", custom_patches=((Test, "attribute", VCRHTTPSConnection),)):
assert issubclass(Test.attribute, VCRHTTPSConnection)
assert VCRHTTPSConnection is not Test.attribute
assert Test.attribute is not old_attribute
assert issubclass(Test.attribute, VCRHTTPSConnection)
assert VCRHTTPSConnection is not Test.attribute
assert Test.attribute is old_attribute
示例7: test_nesting_cassette_context_managers
def test_nesting_cassette_context_managers(*args):
first_response = {"body": {"string": b"first_response"}, "headers": {}, "status": {"message": "m", "code": 200}}
second_response = copy.deepcopy(first_response)
second_response["body"]["string"] = b"second_response"
with contextlib.ExitStack() as exit_stack:
first_cassette = exit_stack.enter_context(Cassette.use(path="test"))
exit_stack.enter_context(mock.patch.object(first_cassette, "play_response", return_value=first_response))
assert_get_response_body_is("first_response")
# Make sure a second cassette can supercede the first
with Cassette.use(path="test") as second_cassette:
with mock.patch.object(second_cassette, "play_response", return_value=second_response):
assert_get_response_body_is("second_response")
# Now the first cassette should be back in effect
assert_get_response_body_is("first_response")
示例8: test_nesting_cassette_context_managers
def test_nesting_cassette_context_managers(*args):
first_response = {'body': {'string': b'first_response'}, 'headers': {},
'status': {'message': 'm', 'code': 200}}
second_response = copy.deepcopy(first_response)
second_response['body']['string'] = b'second_response'
with contextlib.ExitStack() as exit_stack:
first_cassette = exit_stack.enter_context(Cassette.use(path='test'))
exit_stack.enter_context(mock.patch.object(first_cassette, 'play_response',
return_value=first_response))
assert_get_response_body_is('first_response')
# Make sure a second cassette can supercede the first
with Cassette.use(path='test') as second_cassette:
with mock.patch.object(second_cassette, 'play_response', return_value=second_response):
assert_get_response_body_is('second_response')
# Now the first cassette should be back in effect
assert_get_response_body_is('first_response')
示例9: test_cassette_load
def test_cassette_load(tmpdir):
a_file = tmpdir.join("test_cassette.yml")
a_file.write(
yaml.dump(
{
"interactions": [
{"request": {"body": "", "uri": "foo", "method": "GET", "headers": {}}, "response": "bar"}
]
}
)
)
a_cassette = Cassette.load(path=str(a_file))
assert len(a_cassette) == 1
示例10: test_arg_getter_functionality
def test_arg_getter_functionality():
arg_getter = mock.Mock(return_value={"path": "test"})
context_decorator = Cassette.use_arg_getter(arg_getter)
with context_decorator as cassette:
assert cassette._path == "test"
arg_getter.return_value = {"path": "other"}
with context_decorator as cassette:
assert cassette._path == "other"
arg_getter.return_value = {"path": "other", "filter_headers": ("header_name",)}
@context_decorator
def function():
pass
with mock.patch.object(Cassette, "load", return_value=mock.MagicMock(inject=False)) as cassette_load:
function()
cassette_load.assert_called_once_with(**arg_getter.return_value)
示例11: test_arg_getter_functionality
def test_arg_getter_functionality():
arg_getter = mock.Mock(return_value={'path': 'test'})
context_decorator = Cassette.use_arg_getter(arg_getter)
with context_decorator as cassette:
assert cassette._path == 'test'
arg_getter.return_value = {'path': 'other'}
with context_decorator as cassette:
assert cassette._path == 'other'
arg_getter.return_value = {'path': 'other', 'filter_headers': ('header_name',)}
@context_decorator
def function():
pass
with mock.patch.object(
Cassette, 'load',
return_value=mock.MagicMock(inject=False)
) as cassette_load:
function()
cassette_load.assert_called_once_with(**arg_getter.return_value)
示例12: test_cassette_cant_read_same_request_twice
def test_cassette_cant_read_same_request_twice():
a = Cassette('test')
a.append('foo', 'bar')
a.play_response('foo')
with pytest.raises(UnhandledHTTPRequestError):
a.play_response('foo')
示例13: test_cassette_responses_of
def test_cassette_responses_of():
a = Cassette('test')
a.append('foo', 'bar')
assert a.responses_of('foo') == ['bar']
示例14: test_cassette_get_missing_response
def test_cassette_get_missing_response():
a = Cassette('test')
with pytest.raises(UnhandledHTTPRequestError):
a.responses_of('foo')
示例15: test_cassette_len
def test_cassette_len():
a = Cassette('test')
a.append('foo', 'bar')
a.append('foo2', 'bar2')
assert len(a) == 2