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


Python responses.RequestsMock方法代码示例

本文整理汇总了Python中responses.RequestsMock方法的典型用法代码示例。如果您正苦于以下问题:Python responses.RequestsMock方法的具体用法?Python responses.RequestsMock怎么用?Python responses.RequestsMock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在responses的用法示例。


在下文中一共展示了responses.RequestsMock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_api_post_haveibeenpwned_not_reachable

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_api_post_haveibeenpwned_not_reachable(self):
        payload = {
            "secret": "secret message",
            "passphrase": "Hello123",
            "haveibeenpwned": True
        }
        with self.client as c:
            with responses.RequestsMock() as rsps:
                rsps.add(
                    responses.GET,
                    re.compile(r"^(https:\/\/api\.pwnedpasswords\.com\/).*"),
                    body=Exception)
                response = json.loads(
                    c.post("/api/c", json=payload).get_data())

        # haveibeenpwned wasn't reachable, but secret still created if it has
        # all mandatory requirements.
        r = Parse(response)
        self.assertEqual(r.response.status, "created") 
开发者ID:smallwat3r,项目名称:shhh,代码行数:21,代码来源:test_app.py

示例2: test_weather_city

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_weather_city():
    """
    Check result when place is a city
    """
    filepath = os.path.join(os.path.dirname(__file__), "fixtures", "api_weather_response.json")

    with open(filepath, "r") as f:
        json_aq = json.load(f)

    with responses.RequestsMock() as rsps:
        rsps.add(
            "GET", re.compile(r"^http://api.openweathermap.org/data/2.5/"), status=200, json=json_aq
        )

        res = Weather.from_es(Admin(testee), lang="en")

    assert res == Weather(**{"temperature": 291.89, "icon": "01d",}) 
开发者ID:QwantResearch,项目名称:idunn,代码行数:19,代码来源:test_environment_weather.py

示例3: mock_directions_car

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def mock_directions_car():
    with override_settings(
        {
            "QWANT_DIRECTIONS_API_BASE_URL": "http://api.qwant/directions",
            "MAPBOX_DIRECTIONS_ACCESS_TOKEN": None,
        }
    ):
        fixture_path = os.path.join(
            os.path.dirname(__file__), "fixtures/directions", "qwant_directions_car.json",
        )
        with responses.RequestsMock() as rsps:
            rsps.add(
                "GET",
                re.compile(r"^http://api.qwant/directions/"),
                status=200,
                json=json.load(open(fixture_path)),
            )
            yield rsps 
开发者ID:QwantResearch,项目名称:idunn,代码行数:20,代码来源:test_directions.py

示例4: test_rate_limiter_without_redis

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_rate_limiter_without_redis():
    """
    Test that Idunn doesn't stop external requests when
    no redis has been set: 10 requests to Idunn should
    generate 10 requests to Wikipedia API
    """
    client = TestClient(app)

    with responses.RequestsMock() as rsps:
        rsps.add(
            "GET", re.compile(r"^https://.*\.wikipedia.org/"), status=200, json={"test": "test"}
        )
        for i in range(10):
            response = client.get(url=f"http://localhost/v1/pois/osm:relation:7515426?lang=es",)

        assert len(rsps.calls) == 10 
开发者ID:QwantResearch,项目名称:idunn,代码行数:18,代码来源:test_rate_limiter.py

示例5: test_kuzzle_event_nok

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_kuzzle_event_nok():
    """
    Check that an error  501 is raised when kuzzle port and address not set
    """
    # with pytest.raises(Exception):
    filepath = os.path.join(os.path.dirname(__file__), "fixtures", "kuzzle_event_response.json")
    with open(filepath, "r") as f:
        json_event = json.load(f)

    client = TestClient(app)
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
            "POST",
            re.compile(r"^http://localhost:7512/opendatasoft/events/"),
            status=200,
            json=json_event,
        )

        response = client.get(
            url=f"http://localhost/v1/events?bbox=2.0667651,48.432533,2.9384989,49.0349191& ,*&size=5",
        )

        assert response.status_code == 501 
开发者ID:QwantResearch,项目名称:idunn,代码行数:25,代码来源:test_kuzzle.py

示例6: test_no_lang_WIKI_ES

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_no_lang_WIKI_ES():
    """
    Test that when we don't have the lang available in the index
    we call Wikipedia API
    """
    client = TestClient(app)
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
            "GET", re.compile(r"^https://.*\.wikipedia.org/"), status=200, json={"test": "test"}
        )

        """
        We make a request in russian language ("ru")
        """
        response = client.get(url=f"http://localhost/v1/pois/osm:way:7777777?lang=ru",)

        assert len(rsps.calls) == 1 
开发者ID:QwantResearch,项目名称:idunn,代码行数:19,代码来源:test_wiki_ES.py

示例7: generate_and_save_stripe_fixture

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def generate_and_save_stripe_fixture(decorated_function_name: str, mocked_function_name: str,
                                     mocked_function: CallableT) -> Callable[[Any, Any], Any]:  # nocoverage
    def _generate_and_save_stripe_fixture(*args: Any, **kwargs: Any) -> Any:
        # Note that mock is not the same as mocked_function, even though their
        # definitions look the same
        mock = operator.attrgetter(mocked_function_name)(sys.modules[__name__])
        fixture_path = stripe_fixture_path(decorated_function_name, mocked_function_name, mock.call_count)
        try:
            with responses.RequestsMock() as request_mock:
                request_mock.add_passthru("https://api.stripe.com")
                # Talk to Stripe
                stripe_object = mocked_function(*args, **kwargs)
        except stripe.error.StripeError as e:
            with open(fixture_path, 'w') as f:
                error_dict = e.__dict__
                error_dict["headers"] = dict(error_dict["headers"])
                f.write(json.dumps(error_dict, indent=2, separators=(',', ': '), sort_keys=True) + "\n")
            raise e
        with open(fixture_path, 'w') as f:
            if stripe_object is not None:
                f.write(str(stripe_object) + "\n")
            else:
                f.write("{}\n")
        return stripe_object
    return _generate_and_save_stripe_fixture 
开发者ID:zulip,项目名称:zulip,代码行数:27,代码来源:test_stripe.py

示例8: dataset_responses

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def dataset_responses():
    """Authentication responses."""
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:

        def request_callback(request):
            return (200, {'Content-Type': 'application/text'}, '1234')

        rsps.add_callback(
            responses.GET,
            'http://example.com/file',
            callback=request_callback
        )
        rsps.add_callback(
            responses.GET,
            'https://example.com/file',
            callback=request_callback
        )
        rsps.add_callback(
            responses.GET,
            'http://example.com/file.ext?foo=bar',
            callback=request_callback
        )
        yield rsps 
开发者ID:SwissDataScienceCenter,项目名称:renku-python,代码行数:25,代码来源:conftest.py

示例9: setUp

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def setUp(self):
        super(SessionTester, self).setUp()
        with open("tests/rets_responses/Login.xml") as f:
            contents = "".join(f.readlines())

        with responses.RequestsMock() as resps:
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/Login.ashx",
                body=contents,
                status=200,
                headers={"Set-Cookie": "ASP.NET_SessionId=zacqcc1gjhkmazjznjmyrinq;"},
            )
            self.session = Session(
                login_url="http://server.rets.com/rets/Login.ashx",
                username="retsuser",
                version="RETS/1.7.2",
                session_id_cookie_name="ASP.NET_SessionId",
            )
            self.session.login() 
开发者ID:refindlyllc,项目名称:rets,代码行数:22,代码来源:test_session.py

示例10: test_system_metadata

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_system_metadata(self):

        with open("tests/rets_responses/COMPACT-DECODED/GetMetadata_system.xml") as f:
            contents = "".join(f.readlines())

        with responses.RequestsMock() as resps:
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/GetMetadata.ashx",
                body=contents,
                status=200,
            )
            sys_metadata = self.session.get_system_metadata()

        self.assertEqual(sys_metadata["version"], "1.11.76001")
        self.assertEqual(sys_metadata["system_id"], "MLS-RETS") 
开发者ID:refindlyllc,项目名称:rets,代码行数:18,代码来源:test_session.py

示例11: test_cache_metadata

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_cache_metadata(self):
        with open("tests/rets_responses/COMPACT-DECODED/GetMetadata_table.xml") as f:
            contents = "".join(f.readlines())

        with responses.RequestsMock() as resps:
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/GetMetadata.ashx",
                body=contents,
                status=200,
            )
            self.session.get_table_metadata(resource="Property", resource_class="RES")

        self.assertIn(
            "METADATA-TABLE:Property:RES", list(self.session.metadata_responses.keys())
        )

        # Subsequent call without RequestMock should fail unless we get the saved response from metadata_responses
        table = self.session.get_table_metadata(
            resource="Property", resource_class="RES"
        )
        self.assertEqual(len(list(table)), 208) 
开发者ID:refindlyllc,项目名称:rets,代码行数:24,代码来源:test_session.py

示例12: test_table_metadata

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_table_metadata(self):
        with open("tests/rets_responses/COMPACT-DECODED/GetMetadata_table.xml") as f:
            contents = "".join(f.readlines())

        with responses.RequestsMock() as resps:
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/GetMetadata.ashx",
                body=contents,
                status=200,
            )
            table = self.session.get_table_metadata(
                resource="Property", resource_class="RES"
            )

        self.assertEqual(len(list(table)), 208) 
开发者ID:refindlyllc,项目名称:rets,代码行数:18,代码来源:test_session.py

示例13: test_change_parser_automatically

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_change_parser_automatically(self):
        self.assertEqual(self.session.metadata_format, "COMPACT-DECODED")

        with open("tests/rets_responses/Errors/20514.xml") as f:
            dtd_error = "".join(f.readlines())

        with open("tests/rets_responses/STANDARD-XML/GetMetadata_system.xml") as f:
            content = "".join(f.readlines())

        with responses.RequestsMock() as resps:
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/GetMetadata.ashx",
                body=dtd_error,
                status=200,
            )
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/GetMetadata.ashx",
                body=content,
                status=200,
            )
            self.session.get_system_metadata()

        self.assertEqual(self.session.metadata_format, "STANDARD-XML") 
开发者ID:refindlyllc,项目名称:rets,代码行数:27,代码来源:test_session.py

示例14: test_wildcard_lookups

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_wildcard_lookups(self):
        with open("tests/rets_responses/STANDARD-XML/GetMetadata_wildcard.xml") as f:
            contents = "".join(f.readlines())

        with responses.RequestsMock() as resps:
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/GetMetadata.ashx",
                body=contents,
                status=200,
            )
            format_hold = self.session.metadata_format
            try:
                self.session.metadata_format = "STANDARD-XML"
                lookup_values = self.session.get_lookup_values(
                    resource="Property", lookup_name="*"
                )
            finally:
                self.session.metadata_format = format_hold

        self.assertEqual(len(lookup_values), 40) 
开发者ID:refindlyllc,项目名称:rets,代码行数:23,代码来源:test_session.py

示例15: test_lookup_type_metadata

# 需要导入模块: import responses [as 别名]
# 或者: from responses import RequestsMock [as 别名]
def test_lookup_type_metadata(self):
        with open("tests/rets_responses/STANDARD-XML/GetMetadata_lookup.xml") as f:
            contents = "".join(f.readlines())

        with responses.RequestsMock() as resps:
            resps.add(
                resps.POST,
                "http://server.rets.com/rets/GetMetadata.ashx",
                body=contents,
                status=200,
            )
            lookup_values = self.session.get_lookup_values(
                resource="Property", lookup_name="1_2"
            )

        self.assertEqual(len(lookup_values), 9) 
开发者ID:refindlyllc,项目名称:rets,代码行数:18,代码来源:test_session.py


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