本文整理汇总了Python中unittest.mock.Mock.status_code方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.status_code方法的具体用法?Python Mock.status_code怎么用?Python Mock.status_code使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.status_code方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: crawl
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def crawl(source_host):
to_crawl = set()
crawled = set((None,))
to_crawl.add(source_host)
to_from = defaultdict(set)
while to_crawl:
if len(crawled) % 30 == 0:
print('')
print('Crawled: {} ToCrawl: {}'.format(len(crawled), len(to_crawl)))
else:
print('.', end="", flush=True)
url = to_crawl.pop()
try:
response = requests.get(url, timeout=5)
except requests.exceptions.TooManyRedirects:
response = Mock()
response.status_code = 666
except (requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError):
response = Mock()
response.status_code = 667
crawled.add(url)
if response.status_code < 200 or response.status_code >= 400:
print('{}: {} -> {}'.format(response.status_code, to_from[url], url))
continue
soup = BeautifulSoup(response.text)
page_links = {tidy_url(a.get('href')) for a in soup.findAll('a')}
for url_to in page_links:
to_from[url].add(url_to)
to_crawl |= page_links - crawled
pprint(crawled)
示例2: test_get_instance_config_errors
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_get_instance_config_errors(self, mock_req):
"""Tests the behavior when fetching the config ID causes an error"""
# A 404 should do nothing, since that's a missing configuration for
# the instance
mock_resp = Mock()
mock_resp.status_code = 404
mock_req.get.side_effect = HTTPError(response=mock_resp)
self._proxy.get_instance_config_ids()
# Any non-400 should raise though
mock_resp.status_code = 500
with self.assertRaises(HTTPError):
self._proxy.get_instance_config_ids()
示例3: setUp
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def setUp(self):
response = Mock()
response.content = json.dumps({'status': 'ok'})
response.headers = {'Content-Type': 'application/json'}
response.ok = True
response.status_code = requests.codes.ok
self.response = response
示例4: test_chain_check_empty_input
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_chain_check_empty_input(self, requests_get_patched):
mock_response = Mock(spec=Response)
mock_response.status_code = 404
requests_get_patched.return_value = mock_response
# noinspection PyTypeChecker
self.assertFalse(NistBeacon.chain_check(None))
示例5: mock_response
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def mock_response(
status=200,
content="CONTENT",
json_data=None,
raise_for_status=None,
url=None):
"""
since we typically test a bunch of different
requests calls for a service, we are going to do
a lot of mock responses, so its usually a good idea
to have a helper function that builds these things
"""
mock_resp = Mock()
# mock raise_for_status call w/optional error
mock_resp.raise_for_status = Mock()
if raise_for_status:
mock_resp.raise_for_status.side_effect = raise_for_status
# set status code and content
mock_resp.status_code = status
mock_resp.content = content
mock_resp.url = url
# add json data if provided
if json_data:
mock_resp.json = Mock(
return_value=json_data
)
return mock_resp
示例6: test_help
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_help(mock_head, mock_new_tab):
mock_response = Mock(spec='status_code')
mock_head.return_value = mock_response
docs_url = "http://fomod-designer.readthedocs.io/en/stable/index.html"
local_docs = "file://" + \
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "resources", "docs", "index.html"))
mock_response.status_code = codes.ok
MainFrame.help()
mock_head.assert_called_once_with(docs_url, timeout=0.5)
mock_new_tab.assert_called_once_with(docs_url)
mock_response.status_code = codes.forbidden
MainFrame.help()
mock_head.assert_called_with(docs_url, timeout=0.5)
mock_new_tab.assert_called_with(local_docs)
示例7: test_fetch_and_validate_source_when_source_does_not_contain_target
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_fetch_and_validate_source_when_source_does_not_contain_target(self, mock_get):
mock_response = Mock()
mock_response.status_code = 200
mock_response.content = 'foo'
mock_get.return_value = mock_response
self.assertRaises(TargetNotFoundError, fetch_and_validate_source, self.source, self.target)
示例8: mock_get_error
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def mock_get_error(self, url, headers, params, verify):
mock_response = Mock()
mock_response.json.return_value = {
"error": "test error",
"error_description": "This is a test error"}
mock_response.status_code = 200
return mock_response
示例9: test_fetch_and_validate_source_happy_path
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_fetch_and_validate_source_happy_path(self, mock_get):
mock_response = Mock()
mock_response.status_code = 200
mock_response.content = '<a href="{href}">{href}</a>'.format(href=self.target)
mock_get.return_value = mock_response
self.assertEqual(mock_response.content, fetch_and_validate_source(self.source, self.target))
示例10: test_validate_link_with_bad_link
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_validate_link_with_bad_link(self, template):
mock_response = Mock()
mock_response.status_code = 404
with patch('requests.get', Mock(return_value=mock_response)):
template.link = "example.com/fake"
assert False is template.validate_link()
示例11: with_bad_link
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def with_bad_link(template):
mock_response = Mock()
mock_response.status_code = 404
with patch('requests.head', Mock(return_value=mock_response)):
template.link = "example.com/fake"
expect(template.validate_link()) == False
示例12: test_get_last_record
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_get_last_record(self, requests_get_patched):
mock_response = Mock(spec=Response)
mock_response.status_code = 200
mock_response.text = self.expected_current.xml
requests_get_patched.return_value = mock_response
last_record = NistBeacon.get_last_record()
self.assertIsInstance(last_record, NistBeaconValue)
示例13: test_get_record
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_get_record(self, requests_get_patched):
mock_response = Mock(spec=Response)
mock_response.status_code = 200
mock_response.text = self.expected_current.xml
requests_get_patched.return_value = mock_response
record = NistBeacon.get_record(self.reference_timestamp)
self.assertEqual(self.expected_current, record)
示例14: should_raise_exception_when_simultaneous_update
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def should_raise_exception_when_simultaneous_update(self):
mock_put = Mock()
mock_response = Mock()
mock_response.status_code = 409
mock_put.return_value = mock_response
self.service.session.put = mock_put
with pytest.raises(UpdatedSimultaneous):
self.service.update(self.dict_object, "test")
示例15: test_get_next_number_bad_response
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import status_code [as 别名]
def test_get_next_number_bad_response(self):
"""Verify the client can handle bad responses for the next number."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.json = Mock(return_value={})
mock_post = Mock(return_value=mock_response)
# Act and assert
with patch('requests.post', mock_post):
self.assertRaises(DoorstopError, client.get_next_number, 'PREFIX')