本文整理汇总了Python中httmock.all_requests方法的典型用法代码示例。如果您正苦于以下问题:Python httmock.all_requests方法的具体用法?Python httmock.all_requests怎么用?Python httmock.all_requests使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类httmock
的用法示例。
在下文中一共展示了httmock.all_requests方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mock_response
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def mock_response(
content, url=None, path='', headers=None, response_url=None, status_code=200, cookies=None, func=None
):
"""Universal handler for specify mocks inplace"""
if func is None:
def mocked(url, request):
mock = response(
status_code=status_code, content=content, request=request, headers=headers
)
if cookies:
mock.cookies = cookies
mock.url = response_url if response_url else url
return mock
else:
mocked = func
if url:
parsed = urlparse(url)
return urlmatch(netloc=parsed.netloc, path=parsed.path)(func=mocked)
elif path:
return urlmatch(path=path)(func=mocked)
else:
return all_requests(func=mocked)
示例2: test_user_agent
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_user_agent(self):
@httmock.all_requests
def default_user_agent(url, request):
user_agent = request.headers.get('User-Agent', None)
self.assertEqual(
user_agent, 'PyCrest/{0} +https://github.com/pycrest/PyCrest'
.format(pycrest.version))
with httmock.HTTMock(default_user_agent):
EVE()
@httmock.all_requests
def customer_user_agent(url, request):
user_agent = request.headers.get('User-Agent', None)
self.assertEqual(
user_agent,
'PyCrest-Testing/{0} +https://github.com/pycrest/PyCrest'
.format(pycrest.version))
with httmock.HTTMock(customer_user_agent):
EVE(user_agent='PyCrest-Testing/{0} +https://github.com/pycrest/P'
'yCrest'.format(pycrest.version))
示例3: test_headers
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_headers(self):
# Check default header
@httmock.all_requests
def check_default_headers(url, request):
self.assertNotIn('PyCrest-Testing', request.headers)
with httmock.HTTMock(check_default_headers):
EVE()
# Check custom header
def check_custom_headers(url, request):
self.assertIn('PyCrest-Testing', request.headers)
with httmock.HTTMock(check_custom_headers):
EVE(additional_headers={'PyCrest-Testing': True})
示例4: test_cache_hit
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_cache_hit(self):
@httmock.all_requests
def prime_cache(url, request):
headers = {'content-type': 'application/json',
'Cache-Control': 'max-age=300;'}
return httmock.response(200, '{}'.encode('utf-8'), headers)
with httmock.HTTMock(prime_cache):
self.assertEqual(self.api()._dict, {})
@httmock.all_requests
def cached_request(url, request):
raise RuntimeError(
'A cached request should never yield a HTTP request')
with httmock.HTTMock(cached_request):
self.api._data = None
self.assertEqual(self.api()._dict, {})
示例5: test_cache_invalidate
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_cache_invalidate(self):
@httmock.all_requests
def prime_cache(url, request):
headers = {'content-type': 'application/json',
'Cache-Control': 'max-age=300;'}
return httmock.response(
200, '{"cached": true}'.encode('utf-8'), headers)
# Prime cache and force the expiration
with httmock.HTTMock(prime_cache):
self.api()
# Nuke _data so the .get() is actually being called the next call
self.api._data = None
for key in self.api.cache._dict:
# Make sure the cache is concidered 'expired'
self.api.cache._dict[key]['expires'] = 0
@httmock.all_requests
def expired_request(url, request):
self.assertTrue(isinstance(request, PreparedRequest))
return httmock.response(200, '{}'.encode('utf-8'))
with httmock.HTTMock(expired_request):
self.api()
示例6: test_get_200_returns_response_body
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_get_200_returns_response_body(self):
@httmock.all_requests
def response_content(_url_unused, request):
headers = {
'X-Consul-Index': 4,
'X-Consul-Knownleader': 'true',
'X-Consul-Lastcontact': 0,
'Date': 'Fri, 19 Dec 2014 20:44:28 GMT',
'Content-Length': 13,
'Content-Type': 'application/json'
}
content = b'{"consul": []}'
return httmock.response(200, content, headers, None, 0, request)
with httmock.HTTMock(response_content):
values = self.endpoint._get([str(uuid.uuid4())])
self.assertEqual(values, {'consul': []})
示例7: test_get_list_200_returns_response_body
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_get_list_200_returns_response_body(self):
@httmock.all_requests
def response_content(_url_unused, request):
headers = {
'X-Consul-Index': 4,
'X-Consul-Knownleader': 'true',
'X-Consul-Lastcontact': 0,
'Date': 'Fri, 19 Dec 2014 20:44:28 GMT',
'Content-Length': 13,
'Content-Type': 'application/json'
}
content = b'{"consul": []}'
return httmock.response(200, content, headers, None, 0, request)
with httmock.HTTMock(response_content):
values = self.endpoint._get_list([str(uuid.uuid4())])
self.assertEqual(values, [{'consul': []}])
示例8: test_client_cache_request
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_client_cache_request(self):
@httmock.all_requests
def fail_if_request(url, request):
self.fail('Cached data is not supposed to do requests')
incursion_operation = self.app.op['get_incursions']
with httmock.HTTMock(public_incursion_no_expires):
incursions = self.client_no_auth.request(incursion_operation())
self.assertEqual(incursions.data[0].state, 'mobilizing')
with httmock.HTTMock(public_incursion_no_expires_second):
incursions = self.client_no_auth.request(incursion_operation())
self.assertEqual(incursions.data[0].state, 'established')
with httmock.HTTMock(public_incursion):
incursions = self.client_no_auth.request(incursion_operation())
self.assertEqual(incursions.data[0].state, 'mobilizing')
with httmock.HTTMock(fail_if_request):
incursions = self.client_no_auth.request(incursion_operation())
self.assertEqual(incursions.data[0].state, 'mobilizing')
示例9: test_get_base_test_ids_from_list_file_url
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_get_base_test_ids_from_list_file_url(self):
"""Test that we can parse the test cases from a test list URL."""
list_file = self.test_path + "/test-list.txt"
with open(list_file, 'rb') as f:
content = f.read()
@httmock.all_requests
def request_mock(url, request):
return {'status_code': 200,
'content': content}
with httmock.HTTMock(request_mock):
online_list = self.parser._get_base_test_ids_from_list_file(
"http://127.0.0.1/test-list.txt")
expected_list = ['tempest.api.test1',
'tempest.api.test2',
'tempest.api.test3(scenario)']
self.assertEqual(expected_list, sorted(online_list))
示例10: test_authorize_non_200
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_authorize_non_200(self):
@httmock.all_requests
def mock_login(url, request):
return httmock.response(status_code=204,
content='{}')
with httmock.HTTMock(mock_login):
self.assertRaises(APIException, self.api.authorize, code='code')
示例11: test_default_url
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_default_url(self):
@httmock.all_requests
def root_mock(url, request):
self.assertEqual(url.path, '/')
self.assertEqual(url.query, '')
return {'status_code': 200,
'content': '{}'.encode('utf-8')}
with httmock.HTTMock(root_mock):
self.api()
示例12: test_parse_parameters_url
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_parse_parameters_url(self):
@httmock.all_requests
def key_mock(url, request):
self.assertEqual(url.path, '/')
self.assertEqual(url.query, 'key=value1')
return {'status_code': 200,
'content': '{}'.encode('utf-8')}
with httmock.HTTMock(key_mock):
self.api.get('https://crest-tq.eveonline.com/?key=value1')
示例13: test_non_http_200
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_non_http_200(self):
@httmock.all_requests
def non_http_200(url, request):
return {'status_code': 404, 'content' : {'message' : 'not found'}}
with httmock.HTTMock(non_http_200):
self.assertRaises(APIException, self.api)
示例14: test_session_mock
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_session_mock(self):
# Check default header
@httmock.all_requests
def expired_request(url, request):
print(url)
print(request)
self.assertTrue(isinstance(request, PreparedRequest))
return httmock.response(200, '{}'.encode('utf-8'))
with httmock.HTTMock(expired_request):
self.api()
示例15: test_non_http_200_201_post
# 需要导入模块: import httmock [as 别名]
# 或者: from httmock import all_requests [as 别名]
def test_non_http_200_201_post(self):
@httmock.all_requests
def non_http_200(url, request):
return {'status_code': 404, 'content' : {'message' : 'not found'}}
with httmock.HTTMock(non_http_200):
self.assertRaises(APIException, self.api.writeableEndpoint, method='post')