当前位置: 首页>>代码示例>>Python>>正文


Python Mock.status_code方法代码示例

本文整理汇总了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)
开发者ID:,项目名称:,代码行数:35,代码来源:

示例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()
开发者ID:nioinnovation,项目名称:component_config_monitor,代码行数:15,代码来源:test_proxy.py

示例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
开发者ID:maxcountryman,项目名称:rauth,代码行数:9,代码来源:base.py

示例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))
开发者ID:urda,项目名称:nistbeacon,代码行数:9,代码来源:test_nistbeacon.py

示例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
开发者ID:fedspendingtransparency,项目名称:data-act-broker-backend,代码行数:29,代码来源:mock_helpers.py

示例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)
开发者ID:GandaG,项目名称:fomod-designer,代码行数:18,代码来源:test_gui.py

示例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)
开发者ID:easy-as-python,项目名称:django-webmention,代码行数:9,代码来源:test_resolution.py

示例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
开发者ID:optionalg,项目名称:login-and-pay-with-amazon-sdk-python,代码行数:9,代码来源:test_lwa.py

示例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))
开发者ID:easy-as-python,项目名称:django-webmention,代码行数:9,代码来源:test_resolution.py

示例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()
开发者ID:gonzalocasas,项目名称:memegen,代码行数:10,代码来源:test_domain_template.py

示例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
开发者ID:DanLindeman,项目名称:memegen,代码行数:10,代码来源:test_domain_template.py

示例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)
开发者ID:urda,项目名称:nistbeacon,代码行数:10,代码来源:test_nistbeacon.py

示例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)
开发者ID:urda,项目名称:nistbeacon,代码行数:10,代码来源:test_nistbeacon.py

示例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")
开发者ID:sruizr,项目名称:pyactiviti,代码行数:11,代码来源:test_base.py

示例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')
开发者ID:elewis33,项目名称:doorstop,代码行数:11,代码来源:test_client.py


注:本文中的unittest.mock.Mock.status_code方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。