本文整理汇总了Python中httpretty.Response方法的典型用法代码示例。如果您正苦于以下问题:Python httpretty.Response方法的具体用法?Python httpretty.Response怎么用?Python httpretty.Response使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类httpretty
的用法示例。
在下文中一共展示了httpretty.Response方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_http_server
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def setup_http_server():
url_projects = read_file(URL_PROJECTS_FILE)
http_requests = []
def request_callback(method, uri, headers):
last_request = httpretty.last_request()
body = url_projects
http_requests.append(last_request)
return 200, headers, body
httpretty.register_uri(httpretty.GET,
PROJECTS_URL,
responses=[
httpretty.Response(body=request_callback)
])
return http_requests
示例2: setup_http_server
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def setup_http_server():
remote_identities = read_file(REMOTE_IDENTITIES_FILE)
http_requests = []
def request_callback(method, uri, headers):
last_request = httpretty.last_request()
if uri.startswith(REMOTE_IDENTITIES_FILE_URL):
body = remote_identities
http_requests.append(last_request)
return 200, headers, body
httpretty.register_uri(httpretty.GET,
REMOTE_IDENTITIES_FILE_URL,
responses=[
httpretty.Response(body=request_callback)
])
return http_requests
示例3: test_msi_vm_imds_retry
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_msi_vm_imds_retry(self):
json_payload = {
'token_type': "TokenTypeIMDS",
"access_token": "AccessToken"
}
httpretty.register_uri(httpretty.GET,
'http://169.254.169.254/metadata/identity/oauth2/token',
responses=[
httpretty.Response('', status=404),
httpretty.Response('', status=429),
httpretty.Response('', status=599),
httpretty.Response(body=json.dumps(json_payload)),
],
content_type="application/json")
credentials = MSIAuthentication()
assert credentials.scheme == "TokenTypeIMDS"
assert credentials.token == json_payload
# Assert four requests made only
assert len(httpretty.httpretty.latest_requests) == 4
示例4: test_delayed_report
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_delayed_report(self, mock_time):
responses = list(self.RESPONSES)
responses.insert(0, self.create_runreport_response(1, 'Report has been created'))
responses.insert(1, self.create_results_response())
httpretty.register_uri(
httpretty.POST,
TEST_URL,
responses=[
httpretty.Response(body=r)
for r in responses
]
)
mock_time.time.return_value = 123456789012.1
self.task.run()
self.assertEqual(mock_time.time.call_count, 1)
mock_time.sleep.assert_has_calls([
call(5),
call(5)
])
expected_record = ['2015-08-28', 'paypal', 'testing', 'EDX-123456', 'USD', '50.00', '1.40', 'sale',
'instant_transfer', 'paypal', '1FW12345678901234']
self.assertEquals(self.output_target.value.strip(), '\t'.join(expected_record))
示例5: test_report_timeout
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_report_timeout(self, mock_time):
responses = list(self.RESPONSES)
responses.insert(0, self.create_runreport_response(1, 'Report has been created'))
httpretty.register_uri(
httpretty.POST,
TEST_URL,
responses=[
httpretty.Response(body=r)
for r in responses
]
)
arbitrary_time = 123456789012.1234
mock_time.time.side_effect = [arbitrary_time, arbitrary_time + 5]
with self.assertRaises(PaypalTimeoutError):
self.task.run()
示例6: test_non_fatal_error
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_non_fatal_error(self):
self.prepare_for_token_request()
httpretty.register_uri('GET', FAKE_RESOURCE_URL,
responses=[
httpretty.Response(body='Too many requests!', status=429),
httpretty.Response(body='Unavailable', status=503),
httpretty.Response(body='{}', status=200)
])
responses = list(self.client.paginated_get(FAKE_RESOURCE_URL))
self.assertEqual(len(responses), 1)
response = responses[0]
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {})
num_auth_token_requests = 1
num_failed_requests = 2
num_successful_requests = 1
total_expected_requests = num_auth_token_requests + num_failed_requests + num_successful_requests
self.assertEqual(len(httpretty.httpretty.latest_requests), total_expected_requests)
示例7: mock_course_runs_contains_endpoint_failure
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def mock_course_runs_contains_endpoint_failure(self, course_run_ids, catalog_id, error, discovery_api_url):
"""
Helper function to register a discovery API endpoint with failure
for getting course runs information.
"""
def callback(request, uri, headers): # pylint: disable=unused-argument
raise error
catalog_contains_course_run_url = '{}catalogs/{}/contains/?course_run_id={}'.format(
discovery_api_url,
catalog_id,
','.join(course_run_id for course_run_id in course_run_ids),
)
httpretty.register_uri(
method=httpretty.GET,
uri=catalog_contains_course_run_url,
responses=[
httpretty.Response(body=callback, content_type='application/json', status_code=500)
]
)
示例8: test_remove_units_should_return_false_when_doesnt_remove_all_process_types
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_remove_units_should_return_false_when_doesnt_remove_all_process_types(self):
self.bg.total_units = MagicMock(return_value={'web': 2, 'resque': 1})
httpretty.register_uri(httpretty.DELETE, 'http://tsuruhost.com/apps/xpto/units',
data='',
responses=[
httpretty.Response(body='', status=500),
httpretty.Response(body='', status=200)
])
self.assertFalse(self.bg.remove_units('xpto'))
requests = httpretty.HTTPretty.latest_requests
self.assertEqual(len(requests), 2)
示例9: test_remove_units_should_return_false_when_doesnt_add_all_process_types
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_remove_units_should_return_false_when_doesnt_add_all_process_types(self):
self.bg.total_units = MagicMock(return_value={'web': 2, 'resque': 1})
httpretty.register_uri(httpretty.PUT, 'http://tsuruhost.com/apps/xpto/units',
data='',
responses=[
httpretty.Response(body='', status=500),
httpretty.Response(body='', status=200)
])
self.assertFalse(self.bg.add_units('xpto', {'web': 3, 'resque': 2}))
requests = httpretty.HTTPretty.latest_requests
self.assertEqual(len(requests), 2)
示例10: test_request_redirect_post
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_request_redirect_post(self):
url = self.client.format_url("/get_endpoint")
request = self.client.post(url, {'check':True})
httpretty.register_uri(httpretty.GET, 'https://my_service.com/http/success/get/200', status=200)
httpretty.register_uri(httpretty.POST, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=303, method='POST', location='/http/success/get/200'),
])
response = self.client.send(request)
assert response.status_code == 200, "Should redirect with GET on 303 with location header"
assert response.request.method == 'GET'
assert response.history[0].status_code == 303
assert response.history[0].is_redirect
httpretty.reset()
httpretty.register_uri(httpretty.POST, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=303, method='POST'),
])
response = self.client.send(request)
assert response.status_code == 303, "Should not redirect on 303 without location header"
assert response.history == []
assert not response.is_redirect
示例11: test_request_redirect_head
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_request_redirect_head(self):
url = self.client.format_url("/get_endpoint")
request = self.client.head(url, {'check':True})
httpretty.register_uri(httpretty.HEAD, 'https://my_service.com/http/success/200', status=200)
httpretty.register_uri(httpretty.HEAD, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=307, method='HEAD', location='/http/success/200'),
])
response = self.client.send(request)
assert response.status_code == 200, "Should redirect on 307 with location header"
assert response.request.method == 'HEAD'
assert response.history[0].status_code == 307
assert response.history[0].is_redirect
httpretty.reset()
httpretty.register_uri(httpretty.HEAD, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=307, method='HEAD'),
])
response = self.client.send(request)
assert response.status_code == 307, "Should not redirect on 307 without location header"
assert response.history == []
assert not response.is_redirect
示例12: test_request_redirect_delete
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_request_redirect_delete(self):
url = self.client.format_url("/get_endpoint")
request = self.client.delete(url, {'check':True})
httpretty.register_uri(httpretty.DELETE, 'https://my_service.com/http/success/200', status=200)
httpretty.register_uri(httpretty.DELETE, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=307, method='DELETE', location='/http/success/200'),
])
response = self.client.send(request)
assert response.status_code == 200, "Should redirect on 307 with location header"
assert response.request.method == 'DELETE'
assert response.history[0].status_code == 307
assert response.history[0].is_redirect
httpretty.reset()
httpretty.register_uri(httpretty.DELETE, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=307, method='DELETE'),
])
response = self.client.send(request)
assert response.status_code == 307, "Should not redirect on 307 without location header"
assert response.history == []
assert not response.is_redirect
示例13: test_request_redirect_put
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_request_redirect_put(self):
url = self.client.format_url("/get_endpoint")
request = self.client.put(url, {'check':True})
httpretty.register_uri(httpretty.PUT, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=305, method='PUT', location='/http/success/200'),
])
response = self.client.send(request)
assert response.status_code == 305, "Should not redirect on 305"
assert response.history == []
assert not response.is_redirect
示例14: test_request_redirect_get
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_request_redirect_get(self):
url = self.client.format_url("/get_endpoint")
request = self.client.get(url, {'check':True})
httpretty.register_uri(httpretty.GET, "https://my_service.com/http/finished",
responses=[
httpretty.Response(body="", status=200, method='GET'),
])
httpretty.register_uri(httpretty.GET, "https://my_service.com/http/redirect3",
responses=[
httpretty.Response(body="", status=307, method='GET', location='/http/finished'),
])
httpretty.register_uri(httpretty.GET, "https://my_service.com/http/redirect2",
responses=[
httpretty.Response(body="", status=307, method='GET', location='/http/redirect3'),
])
httpretty.register_uri(httpretty.GET, "https://my_service.com/http/redirect1",
responses=[
httpretty.Response(body="", status=307, method='GET', location='/http/redirect2'),
])
httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="", status=307, method='GET', location='/http/redirect1'),
])
with self.assertRaises(ClientRequestError, msg="Should exceed maximum redirects"):
self.client.send(request)
示例15: test_request_retry_408
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import Response [as 别名]
def test_request_retry_408(self):
httpretty.register_uri(httpretty.GET, "https://my_service.com/get_endpoint",
responses=[
httpretty.Response(body="retry response", status=408),
httpretty.Response(body='success response', status=202),
])
response = self.client.send(self.request)
self.assertEqual(response.status_code, 202, msg="Should retry on 408")