本文整理汇总了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)
示例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)
示例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))
示例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
示例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
示例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
示例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")
示例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": {}})
示例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"})
示例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)
示例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
示例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
示例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
示例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
示例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