本文整理汇总了Python中mock.Mock.request方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.request方法的具体用法?Python Mock.request怎么用?Python Mock.request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_init
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_init(self):
requests = Mock()
requests.request = Mock(return_value=self.response_login)
with patch("cloudify_vsphere.contentlibrary.requests", requests):
# correct session id
cl = contentlibrary.ContentLibrary({'host': 'host',
'username': 'username',
'password': 'password',
'allow_insecure': True})
cl.__del__()
# wrong session id
response = Mock()
response.json = Mock(return_value={"value": 'other_id'})
response.cookies = {'vmware-api-session-id': 'session_id'}
requests.request = Mock(return_value=response)
with self.assertRaises(NonRecoverableError):
contentlibrary.ContentLibrary({'host': 'host',
'username': 'username',
'password': 'password',
'allow_insecure': True})
# no response
response = Mock()
response.json = Mock(return_value={})
response.cookies = {}
requests.request = Mock(return_value=response)
with self.assertRaises(NonRecoverableError):
contentlibrary.ContentLibrary({'host': 'host',
'username': 'username',
'password': 'password',
'allow_insecure': True})
示例2: test_photo_queryset
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_photo_queryset(self):
viewset_mock = Mock(spec=PhotoViewSet)
viewset_mock.request = Mock(user=self.user2)
self.assertEqual(len(PhotoViewSet.get_queryset(viewset_mock)), 2)
self.contest.publish_date = date.today() - timedelta(days=1)
self.contest.save()
viewset_mock.request = Mock(user=self.user2)
self.assertEqual(len(PhotoViewSet.get_queryset(viewset_mock)), 3)
示例3: test_return_correct_body_for_multiple_response
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_return_correct_body_for_multiple_response(self):
item1 = Mock(file_name="file1")
item1.request = "request1"
item2 = Mock(file_name="file2")
item2.request = "request2"
# item2.title = Mock(return_value="title2")
mapper = CherryPyMapper()
mapper.cherrypy = Mock()
mapper.cherrypy.url = Mock(return_value="some url")
body = log.log_multiple_matches([item1, item2])
self.assertEqual(body, "Matched 2 items, choosing the first one\n- file1\nrequest1\n\n- file2\nrequest2\n")
示例4: test_cleanup_parmeters
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_cleanup_parmeters(self):
_responses = [self.response_logout,
self.response_login]
def _fake_response(*argc, **kwargs):
return _responses.pop()
requests = Mock()
requests.request = _fake_response
# check correct deployment
with patch("cloudify_vsphere.contentlibrary.requests", requests):
cl = contentlibrary.ContentLibrary({'host': 'host',
'username': 'username',
'password': 'password',
'allow_insecure': True})
self.assertEqual(
cl._cleanup_specs({"additional_parameters": [{
"type": "DeploymentOptionParams",
"@class": "com.vmware.vcenter.ovf.deployment_option_params"
}]}),
{'additional_parameters': [collections.OrderedDict([(
# class should be always first in list
'@class', 'com.vmware.vcenter.ovf.deployment_option_params'
), (
'type', 'DeploymentOptionParams'
)])]}
)
cl.__del__()
示例5: test_should_not_update_state_if_unaavailable
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_should_not_update_state_if_unaavailable(self):
# Set
app = NewrelicApp('1111','777')
headers = {"X-Api-Key":'1111'}
url ='/v2/applications/777.json'
# Mocks
connection_obj = Mock()
response_obj = Mock()
httplib.HTTPConnection = Mock(return_value=connection_obj)
response_obj.status = 500 # Error
response_obj.read = Mock(return_value='')
connection_obj.connect = Mock()
connection_obj.request = Mock()
connection_obj.getresponse = Mock(return_value=response_obj)
# Assertion
app.update_state()
connection_obj.connect.assert_called_once()
connection_obj.request.assert_called_once_with('GET', url, '', headers)
connection_obj.getresponse.assert_called_once()
self.assertFalse(app.reachable)
示例6: test_concurrency_no_auth
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_concurrency_no_auth(self):
lock = Lock()
calls = set()
items = []
def add_item(method, url, **kwargs):
thread = kwargs['thread']
with lock:
if items:
other = items.pop()
ok_(other != thread)
else:
items.append(thread)
sleep(0.25)
with lock:
ok_(not items)
calls.add(thread)
return Mock()
session = Mock()
session.request = add_item
client = KerberosClient('http://nn', max_concurrency=1, session=session)
args = ('POST', 'http://foo')
t1 = Thread(
target=client._request,
args=args,
kwargs={'thread': 1, 'auth': True}
)
t1.start()
t2 = Thread(target=client._request, args=args, kwargs={'thread': 2})
t2.start()
t1.join()
t2.join()
eq_(calls, set([1, 2]))
示例7: test_content_item_deploy_fail
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_content_item_deploy_fail(self):
# failed deployment
response_deployment_state = Mock()
response_deployment_state.cookies = {}
response_deployment_state.json = Mock(return_value={"value": {
'name': 'def',
'id': 'id'}})
response_failed_deployment = Mock()
response_failed_deployment.cookies = {}
response_failed_deployment.json = Mock(return_value={"value": {
'name': 'def',
'succeeded': False,
'id': 'id'}})
_responses = [self.response_logout,
response_failed_deployment,
response_deployment_state,
self.response_login]
def _fake_response(*argc, **kwargs):
return _responses.pop()
requests = Mock()
requests.request = _fake_response
# check correct deployment
with patch("cloudify_vsphere.contentlibrary.requests", requests):
with self.assertRaises(NonRecoverableError):
cl = contentlibrary.ContentLibrary({'host': 'host',
'username': 'username',
'password': 'password',
'allow_insecure': True})
cl.content_item_deploy(
"abc", {'target': '_target'}, {'param': '_param'})
cl.__del__()
示例8: test_content_library_get
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_content_library_get(self):
response_list = Mock()
response_list.cookies = {}
response_list.json = Mock(return_value={"value": ['abc']})
response_library = Mock()
response_library.cookies = {}
response_library.json = Mock(return_value={"value": {'name': 'abc',
'id': 'id'}})
_responses = [self.response_logout,
response_library,
response_list,
self.response_login]
def _fake_response(*argc, **kwargs):
return _responses.pop()
requests = Mock()
requests.request = _fake_response
# check content libraries list
with patch("cloudify_vsphere.contentlibrary.requests", requests):
cl = contentlibrary.ContentLibrary({'host': 'host',
'username': 'username',
'password': 'password',
'allow_insecure': True})
self.assertEqual(cl.content_library_get("abc"),
{'name': 'abc', 'id': 'id'})
cl.__del__()
示例9: test_execute_script_both
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_execute_script_both(mock_requests):
r = Mock()
r.status_code = 200
r.content = resource_string(
__name__,
'assets/test_execute_script_both.xml'
)
r.request = Mock()
mock_requests.post.return_value = r
name = 'test_execute_script'
engine = 'groovy'
script = 'htmlOut.print("a")\nrawOut.print("b")'
raw, html = h.execute_script(name, engine, script)
mock_requests.post.assert_called_with(
url='https://localhost:8443/engine/job/%s/script' % name,
data=dict(
engine=engine,
script=script
),
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False,
timeout=None
)
assert_equals("raw", raw)
assert_equals("html", html)
示例10: test_target_source_changed
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_target_source_changed(self):
validator = DjangoUserPermissionValidator('value')
ctx = Mock(spec=['user'])
ctx.request = Mock()
ctx.request.user.has_perm.return_value = False
self.assertFalse(validator.is_write_authorized(ctx, None, 'a', 'b'))
ctx.request.user.has_perm.assert_called_with('value')
示例11: test_onionoo_too_many_answers
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_onionoo_too_many_answers(self):
agent = Mock()
resp = Mock()
resp.code = 200
def feed_response(protocol):
config = {
"relays": [
{
"fingerprint": "00786E43CCC5409753F25E36031C5CEA6EA43702",
},
{
"fingerprint": "boom",
}
]
}
protocol.dataReceived(json.dumps(config).encode())
protocol.connectionLost(Failure(ResponseDone()))
resp.deliverBody = Mock(side_effect=feed_response)
agent.request = Mock(return_value=defer.succeed(resp))
with self.assertRaises(Exception) as ctx:
yield self.router.get_onionoo_details(agent)
self.assertTrue(
"multiple relays for" in str(ctx.exception)
)
示例12: test_widget_addform_url_on_addform
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_widget_addform_url_on_addform(self):
from plone.app.widgets.dx import AjaxSelectWidget
widget = AjaxSelectWidget(self.request)
form = Mock()
from zope.interface import directlyProvides
from z3c.form.interfaces import IAddForm
directlyProvides(form, IAddForm)
form.request = {'URL': 'http://addform_url'}
widget.form = form
self.assertEqual(
{
'name': None,
'value': u'',
'pattern': 'select2',
'pattern_options': {'separator': ';'},
},
widget._base_args(),
)
widget.vocabulary = 'vocabulary1'
self.assertEqual(
{
'name': None,
'value': u'',
'pattern': 'select2',
'pattern_options': {
'separator': ';',
'vocabularyUrl':
'http://addform_url/@@getVocabulary?name=vocabulary1'}
},
widget._base_args(),
)
示例13: test_handle_produce_success_mixed_success
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_handle_produce_success_mixed_success(self, fake_retry):
m1 = Message('test_driver', {'val': 1}, None, None, 0, 1)
m2 = Message('test_driver', {'val': 2}, None, None, 0, 1)
m3 = Message('test_driver', {'val': 3}, None, None, 0, 1)
offsets = [{},
{'error_code': 1, 'message': 'Nonretriable'},
{'error_code': 2, 'message': 'Retriable'}]
response = Mock()
response.request = Mock()
response.request._batch = [m1, m2, m3]
response.request._id = 1
body = {'offsets': offsets,
'value_schema_id': 1,
'key_schema_id': 2}
self.producer._handle_produce_success('test_driver', response, body)
self.client.mock_for('produce_success').assert_called_once_with('test_driver',
[(m1, {})],
[(m2, offsets[1]),
(m3, offsets[2])])
self.client.mock_for('drop_message').assert_called_once_with('test_driver',
m2,
'nonretriable')
fake_retry.assert_called_once_with('test_driver', m3)
示例14: test_target_source_not_changed
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_target_source_not_changed(self):
validator = DjangoUserPermissionValidator('value')
ctx = Mock(spec=['user'])
ctx.request = Mock()
# Should not call has_perm
ctx.request.user.has_perm.side_effect = Exception
self.assertTrue(validator.is_write_authorized(ctx, None, 'a', 'a'))
示例15: test_no_credentials_no_401
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import request [as 别名]
def test_no_credentials_no_401(self):
http = Mock()
http.request = Mock(return_value='response')
client = Client(http, None)
response = client.request('/uri')
self.assertEquals('response', response)
http.request.assert_called_once_with(DEFAULT_INSTANCE_URL + '/uri',
'GET', None, None, 5, None)