本文整理汇总了Python中mock.Mock.body方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.body方法的具体用法?Python Mock.body怎么用?Python Mock.body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.body方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_call
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_call(self):
body = "body"
headers = {"header": "value"}
path = "/authorize"
status_code = 200
http_code = "200 OK"
environment = {"PATH_INFO": path, "myvar": "value"}
request_mock = Mock(spec=Request)
request_class_mock = Mock(return_value=request_mock)
responseMock = Mock(spec=Response)
responseMock.body = body
responseMock.headers = headers
responseMock.status_code = status_code
server_mock = Mock(spec=Provider)
server_mock.dispatch.return_value = responseMock
start_response_mock = Mock()
wsgi = Wsgi(server=server_mock, authorize_uri=path,
request_class=request_class_mock, env_vars=["myvar"])
result = wsgi(environment, start_response_mock)
request_class_mock.assert_called_with(environment)
server_mock.dispatch.assert_called_with(request_mock,
{"myvar": "value"})
start_response_mock.assert_called_with(http_code, headers.items())
self.assertEqual(result, [body])
示例2: test_rpc_request_404
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_rpc_request_404(self):
response = Mock()
response.code = 404
response.phrase = "Not found."
response.body = escape.json_encode({"result": True})
cyclone.httpclient.fetch.return_value = succeed(response)
return self.assertFailure(self.client.foo(), HTTPError)
示例3: test_respond_to_detection_response
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_respond_to_detection_response(self):
sender = MagicMock()
detect = MagicMock()
detect.respond_to_detection_response = Mock(return_value="something_to_send")
context = MagicMock()
suggest = Mock()
target = Target(sender, detect, context, suggest)
target.json_decode = MagicMock(return_value="decode_detection_response")
response = Mock()
response.body = "response_body"
handler = Mock()
handler.context_id = "context_id_value"
target.get_detect_callback(response, handler, "message_value")
target.json_decode.assert_called_once_with("response_body")
# context.post_context_message.assert_called_once_with("context_id_value", 1, "", dection="decode_detection_response")
self.assertEqual(1, context.post_context_message.call_count)
self.assertEqual("context_id_value", context.post_context_message.call_args_list[0][0][0])
self.assertEqual(1, context.post_context_message.call_args_list[0][0][1])
self.assertEqual("", context.post_context_message.call_args_list[0][0][2])
self.assertEqual("decode_detection_response", context.post_context_message.call_args_list[0][1]["detection"])
detect.respond_to_detection_response.assert_called_once_with(handler, "decode_detection_response")
sender.write_jemboo_response_message.assert_called_once_with(handler, "something_to_send")
示例4: get_signed_grade_mock_request_with_correct_signature
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def get_signed_grade_mock_request_with_correct_signature(self):
"""
Generate a proper LTI request object
"""
mock_request = Mock()
mock_request.headers = {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': (
u'OAuth realm="https://testurl/", oauth_body_hash="wwzA3s8gScKD1VpJ7jMt9b%2BMj9Q%3D",'
'oauth_nonce="18821463", oauth_timestamp="1409321145", '
'oauth_consumer_key="__consumer_key__", oauth_signature_method="HMAC-SHA1", '
'oauth_version="1.0", oauth_signature="fHsE1hhIz76/msUoMR3Lyb7Aou4%3D"'
)
}
mock_request.url = u'https://testurl'
mock_request.http_method = u'POST'
mock_request.method = mock_request.http_method
mock_request.body = (
'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n'
'<imsx_POXEnvelopeRequest xmlns="http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0">'
'<imsx_POXHeader><imsx_POXRequestHeaderInfo><imsx_version>V1.0</imsx_version>'
'<imsx_messageIdentifier>edX_fix</imsx_messageIdentifier></imsx_POXRequestHeaderInfo>'
'</imsx_POXHeader><imsx_POXBody><replaceResultRequest><resultRecord><sourcedGUID>'
'<sourcedId>MITxLTI/MITxLTI/201x:localhost%3A8000-i4x-MITxLTI-MITxLTI-lti-3751833a214a4f66a0d18f63234207f2:363979ef768ca171b50f9d1bfb322131</sourcedId>' # pylint: disable=line-too-long
'</sourcedGUID><result><resultScore><language>en</language><textString>0.32</textString></resultScore>'
'</result></resultRecord></replaceResultRequest></imsx_POXBody></imsx_POXEnvelopeRequest>'
)
return mock_request
示例5: test_post_with_bad_data
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_post_with_bad_data(service_client, get_repositories, get_repository):
get_repositories.return_value = future_repositories
get_repository.return_value = future_repository
mock_response = Mock()
mock_response.body = '{"errors": [{"source_id_type": "", "message": "not supported asset id type"}]}'
mock_response.code = 400
exc = httpclient.HTTPError(400, response=mock_response)
service_client.return_value = make_future(MagicMock())
client = yield service_client()
endpoint = client.repository.repositories[''].search.offers
endpoint.post.side_effect = exc
handler = _create_offers_handler()
# MUT
handler.request.body = ('[{"source_id":' +
'"https://openpermissions.org/s0/hub1/asset/exampleco/ExampleCoPictureID/1",' +
'"source_id_type":""}]')
with pytest.raises(HTTPError) as excinfo:
handler.post().result()
assert excinfo.value.status_code == mock_response.code
assert excinfo.value.errors == json.loads(mock_response.body)
示例6: test_regular
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_regular(self):
sender = MagicMock()
detect = MagicMock()
context = MagicMock()
suggest = Mock()
target = Target(sender, detect, context, suggest)
target.json_decode = MagicMock(return_value={"_rev": "context_revision_value"})
target.context_responder.unsupported_entities = MagicMock()
response = Mock()
response.body = "response_body_value"
handler = MagicMock()
handler.user_id = "user_id_value"
handler.application_id = "application_id_value"
handler.session_id = "session_id_value"
handler.locale = "locale_value"
target.get_context_callback(response, handler, "message_value")
target.json_decode.assert_called_once_with("response_body_value")
target.context_responder.unsupported_entities.assert_called_once_with(
handler, {"_rev": "context_revision_value"}
)
self.assertEqual(1, suggest.post_suggest.call_count)
self.assertEqual("user_id_value", suggest.post_suggest.call_args_list[0][0][0])
self.assertEqual("application_id_value", suggest.post_suggest.call_args_list[0][0][1])
self.assertEqual("session_id_value", suggest.post_suggest.call_args_list[0][0][2])
self.assertEqual("locale_value", suggest.post_suggest.call_args_list[0][0][3])
self.assertDictEqual({"_rev": "context_revision_value"}, suggest.post_suggest.call_args_list[0][0][4])
示例7: test_update_item_dbex
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_update_item_dbex(self):
'''This test case covers scenario when an item can not be updated because of a db exception.'''
expected_body = {"name": "cool name",
"description": "incredible simple description"}
url = "/simple-resources"
version = "1.0"
resource_id = "12345"
request = Mock()
request.body = json.dumps(expected_body).encode()
resource = Resource(name="Mock Simple Resource", url=url,
version=float(version))
resource(MockSimpleResourceRoa, self._resources_registry)
pk_col = Mock()
pk_col.name = "id"
model = Mock()
self._resources_registry.find_by_url = Mock(return_value=resource)
self._json_serializer.deserialize = Mock(return_value=model)
self._model_facade.update = Mock(side_effect=FantasticoDbError("Unexpected exception"))
self._model_facade.model_pk_cols = [pk_col]
response = self._controller.update_item(request, version, url, resource_id)
self._assert_resource_error(response, 400, 10030, version, url)
self.assertEqual(resource_id, model.id)
self._resources_registry.find_by_url.assert_called_once_with(url, float(version))
self._json_serializer_cls.assert_called_once_with(resource)
self._json_serializer.deserialize.assert_called_once_with(json.dumps(expected_body))
示例8: test_update_item_itemnotfound
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_update_item_itemnotfound(self):
'''This test case covers scenario when we want to update an item which does not exist.'''
expected_body = {"name": "cool name",
"description": "incredible simple description"}
url = "/simple-resources"
version = "1.0"
resource_id = "12345"
request = Mock()
request.body = json.dumps(expected_body).encode()
resource = Resource(name="Mock Simple Resource", url=url,
version=float(version))
resource(MockSimpleResourceRoa, self._resources_registry)
pk_col = MockSimpleResourceRoa.id
model = Mock()
self._resources_registry.find_by_url = Mock(return_value=resource)
self._json_serializer.deserialize = Mock(return_value=model)
self._model_facade.find_by_pk = Mock(return_value=None)
self._model_facade.model_pk_cols = [pk_col]
response = self._controller.update_item(request, version, url, resource_id)
self._assert_resource_error(response, 404, 10040, version, url)
self._resources_registry.find_by_url.assert_called_once_with(url, float(version))
self._model_facade.find_by_pk.assert_called_once_with({MockSimpleResourceRoa.id: resource_id})
self._json_serializer_cls.assert_called_once_with(resource)
self._json_serializer.deserialize.assert_called_once_with(json.dumps(expected_body))
示例9: test_create_item_dbexception
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_create_item_dbexception(self):
'''This test case ensures an error response is received if an unexpected db error occurs when creating the resource.'''
resource = Resource(name="Mock Simple Resource", url="/mock-simple-resources",
version=1.0)
resource(MockSimpleResourceRoa, self._resources_registry)
request_body = {"name": "simple-resource1",
"description": "Simple resource description."}
expected_model = MockSimpleResourceRoa(name=request_body.get("name"),
description=request_body.get("description"))
request = Mock()
request.body = json.dumps(request_body).encode()
self._resources_registry.find_by_url = Mock(return_value=resource)
self._json_serializer.deserialize = Mock(return_value=expected_model)
self._model_facade.create = Mock(side_effect=FantasticoDbError("Unexpected db error."))
response = self._controller.create_item_latest(request, resource.url)
self._assert_resource_error(response, 400, 10030, str(resource.version), resource.url)
self._resources_registry.find_by_url.assert_called_once_with(resource.url, "latest")
self._json_serializer.deserialize.assert_called_once_with(request.body.decode())
self._model_facade.create.assert_called_once_with(expected_model)
示例10: test_update_item_resource_invalid
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_update_item_resource_invalid(self):
'''This test case ensures an item is not updated if it fails validation.'''
expected_body = {}
url = "/simple-resources"
version = "1.0"
resource = Resource(name="Mock Simple Resource", url=url,
version=float(version),
validator=MockSimpleResourceValidator)
resource(MockSimpleResourceRoa, self._resources_registry)
resource_id = 1986
request = Mock()
request.body = json.dumps(expected_body).encode()
self._resources_registry.find_by_url = Mock(return_value=resource)
self._json_serializer.deserialize = Mock(return_value=MockSimpleResourceRoa())
response = self._controller.update_item(request, version, url, resource_id)
self._assert_resource_error(response, 400, 10010, version, url)
self._resources_registry.find_by_url.assert_called_once_with(url, float(version))
self._json_serializer.deserialize.assert_called_once_with(json.dumps(expected_body))
示例11: get_signed_grade_mock_request
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def get_signed_grade_mock_request(self, namespace_lti_v1p1=True):
"""
Example of signed request from LTI Provider.
When `namespace_v1p0` is set to True then the default namespase from
LTI 1.1 will be used. Otherwise fake namespace will be added to XML.
"""
mock_request = Mock()
mock_request.headers = {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': u'OAuth oauth_nonce="135685044251684026041377608307", \
oauth_timestamp="1234567890", oauth_version="1.0", \
oauth_signature_method="HMAC-SHA1", \
oauth_consumer_key="test_client_key", \
oauth_signature="my_signature%3D", \
oauth_body_hash="JEpIArlNCeV4ceXxric8gJQCnBw="'
}
mock_request.url = u'http://testurl'
mock_request.http_method = u'POST'
params = {}
if not namespace_lti_v1p1:
params = {
'namespace': "http://www.fakenamespace.com/fake"
}
mock_request.body = self.get_request_body(params)
return mock_request
示例12: test_rpc_request
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_rpc_request(self):
response = Mock()
response.code = 200
response.body = escape.json_encode({"result": True})
cyclone.httpclient.fetch.return_value = succeed(response)
result = yield self.client.foo()
self.assertTrue(result)
示例13: test_payment_already_paid
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_payment_already_paid(self):
payment = mommy.make(getpaid.models.Payment, id=90, amount=119000, paid_on=now())
request = Mock()
request.POST = {'TBK_MONTO': '11900000'}
request.body = self.QUOTED_REQUEST_BODY
self.assertFalse(PaymentProcessor.validate(payment, request))
示例14: test_exists_false
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_exists_false():
db = create_mockdb()
response = Mock()
response.body = '{"boolean":false}'
db.query.return_value = make_future(response)
entity_id = 'e321111e118'
result = yield asset.exists(db, entity_id)
assert result is False
示例15: test_dump_body_buffer
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import body [as 别名]
def test_dump_body_buffer(self):
msg = Mock()
msg.body = 'str'
try:
buf = buffer(msg.body)
except NameError:
raise SkipTest('buffer type not available')
self.assertTrue(dump_body(msg, buf))