本文整理汇总了Python中tests.mocked_response函数的典型用法代码示例。如果您正苦于以下问题:Python mocked_response函数的具体用法?Python mocked_response怎么用?Python mocked_response使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mocked_response函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_long_poll_for_events_and_errors
def test_long_poll_for_events_and_errors(self):
client = BoxClient("my_token")
longpoll_response = {
"type": "realtime_server",
"url": "http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all",
"ttl": "10",
"max_retries": "10",
"retry_timeout": 610,
}
(flexmock(client).should_receive("_get_long_poll_data").and_return(longpoll_response).times(2))
expected_get_params = {
"channel": ["12345678"],
"stream_type": "changes",
"stream_position": "some_stream_position",
}
(
flexmock(requests)
.should_receive("get")
.with_args("http://2.realtime.services.box.net/subscribe", params=expected_get_params)
.and_return(mocked_response({"message": "foo"}))
.and_return(mocked_response("some error", status_code=400))
.times(2)
)
with self.assertRaises(BoxClientException) as expect_exception:
client.long_poll_for_events("some_stream_position", stream_type=EventFilter.CHANGES)
self.assertEqual(400, expect_exception.exception.status_code)
self.assertEqual("some error", expect_exception.exception.message)
示例2: test_handle_auth_response
def test_handle_auth_response(self):
_handle_auth_response(mocked_response({'code': 'bla'}))
with self.assertRaises(BoxAuthenticationException) as expected_exception:
_handle_auth_response(mocked_response({'error': 'some_error', 'error_description': 'foobar'}))
self.assertEqual('foobar', expected_exception.exception.message)
self.assertEqual('some_error', expected_exception.exception.error)
示例3: test_get_client_with_retry
def test_get_client_with_retry(self):
client = BoxClient("my_token")
(
flexmock(requests)
.should_receive("request")
.with_args(
"get",
"https://api.box.com/2.0/files/123/thumbnail.png",
params={},
data=None,
headers=client.default_headers,
stream=True,
)
.and_return(
mocked_response(
status_code=202, headers={"Location": "http://box.com/url_to_thumbnail", "Retry-After": "1"}
),
mocked_response(StringIO("Thumbnail contents")),
)
.one_by_one()
)
thumbnail = client.get_thumbnail(123, max_wait=1)
self.assertEqual("Thumbnail contents", thumbnail.read())
示例4: test_long_poll_for_events_multiple_tries
def test_long_poll_for_events_multiple_tries(self):
client = BoxClient("my_token")
longpoll_response = {
"type": "realtime_server",
"url": "http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all",
"ttl": "10",
"max_retries": "10",
"retry_timeout": 610,
}
(flexmock(client).should_receive("_get_long_poll_data").and_return(longpoll_response).times(5))
(flexmock(client).should_receive("get_events").times(0))
expected_get_params = {
"channel": ["12345678"],
"stream_type": "changes",
"stream_position": "some_stream_position",
}
(
flexmock(requests)
.should_receive("get")
.with_args("http://2.realtime.services.box.net/subscribe", params=expected_get_params)
.and_return(mocked_response({"message": "foo"}))
.and_return(mocked_response({"message": "foo"}))
.and_return(mocked_response({"message": "foo"}))
.and_return(mocked_response({"message": "foo"}))
.and_return(mocked_response({"message": "new_message"}))
.times(5)
)
position = client.long_poll_for_events("some_stream_position", stream_type=EventFilter.CHANGES)
self.assertEqual("some_stream_position", position)
示例5: test_finish_authenticate_error
def test_finish_authenticate_error(self):
response = mocked_response('something_terrible', status_code=400)
(flexmock(requests)
.should_receive('get')
.with_args('https://www.box.com/api/1.0/rest', params={
'action': 'get_auth_token',
'api_key': 'my_api_key',
'ticket': 'golden_ticket'
})
.and_return(response))
with self.assertRaises(BoxAuthenticationException) as expected_exception:
finish_authenticate_v1('my_api_key', 'golden_ticket')
self.assertEqual('something_terrible', expected_exception.exception.message)
response = mocked_response('<response><status>something_terrible</status></response>')
(flexmock(requests)
.should_receive('get')
.with_args('https://www.box.com/api/1.0/rest', params={
'action': 'get_auth_token',
'api_key': 'my_api_key',
'ticket': 'golden_ticket'
})
.and_return(response))
with self.assertRaises(BoxAuthenticationException) as expected_exception:
finish_authenticate_v1('my_api_key', 'golden_ticket')
self.assertEqual('something_terrible', expected_exception.exception.message)
示例6: test_error
def test_error(self):
expected_args = {
'sensor': 'false',
'address': 'my magic address'
}
(flexmock(requests)
.should_receive('get')
.with_args('http://maps.googleapis.com/maps/api/geocode/json', params=expected_args)
.and_return(mocked_response({'status': 'ERROR'}))
)
with self.assertRaises(GeolocationExcetion) as expected_exception:
geolocate('my magic address')
self.assertEqual(expected_exception.exception.message, {'status': 'ERROR'})
expected_args = {
'sensor': 'false',
'address': 'my magic address'
}
(flexmock(requests)
.should_receive('get')
.with_args('http://maps.googleapis.com/maps/api/geocode/json', params=expected_args)
.and_return(mocked_response({'say': 'what'}, status_code=401))
)
with self.assertRaises(GeolocationExcetion) as expected_exception:
geolocate('my magic address')
self.assertEqual(expected_exception.exception.message, {'say': 'what'})
示例7: test_long_poll_for_events_and_errors
def test_long_poll_for_events_and_errors(self):
client = BoxClient('my_token')
longpoll_response = {
'type': 'realtime_server',
'url': 'http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all',
'ttl': '10',
'max_retries': '10',
'retry_timeout': 610
}
(flexmock(client)
.should_receive('_get_long_poll_data')
.and_return(longpoll_response)
.times(2))
expected_get_params = {
'channel': ['12345678'],
'stream_type': 'changes',
'stream_position': 'some_stream_position',
}
(flexmock(requests)
.should_receive('get')
.with_args('http://2.realtime.services.box.net/subscribe', params=expected_get_params)
.and_return(mocked_response({'message': 'foo'}))
.and_return(mocked_response('some error', status_code=400))
.times(2))
with self.assertRaises(BoxClientException) as expect_exception:
client.long_poll_for_events('some_stream_position', stream_type=EventFilter.CHANGES)
self.assertEqual(400, expect_exception.exception.status_code)
self.assertEqual('some error', expect_exception.exception.message)
示例8: test_get_thumbnail_with_params
def test_get_thumbnail_with_params(self):
client = BoxClient("my_token")
# Not available
(
flexmock(requests)
.should_receive("request")
.with_args(
"get",
"https://api.box.com/2.0/files/123/thumbnail.png",
params={},
data=None,
headers=client.default_headers,
stream=True,
)
.and_return(mocked_response(status_code=302))
.once()
)
thumbnail = client.get_thumbnail(123)
self.assertIsNone(thumbnail)
# Already available
(
flexmock(requests)
.should_receive("request")
.with_args(
"get",
"https://api.box.com/2.0/files/123/thumbnail.png",
params={},
data=None,
headers=client.default_headers,
stream=True,
)
.and_return(mocked_response(StringIO("Thumbnail contents")))
.once()
)
thumbnail = client.get_thumbnail(123)
self.assertEqual("Thumbnail contents", thumbnail.read())
# With size requirements
(
flexmock(requests)
.should_receive("request")
.with_args(
"get",
"https://api.box.com/2.0/files/123/thumbnail.png",
params={"min_height": 1, "max_height": 2, "min_width": 3, "max_width": 4},
data=None,
headers=client.default_headers,
stream=True,
)
.and_return(mocked_response(StringIO("Thumbnail contents")))
.once()
)
thumbnail = client.get_thumbnail(123, min_height=1, max_height=2, min_width=3, max_width=4)
self.assertEqual("Thumbnail contents", thumbnail.read())
示例9: test_automatic_refresh
def test_automatic_refresh(self):
credentials = CredentialsV2("access_token", "refresh_token", "client_id", "client_secret")
client = BoxClient(credentials)
requests_mock = flexmock(requests)
# The first attempt, which is denied
(
requests_mock.should_receive("request")
.with_args(
"get", "https://api.box.com/2.0/users/me", params=None, data=None, headers=client.default_headers
)
.and_return(mocked_response(status_code=401))
.once()
)
# The call to refresh the token
(
requests_mock.should_receive("post")
.with_args(
"https://www.box.com/api/oauth2/token",
{
"client_id": "client_id",
"client_secret": "client_secret",
"refresh_token": "refresh_token",
"grant_type": "refresh_token",
},
)
.and_return(mocked_response({"access_token": "new_access_token", "refresh_token": "new_refresh_token"}))
.once()
)
# The second attempt with the new access token
(
requests_mock.should_receive("request")
.with_args(
"get",
"https://api.box.com/2.0/users/me",
params=None,
data=None,
headers={"Authorization": "Bearer new_access_token"},
)
.and_return(mocked_response({"name": "bla"}))
.once()
)
result = client.get_user_info()
self.assertDictEqual(result, {"name": "bla"})
self.assertEqual(credentials._access_token, "new_access_token")
self.assertEqual(credentials._refresh_token, "new_refresh_token")
示例10: test_overwrite_file
def test_overwrite_file(self):
client = BoxClient("my_token")
(flexmock(client).should_receive("_check_for_errors").once())
expected_headers = {"If-Match": "some_tag"}
expected_headers.update(client.default_headers)
expected_response = mocked_response({"entries": [{"id": "1"}]})
(
flexmock(requests)
.should_receive("post")
.with_args(
"https://upload.box.com/api/2.0/files/666/content",
{"content_modified_at": "2006-05-04T03:02:01+00:00"},
headers=expected_headers,
files={"file": FileObjMatcher("hello world")},
)
.and_return(expected_response)
.once()
)
result = client.overwrite_file(
666,
StringIO("hello world"),
etag="some_tag",
content_modified_at=datetime(2006, 5, 4, 3, 2, 1, 0, tzinfo=UTC()),
)
self.assertEqual({"id": "1"}, result)
示例11: test_upload_file_with_timestamps
def test_upload_file_with_timestamps(self):
client = BoxClient("my_token")
response = mocked_response({"entries": [{"id": "1"}]})
(flexmock(client).should_receive("_check_for_errors").once())
(
flexmock(requests)
.should_receive("post")
.with_args(
"https://upload.box.com/api/2.0/files/content",
{
"parent_id": "666",
"content_modified_at": "2007-05-04T03:02:01+00:00",
"content_created_at": "2006-05-04T03:02:01+00:00",
},
headers=client.default_headers,
files={"hello.jpg": ("hello.jpg", FileObjMatcher("hello world"))},
)
.and_return(response)
.once()
)
result = client.upload_file(
"hello.jpg",
StringIO("hello world"),
parent=666,
content_created_at=datetime(2006, 5, 4, 3, 2, 1, 0, tzinfo=UTC()),
content_modified_at=datetime(2007, 5, 4, 3, 2, 1, 0, tzinfo=UTC()),
)
self.assertEqual({"id": "1"}, result)
示例12: test_add_task
def test_add_task(self):
client = BoxClient("my_token")
due_at = datetime.now()
expected_data = {
"item": {"type": "file", "id": 123},
"action": "review",
"due_at": str(due_at),
"message": "test",
}
response = {"type": "task", "id": 123, "action": "review", "message": "test", "due_at": str(due_at)}
(
flexmock(requests)
.should_receive("request")
.with_args(
"post",
"https://api.box.com/2.0/tasks",
params=None,
data=json.dumps(expected_data),
headers=client.default_headers,
)
.and_return(mocked_response(response))
)
task = client.add_task(123, due_at, message="test")
self.assertEquals(task, response)
示例13: test_get_long_poll_data
def test_get_long_poll_data(self):
client = BoxClient("my_token")
expected_response = {
"type": "realtime_server",
"url": "http://2.realtime.services.box.net/subscribe?channel=e9de49a73f0c93a872d7&stream_type=all",
"ttl": "10",
"max_retries": "10",
"retry_timeout": 610,
}
response = mocked_response({"chunk_size": 1, "entries": [expected_response]})
(
flexmock(requests)
.should_receive("request")
.with_args(
"options", "https://api.box.com/2.0/events", headers=client.default_headers, data=None, params=None
)
.and_return(response)
.once()
)
actual_response = client._get_long_poll_data()
self.assertDictEqual(expected_response, actual_response)
示例14: test_send_message
def test_send_message(self):
client = UberClient("[email protected]", "12345")
response = mocked_response("omg")
expected_data = {
"email": "[email protected]",
"deviceOS": settings.DEVICE_OS,
"language": "en",
"deviceModel": settings.DEVICE_MODEL,
"app": "client",
"messageType": "111",
"token": "12345",
"version": settings.UBER_VERSION,
"device": settings.DEVICE_NAME,
"aaa": "bbb",
}
(flexmock(UberClient).should_receive("_copy_location_for_message").with_args(self.mock_location, dict).times(1))
(
flexmock(UberClient)
.should_receive("_post")
.with_args(UberClient.ENDPOINT, data=DictPartialMatcher(expected_data))
.times(1)
.and_return(response)
)
(flexmock(UberClient).should_receive("_validate_message_response").with_args(response.json()).times(1))
params = {"aaa": "bbb"}
self.assertEqual("omg", client._send_message("111", params, self.mock_location))
示例15: test_long_poll_for_events_ok
def test_long_poll_for_events_ok(self):
client = BoxClient('my_token')
longpoll_response = {
'type': 'realtime_server',
'url': 'http://2.realtime.services.box.net/subscribe?channel=12345678&stream_type=all',
'ttl': '10',
'max_retries': '10',
'retry_timeout': 610
}
(flexmock(client)
.should_receive('_get_long_poll_data')
.and_return(longpoll_response)
.once())
expected_get_params = {
'channel': ['12345678'],
'stream_type': 'changes',
'stream_position': 'some_stream_position',
}
(flexmock(requests)
.should_receive('get')
.with_args('http://2.realtime.services.box.net/subscribe', params=expected_get_params)
.and_return(mocked_response({'message': 'new_message'}))
.once())
position = client.long_poll_for_events('some_stream_position', stream_type=EventFilter.CHANGES)
self.assertEqual('some_stream_position', position)