當前位置: 首頁>>代碼示例>>Python>>正文


Python responses.add_callback方法代碼示例

本文整理匯總了Python中responses.add_callback方法的典型用法代碼示例。如果您正苦於以下問題:Python responses.add_callback方法的具體用法?Python responses.add_callback怎麽用?Python responses.add_callback使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在responses的用法示例。


在下文中一共展示了responses.add_callback方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_retry

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_retry(self):
        class request_callback:
            def __init__(self):
                self.first_req = True

            def __call__(self, req):
                if self.first_req:
                    self.first_req = False
                    return (500, {}, "Internal Server Error.")
                return (200, {}, '{"speedLimits":[]}')

        responses.add_callback(
            responses.GET,
            "https://roads.googleapis.com/v1/speedLimits",
            content_type="application/json",
            callback=request_callback(),
        )

        self.client.speed_limits([])

        self.assertEqual(2, len(responses.calls))
        self.assertEqual(responses.calls[0].request.url, responses.calls[1].request.url) 
開發者ID:googlemaps,項目名稱:google-maps-services-python,代碼行數:24,代碼來源:test_roads.py

示例2: test_retry

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_retry(self):
        class request_callback:
            def __init__(self):
                self.first_req = True

            def __call__(self, req):
                if self.first_req:
                    self.first_req = False
                    return (200, {}, '{"status":"OVER_QUERY_LIMIT"}')
                return (200, {}, '{"status":"OK","results":[]}')

        responses.add_callback(
            responses.GET,
            "https://maps.googleapis.com/maps/api/geocode/json",
            content_type="application/json",
            callback=request_callback(),
        )

        client = googlemaps.Client(key="AIzaasdf")
        client.geocode("Sesame St.")

        self.assertEqual(2, len(responses.calls))
        self.assertEqual(responses.calls[0].request.url, responses.calls[1].request.url) 
開發者ID:googlemaps,項目名稱:google-maps-services-python,代碼行數:25,代碼來源:test_client.py

示例3: test_retry_intermittent

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_retry_intermittent(self):
        class request_callback:
            def __init__(self):
                self.first_req = True

            def __call__(self, req):
                if self.first_req:
                    self.first_req = False
                    return (500, {}, "Internal Server Error.")
                return (200, {}, '{"status":"OK","results":[]}')

        responses.add_callback(
            responses.GET,
            "https://maps.googleapis.com/maps/api/geocode/json",
            content_type="application/json",
            callback=request_callback(),
        )

        client = googlemaps.Client(key="AIzaasdf")
        client.geocode("Sesame St.")

        self.assertEqual(2, len(responses.calls)) 
開發者ID:googlemaps,項目名稱:google-maps-services-python,代碼行數:24,代碼來源:test_client.py

示例4: test_token_cached_per_repo

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_token_cached_per_repo(self):
        responses.add(responses.GET, BEARER_REALM_URL + '?scope=repository:fedora:pull',
                      json={'token': BEARER_TOKEN}, match_querystring=True)
        responses.add(responses.GET, BEARER_REALM_URL + '?scope=repository:centos:pull',
                      json={'token': BEARER_TOKEN}, match_querystring=True)

        fedora_url = 'https://registry.example.com/v2/fedora/tags/list'
        responses.add_callback(responses.GET, fedora_url, callback=bearer_unauthorized_callback)
        responses.add(responses.GET, fedora_url, status=200, json='fedora-success')
        responses.add(responses.GET, fedora_url, status=200, json='fedora-success-also')

        centos_url = 'https://registry.example.com/v2/centos/tags/list'
        responses.add_callback(responses.GET, centos_url, callback=bearer_unauthorized_callback)
        responses.add(responses.GET, centos_url, status=200, json='centos-success')
        responses.add(responses.GET, centos_url, status=200, json='centos-success-also')

        auth = HTTPBearerAuth()

        assert requests.get(fedora_url, auth=auth).json() == 'fedora-success'
        assert requests.get(fedora_url, auth=auth).json() == 'fedora-success-also'

        assert requests.get(centos_url, auth=auth).json() == 'centos-success'
        assert requests.get(centos_url, auth=auth).json() == 'centos-success-also'

        assert len(responses.calls) == 8 
開發者ID:containerbuildsystem,項目名稱:atomic-reactor,代碼行數:27,代碼來源:test_auth.py

示例5: test_non_401_error_propagated

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_non_401_error_propagated(self):

        def bearer_teapot_callback(request):
            headers = {'www-authenticate': 'Bearer realm={}'.format(BEARER_REALM_URL)}
            return (418, headers, json.dumps("I'm a teapot!"))

        url = 'https://registry.example.com/v2/fedora/tags/list'
        responses.add_callback(responses.GET, url, callback=bearer_teapot_callback)
        responses.add(responses.GET, url, status=200, json='success')  # Not actually called

        auth = HTTPBearerAuth()

        response = requests.get(url, auth=auth)
        assert response.json() == "I'm a teapot!"
        assert response.status_code == 418
        assert len(responses.calls) == 1 
開發者ID:containerbuildsystem,項目名稱:atomic-reactor,代碼行數:18,代碼來源:test_auth.py

示例6: test_not_bearer_auth

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_not_bearer_auth(self):
        url = 'https://registry.example.com/v2/fedora/tags/list'

        def unsupported_callback(request):
            headers = {'www-authenticate': 'Spam realm={}'.format(BEARER_REALM_URL)}
            return (401, headers, json.dumps('unauthorized'))

        responses.add_callback(responses.GET, url, callback=unsupported_callback)
        responses.add(responses.GET, url, status=200, json='success')  # Not actually called

        auth = HTTPBearerAuth()

        response = requests.get(url, auth=auth)
        assert response.json() == 'unauthorized'
        assert response.status_code == 401
        assert len(responses.calls) == 1 
開發者ID:containerbuildsystem,項目名稱:atomic-reactor,代碼行數:18,代碼來源:test_auth.py

示例7: test_get_identities

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_get_identities(self):
        """
        The get_identities function should call the correct URL, and return the relevant identities.
        """
        identity_store = IdentityStore("http://identitystore.org/", "auth-token", "msisdn")

        responses.add_callback(
            responses.GET,
            "http://identitystore.org/api/v1/identities/?details__name=test",
            match_querystring=True,
            callback=self.get_identities_callback,
            content_type="application/json",
        )

        [identity] = identity_store.get_identities(details__name="test")
        self.assertEqual(identity.name, "test") 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:18,代碼來源:test_junebug.py

示例8: test_get_identities_for_address

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_get_identities_for_address(self):
        """
        The get_identities_for_address to create the correct request to list all of the identities for a specified
        address and address type.
        """
        identity_store = IdentityStore("http://identitystore.org/", "auth-token", "msisdn")

        query = "?details__addresses__msisdn=%2B1234"
        url = "http://identitystore.org/api/v1/identities/search/"
        responses.add_callback(
            responses.GET,
            url + query,
            callback=self.single_identity_callback,
            match_querystring=True,
            content_type="application/json",
        )

        [identity] = identity_store.get_identities_for_address("+1234")
        self.assertEqual(identity["details"]["addresses"]["msisdn"], {"+1234": {}}) 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:21,代碼來源:test_junebug.py

示例9: test_create_identity

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_create_identity(self):
        """
        The create_identity method should make the correct request to create an identity with the correct details in the
        identity store.
        """
        identity_store = IdentityStore("http://identitystore.org/", "auth-token", "msisdn")

        url = "http://identitystore.org/api/v1/identities/"
        responses.add_callback(
            responses.POST,
            url,
            callback=self.create_identity_callback,
            match_querystring=True,
            content_type="application/json",
        )

        identity = identity_store.create_identity(["tel:+1234"], name="Test identity", language="eng")
        self.assertEqual(identity["details"], {"addresses": {"msisdn": {"+1234": {}}}, "default_addr_type": "msisdn"}) 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:20,代碼來源:test_junebug.py

示例10: test_should_post_changelog

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def test_should_post_changelog(self, mock_token):
        def request_callback(request):
            payload = json.loads(request.body)
            self.assertEqual(payload["tag_name"], "v1.0.0")
            self.assertEqual(payload["body"], "text")
            self.assertEqual(payload["draft"], False)
            self.assertEqual(payload["prerelease"], False)
            self.assertEqual("token super-token", request.headers["Authorization"])

            return 201, {}, json.dumps({})

        responses.add_callback(
            responses.POST,
            self.url,
            callback=request_callback,
            content_type="application/json",
        )
        status = Github.post_release_changelog("relekang", "rmoq", "1.0.0", "text")
        self.assertTrue(status) 
開發者ID:relekang,項目名稱:python-semantic-release,代碼行數:21,代碼來源:test_hvcs.py

示例11: testRetryDelay

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def testRetryDelay():
    global call
    call = 0
    def request_callback(request):
        global call
        if call == 0 :
            call = 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        elif call == 1 :
            call = 2
            "second attempt"
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true,"details":[]},"data":{}}')
        elif call == 2 :
            call = 3
            return (200, headers,  '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    api.get('labels')
    responses.reset()

# TEST 9 
開發者ID:postmen,項目名稱:postmen-sdk-python,代碼行數:23,代碼來源:postmen_test.py

示例12: testRetryMaxAttempt

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def testRetryMaxAttempt(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 4 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            return (200, headers,  '{"meta":{"code":200,"message":"OK","details":[]},"data":{}}')
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    before = time.time()
    api.get('labels')
    after = time.time()
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)

# TEST 10 
開發者ID:postmen,項目名稱:postmen-sdk-python,代碼行數:22,代碼來源:postmen_test.py

示例13: testRetryMaxAttemptExceeded

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def testRetryMaxAttemptExceeded(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 5 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            pytest.fail("Maximum 5 attempts of retry, test #10 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()

# TEST 11 
開發者ID:postmen,項目名稱:postmen-sdk-python,代碼行數:23,代碼來源:postmen_test.py

示例14: testArguments11

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def testArguments11(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call < 1 :
            call += 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":true, "details":[]},"data":{}}')
        else :
            pytest.fail("Shall not retry if retry = False, test #11 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION', retry=False)
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    assert "PROBLEM" in str(e.value.message())
    assert 999 == e.value.code()
    assert e.value.retryable()

# TEST 12 
開發者ID:postmen,項目名稱:postmen-sdk-python,代碼行數:24,代碼來源:postmen_test.py

示例15: testArgument12

# 需要導入模塊: import responses [as 別名]
# 或者: from responses import add_callback [as 別名]
def testArgument12(monkeypatch):
    monkeypatch.setattr(time, 'sleep', lambda s: None)
    global call
    call = 0
    def request_callback(request):
        global call
        if call == 0 :
            call = 1
            return (200, headers,  '{"meta":{"code":999,"message":"PROBLEM","retryable":false, "details":[]},"data":{}}')
        elif call == 1 :
            pytest.fail("Shall not retry if non retryable, test #12 failed")
    responses.add_callback(responses.GET, 'https://REGION-api.postmen.com/v3/labels', callback=request_callback)
    api = Postmen('KEY', 'REGION')
    with pytest.raises(PostmenException) as e:
        api.get('labels')
    responses.reset()
    # print(e)
    assert "PROBLEM" in str(e.value.message())
    assert not e.value.retryable()
    monkeypatch.setattr(time, 'sleep', lambda s: None)

# TEST 13 
開發者ID:postmen,項目名稱:postmen-sdk-python,代碼行數:24,代碼來源:postmen_test.py


注:本文中的responses.add_callback方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。