Python源码示例:responses.calls()
示例1
def test_should_register_dynamic_client_if_client_registration_info_is_given(self):
registration_endpoint = self.PROVIDER_BASEURL + '/register'
responses.add(responses.POST, registration_endpoint, json={'client_id': 'client1', 'client_secret': 'secret1'})
provider_config = ProviderConfiguration(
provider_metadata=self.provider_metadata(registration_endpoint=registration_endpoint),
client_registration_info=ClientRegistrationInfo())
extra_args = {'extra_args': 'should be passed'}
redirect_uris = ['https://client.example.com/redirect']
provider_config.register_client(redirect_uris, extra_args)
assert provider_config._client_metadata['client_id'] == 'client1'
assert provider_config._client_metadata['client_secret'] == 'secret1'
assert provider_config._client_metadata['redirect_uris'] == redirect_uris
expected_registration_request = {'redirect_uris': redirect_uris}
expected_registration_request.update(extra_args)
assert json.loads(responses.calls[0].request.body.decode('utf-8')) == expected_registration_request
示例2
def test_interest_form_post_triggers_slack_notification(self, testapp):
''' A valid interest form post triggers a Slack notification.
'''
# set a fake Slack webhook URL
fake_webhook_url = 'http://webhook.example.com/'
current_app.config['SLACK_WEBHOOK_URL'] = fake_webhook_url
# create a mock to receive POST requests to that URL
responses.add(responses.POST, fake_webhook_url, status=200)
# post an interest form submission
testapp.post("/interest/", params=dict(name="Jean Weaver", agency="Clinton Police Department", location="Clinton, OK", phone="580-970-3338", email="jean.weaver@example.com", comments="I'm interested in Comport as an open-source tool!"))
# test the captured post payload
post_body = json.loads(responses.calls[0].request.body)
assert 'New Interest Form Submission!' in post_body['text']
# delete the fake Slack webhook URL
del(current_app.config['SLACK_WEBHOOK_URL'])
# reset the mock
responses.reset()
示例3
def test_extract_link(self):
success_response = {'success': True, 'data': 'link'}
responses.add(responses.POST, constants.ENDPOINTS['extract_link'],
json=success_response,
status=200)
responses.add(responses.POST, constants.ENDPOINTS['extract_link'],
status=401)
# success call
result = utils.extract_link(self.session, 'https://www.ojbk.com')
self.assertEqual(result, 'link')
self.assertEqual(len(responses.calls), 1)
self.assertEqual(responses.calls[0].request.url, constants.ENDPOINTS['extract_link'])
self.assertEqual(responses.calls[0].response.json(), success_response)
# failed call
with self.assertRaises(requests.HTTPError) as cm:
utils.extract_link(self.session, 'https://www.ojbk.com')
self.assertEqual(len(responses.calls), 2)
self.assertEqual(cm.exception.response.status_code, 401)
示例4
def test_get_uptoken(self):
params = {'bucket': 'jike'}
success_reponse = {'uptoken': 'token'}
responses.add(responses.GET, constants.ENDPOINTS['picture_uptoken'],
json=success_reponse, status=200)
responses.add(responses.GET, constants.ENDPOINTS['picture_uptoken'],
status=404)
# success call
result = utils.get_uptoken()
self.assertEqual(result, 'token')
self.assertEqual(len(responses.calls), 1)
self.assertEqual(responses.calls[0].request.url,
constants.ENDPOINTS['picture_uptoken'] + '?' + urlencode(params))
self.assertEqual(responses.calls[0].response.json(), success_reponse)
# failed call
with self.assertRaises(requests.HTTPError) as cm:
utils.get_uptoken()
self.assertEqual(len(responses.calls), 2)
self.assertEqual(cm.exception.response.status_code, 404)
示例5
def test_address(self):
responses.add(responses.POST, self.jsonrpc_url,
json=self._read('test_address-00-get_accounts.json'),
status=200)
responses.add(responses.POST, self.jsonrpc_url,
json=self._read('test_address-10-getaddress.json'),
status=200)
responses.add(responses.POST, self.jsonrpc_url,
json=self._read('test_address-10-getaddress.json'),
status=200)
self.wallet = Wallet(JSONRPCWallet())
waddr = self.wallet.address()
a0addr = self.wallet.accounts[0].address()
self.assertEqual(len(responses.calls), 3)
self.assertEqual(waddr, a0addr)
self.assertIsInstance(waddr, Address)
self.assertEqual(
waddr,
'596ETuuDVZSNox73YLctrHaAv72fBboxy3atbEMnP3QtdnGFS9KWuHYGuy831SKWLUVCgrRfWLCxuCZ2fbVGh14X7mFrefy')
self.assertEqual(waddr.label, 'Primary account')
self.assertEqual(a0addr.label, 'Primary account')
示例6
def test_request_made(self):
responses.add(responses.GET, 'https://example.com', status=200)
donation = Donation.objects.create(
timereceived=datetime.datetime(2018, 1, 1),
comment='',
amount=Decimal(1.5),
domain='PAYPAL',
donor=self.donor,
event=self.event,
)
eventutil.post_donation_to_postbacks(donation)
assert len(responses.calls) == 1
assert (
responses.calls[0].request.url
== 'https://example.com/?comment=&amount=1.5&timereceived=2018-01-01+00%3A00%3A00&donor__visibility=FIRST&domain=PAYPAL&id=1&donor__visiblename=%28No+Name%29'
)
assert responses.calls[0].response.status_code == 200
示例7
def test_context_manager():
testutil.add_response("login_response_200")
testutil.add_response("query_response_200")
testutil.add_response("logout_response_200")
client_args = {
"username": testutil.username,
"password": testutil.password,
"client_id": testutil.client_id,
"client_secret": testutil.client_secret,
"version": "37.0"}
with sfdc.client(**client_args) as client:
client.query("SELECT Id, Name FROM Account LIMIT 10")
"""
The above should have made 3 calls: login, query, logout
"""
assert len(responses.calls) == 3
示例8
def test_context_manager_negative():
testutil.add_response("login_response_200")
testutil.add_response("query_response_200")
client_args = {
"username": testutil.username,
"password": testutil.password,
"client_id": testutil.client_id,
"client_secret": testutil.client_secret,
"version": "37.0"}
def logout():
raise Exception("Monkey patchin'...")
with sfdc.client(**client_args) as client:
client.query("SELECT Id, Name FROM Account LIMIT 10")
client.logout = logout
"""
The above should have made 2 calls: login, query
"""
assert len(responses.calls) == 2
示例9
def test_los_angeles(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/timezone/json",
body='{"status":"OK"}',
status=200,
content_type="application/json",
)
ts = 1331766000
timezone = self.client.timezone((39.603481, -119.682251), ts)
self.assertIsNotNone(timezone)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/timezone/json"
"?location=39.603481,-119.682251×tamp=%d"
"&key=%s" % (ts, self.key),
responses.calls[0].request.url,
)
示例10
def test_los_angeles_with_no_timestamp(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/timezone/json",
body='{"status":"OK"}',
status=200,
content_type="application/json",
)
timezone = self.client.timezone((39.603481, -119.682251))
self.assertIsNotNone(timezone)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/timezone/json"
"?location=39.603481,-119.682251×tamp=%d"
"&key=%s" % (1608, self.key),
responses.calls[0].request.url,
)
示例11
def test_simple_geolocate(self):
responses.add(
responses.POST,
"https://www.googleapis.com/geolocation/v1/geolocate",
body='{"location": {"lat": 51.0,"lng": -0.1},"accuracy": 1200.4}',
status=200,
content_type="application/json",
)
results = self.client.geolocate()
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://www.googleapis.com/geolocation/v1/geolocate?" "key=%s" % self.key,
responses.calls[0].request.url,
)
示例12
def test_autocomplete_query(self):
url = "https://maps.googleapis.com/maps/api/place/queryautocomplete/json"
responses.add(
responses.GET,
url,
body='{"status": "OK", "predictions": []}',
status=200,
content_type="application/json",
)
self.client.places_autocomplete_query("pizza near New York")
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"%s?input=pizza+near+New+York&key=%s" % (url, self.key),
responses.calls[0].request.url,
)
示例13
def test_elevation_single(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.elevation((40.714728, -73.998672))
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/elevation/json?"
"locations=enc:abowFtzsbM&key=%s" % self.key,
responses.calls[0].request.url,
)
示例14
def test_elevation_multiple(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
locations = [(40.714728, -73.998672), (-34.397, 150.644)]
results = self.client.elevation(locations)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/elevation/json?"
"locations=enc:abowFtzsbMhgmiMuobzi@&key=%s" % self.key,
responses.calls[0].request.url,
)
示例15
def test_elevation_along_path(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
path = [(40.714728, -73.998672), (-34.397, 150.644)]
results = self.client.elevation_along_path(path, 5)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/elevation/json?"
"path=enc:abowFtzsbMhgmiMuobzi@&"
"key=%s&samples=5" % self.key,
responses.calls[0].request.url,
)
示例16
def test_short_latlng(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/elevation/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.elevation((40, -73))
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/elevation/json?"
"locations=40,-73&key=%s" % self.key,
responses.calls[0].request.url,
)
示例17
def test_mixed_params(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/distancematrix/json",
body='{"status":"OK","rows":[]}',
status=200,
content_type="application/json",
)
origins = ["Bobcaygeon ON", [41.43206, -81.38992]]
destinations = [
(43.012486, -83.6964149),
{"lat": 42.8863855, "lng": -78.8781627},
]
matrix = self.client.distance_matrix(origins, destinations)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/distancematrix/json?"
"key=%s&origins=Bobcaygeon+ON%%7C41.43206%%2C-81.38992&"
"destinations=43.012486%%2C-83.6964149%%7C42.8863855%%2C"
"-78.8781627" % self.key,
responses.calls[0].request.url,
)
示例18
def test_lang_param(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/distancematrix/json",
body='{"status":"OK","rows":[]}',
status=200,
content_type="application/json",
)
origins = ["Vancouver BC", "Seattle"]
destinations = ["San Francisco", "Victoria BC"]
matrix = self.client.distance_matrix(
origins, destinations, language="fr-FR", mode="bicycling"
)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/distancematrix/json?"
"key=%s&language=fr-FR&mode=bicycling&"
"origins=Vancouver+BC%%7CSeattle&"
"destinations=San+Francisco%%7CVictoria+BC" % self.key,
responses.calls[0].request.url,
)
示例19
def test_simple_geocode(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode("Sydney")
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=%s&address=Sydney" % self.key,
responses.calls[0].request.url,
)
示例20
def test_reverse_geocode(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.reverse_geocode((-33.8674869, 151.2069902))
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"latlng=-33.8674869,151.2069902&key=%s" % self.key,
responses.calls[0].request.url,
)
示例21
def test_geocoding_the_googleplex(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode("1600 Amphitheatre Parkway, " "Mountain View, CA")
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=%s&address=1600+Amphitheatre+Parkway%%2C+Mountain"
"+View%%2C+CA" % self.key,
responses.calls[0].request.url,
)
示例22
def test_geocode_with_region_biasing(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode("Toledo", region="es")
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"region=es&key=%s&address=Toledo" % self.key,
responses.calls[0].request.url,
)
示例23
def test_geocode_with_component_filter(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode("santa cruz", components={"country": "ES"})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=%s&components=country%%3AES&address=santa+cruz" % self.key,
responses.calls[0].request.url,
)
示例24
def test_geocode_with_multiple_component_filters(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode(
"Torun", components={"administrative_area": "TX", "country": "US"}
)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=%s&components=administrative_area%%3ATX%%7C"
"country%%3AUS&address=Torun" % self.key,
responses.calls[0].request.url,
)
示例25
def test_geocode_with_just_components(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode(
components={
"route": "Annegatan",
"administrative_area": "Helsinki",
"country": "Finland",
}
)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=%s&components=administrative_area%%3AHelsinki"
"%%7Ccountry%%3AFinland%%7Croute%%3AAnnegatan" % self.key,
responses.calls[0].request.url,
)
示例26
def test_simple_reverse_geocode(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.reverse_geocode((40.714224, -73.961452))
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"latlng=40.714224%%2C-73.961452&key=%s" % self.key,
responses.calls[0].request.url,
)
示例27
def test_reverse_geocode_multiple_location_types(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.reverse_geocode(
(40.714224, -73.961452),
location_type=["ROOFTOP", "RANGE_INTERPOLATED"],
result_type="street_address",
)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"latlng=40.714224%%2C-73.961452&result_type=street_address&"
"key=%s&location_type=ROOFTOP%%7CRANGE_INTERPOLATED" % self.key,
responses.calls[0].request.url,
)
示例28
def test_reverse_geocode_multiple_result_types(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.reverse_geocode(
(40.714224, -73.961452),
location_type="ROOFTOP",
result_type=["street_address", "route"],
)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"latlng=40.714224%%2C-73.961452&result_type=street_address"
"%%7Croute&key=%s&location_type=ROOFTOP" % self.key,
responses.calls[0].request.url,
)
示例29
def test_partial_match(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode("Pirrama Pyrmont")
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=%s&address=Pirrama+Pyrmont" % self.key,
responses.calls[0].request.url,
)
示例30
def test_utf_results(self):
responses.add(
responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
body='{"status":"OK","results":[]}',
status=200,
content_type="application/json",
)
results = self.client.geocode(components={"postal_code": "96766"})
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://maps.googleapis.com/maps/api/geocode/json?"
"key=%s&components=postal_code%%3A96766" % self.key,
responses.calls[0].request.url,
)