本文整理汇总了Python中httpretty.register_uri方法的典型用法代码示例。如果您正苦于以下问题:Python httpretty.register_uri方法的具体用法?Python httpretty.register_uri怎么用?Python httpretty.register_uri使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类httpretty
的用法示例。
在下文中一共展示了httpretty.register_uri方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testDonostia
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def testDonostia(self):
httpretty.register_uri(
httpretty.GET,
self.geocoder.url,
body='{"thanks":"For using an OpenCage Data API","status":{"message":"OK","code":200},"rate":{"remaining":2482,"limit":"2500","reset":1402185600},"total_results":7,"results":[{"geometry":{"lat":"43.3213324","lng":"-1.9856227"},"annotations":{},"components":{"postcode":"20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018","county":"Donostialdea/Donostia-San Sebasti\u00e1n","state":"Basque Country","country":"Spain","city":"San Sebasti\u00e1n","country_code":"es"},"formatted":"San Sebasti\u00e1n, Donostialdea/Donostia-San Sebasti\u00e1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es","bounds":{"southwest":{"lat":"43.2178373","lng":"-2.086808"},"northeast":{"lng":"-1.8878838","lat":"43.3381344"}}},{"formatted":"Donostia, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lat":"43.3422299","lng":"-1.8022744"},"northeast":{"lng":"-1.8013452","lat":"43.3449598"}},"geometry":{"lng":"-1.8019153","lat":"43.3432784"},"annotations":{}},{"annotations":{},"geometry":{"lng":"-1.8022744","lat":"43.3422299"},"formatted":"Donostia, Anaka, Irun, Bidasoa Beherea / Bajo Bidasoa, Basque Country, Spain, es","components":{"county":"Bidasoa Beherea / Bajo Bidasoa","state":"Basque Country","country":"Spain","city":"Irun","suburb":"Anaka","country_code":"es","road":"Donostia"},"bounds":{"southwest":{"lng":"-1.8022971","lat":"43.3421635"},"northeast":{"lng":"-1.8022744","lat":"43.3422299"}}},{"geometry":{"lng":"-2.69312049872164","lat":"42.868297"},"annotations":{},"bounds":{"southwest":{"lng":"-2.6933154","lat":"42.8681484"},"northeast":{"lat":"42.8684357","lng":"-2.6929252"}},"formatted":"Donostia kalea, Ibaiondo, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Ibaiondo","country_code":"es","road":"Donostia kalea"}},{"bounds":{"southwest":{"lng":"-2.6889534","lat":"42.8620967"},"northeast":{"lat":"42.8623764","lng":"-2.6885774"}},"formatted":"Donostia kalea, Lakua, Vitoria-Gasteiz, Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria, Basque Country, Spain, es","components":{"county":"Vitoria-Gasteizko Eskualdea / Cuadrilla de Vitoria","state":"Basque Country","country":"Spain","city":"Vitoria-Gasteiz","suburb":"Lakua","country_code":"es","road":"Donostia kalea"},"geometry":{"lat":"42.8622515","lng":"-2.68876582144679"},"annotations":{}},{"annotations":{},"geometry":{"lat":"51.5146888","lng":"-0.1609307"},"components":{"restaurant":"Donostia","country":"United Kingdom","state_district":"Greater London","country_code":"gb","county":"London","state":"England","suburb":"Marylebone","city":"City of Westminster","road":"Great Cumberland Mews"},"formatted":"Donostia, Great Cumberland Mews, Marylebone, City of Westminster, London, Greater London, England, United Kingdom, gb","bounds":{"northeast":{"lng":"-0.1608807","lat":"51.5147388"},"southwest":{"lat":"51.5146388","lng":"-0.1609807"}}},{"geometry":{"lat":43.31283,"lng":-1.97499},"annotations":{},"bounds":{"northeast":{"lng":"-1.92020404339","lat":"43.3401603699"},"southwest":{"lat":"43.2644081116","lng":"-2.04920697212"}},"formatted":"San Sebastian, Gipuzkoa, Basque Country, Spain, Donostia / San Sebasti\u00e1n","components":{"county":"Gipuzkoa","state":"Basque Country","country":"Spain","town":"San Sebastian","local administrative area":"Donostia / San Sebasti\u00e1n"}}],"timestamp":{"created_unix":1402136556,"created_http":"Sat, 07 Jun 2014 10:22:36 GMT"},"licenses":[{"name":"CC-BY-SA","url":"http://creativecommons.org/licenses/by-sa/3.0/"},{"name":"ODbL","url":"http://opendatacommons.org/licenses/odbl/summary/"}]}',
)
results = self.geocoder.geocode("Donostia")
self.assertTrue(
any((abs(result['geometry']['lat'] - 43.300836) < 0.05 and abs(result['geometry']['lng'] - -1.9809529) < 0.05) for result in results),
msg="Bad result"
)
# test that the results are in unicode
self.assertEqual(results[0]['formatted'], six.u('San Sebasti\xe1n, Donostialdea/Donostia-San Sebasti\xe1n, 20001;20002;20003;20004;20005;20006;20007;20008;20009;20010;20011;20012;20013;20014;20015;20016;20017;20018, Basque Country, Spain, es'))
示例2: testRateLimitExceeded
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def testRateLimitExceeded(self):
httpretty.register_uri(
httpretty.GET,
self.geocoder.url,
body='{"status":{"code":402,"message":"OK"},"thanks":"For using an OpenCage Data API","total_results":0,"licenses":[{"url":"http://creativecommons.org/licenses/by-sa/3.0/","name":"CC-BY-SA"},{"url":"http://opendatacommons.org/licenses/odbl/summary/","name":"ODbL"}],"rate":{"reset":1402185600,"limit":"2500","remaining":0},"results":[],"timestamp":{"created_http":"Sat, 07 Jun 2014 10:38:45 GMT","created_unix":1402137525}}',
status=402,
adding_headers={'X-RateLimit-Limit': '2500', 'X-RateLimit-Remaining': '0', 'X-RateLimit-Reset': '1402185600'},
)
self.assertRaises(RateLimitExceededError, self.geocoder.geocode, "whatever")
# check the exception
try:
self.geocoder.geocode("whatever")
except RateLimitExceededError as ex:
self.assertEqual(str(ex), 'Your rate limit has expired. It will reset to 2500 on 2014-06-08T00:00:00')
self.assertEqual(ex.reset_to, 2500)
示例3: setup
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def setup(self):
httpretty.enable()
self.key = RSAKey(kid='testkey').load(os.path.join(FIXTURE_ROOT, 'testkey.pem'))
def jwks(_request, _uri, headers): # noqa: E306
ks = KEYS()
ks.add(self.key.serialize())
return 200, headers, ks.dump_jwks()
httpretty.register_uri(
httpretty.GET, oidc_rp_settings.PROVIDER_JWKS_ENDPOINT, status=200, body=jwks)
httpretty.register_uri(
httpretty.POST, oidc_rp_settings.PROVIDER_TOKEN_ENDPOINT,
body=json.dumps({
'id_token': self.generate_jws(), 'access_token': 'accesstoken',
'refresh_token': 'refreshtoken', }),
content_type='text/json')
httpretty.register_uri(
httpretty.GET, oidc_rp_settings.PROVIDER_USERINFO_ENDPOINT,
body=json.dumps({'sub': '1234', 'email': 'test@example.com', }),
content_type='text/json')
yield
httpretty.disable()
示例4: test_can_process_userinfo_included_in_the_id_token_instead_of_calling_the_userinfo_endpoint
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_can_process_userinfo_included_in_the_id_token_instead_of_calling_the_userinfo_endpoint(
self, rf):
httpretty.register_uri(
httpretty.POST, oidc_rp_settings.PROVIDER_TOKEN_ENDPOINT,
body=json.dumps({
'id_token': self.generate_jws(email='test1@example.com'),
'access_token': 'accesstoken',
'refresh_token': 'refreshtoken',
}),
content_type='text/json')
request = rf.get('/oidc/cb/', {'state': 'state', 'code': 'authcode', })
SessionMiddleware().process_request(request)
request.session.save()
backend = OIDCAuthBackend()
user = backend.authenticate(request, 'nonce')
assert user.email == 'test1@example.com'
assert user.oidc_user.sub == '1234'
示例5: test_log_out_the_user_if_the_refresh_token_is_expired
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_log_out_the_user_if_the_refresh_token_is_expired(self, rf):
request = rf.get('/oidc/cb/', {'state': 'state', 'code': 'authcode', })
SessionMiddleware().process_request(request)
request.session.save()
backend = OIDCAuthBackend()
user = backend.authenticate(request, 'nonce')
request.session['oidc_auth_id_token_exp_timestamp'] = \
(tz.now() - dt.timedelta(minutes=1)).timestamp()
request.session['oidc_auth_refresh_token'] = 'this_is_a_refresh_token'
auth.login(request, user)
request.user = user
httpretty.register_uri(
httpretty.POST, oidc_rp_settings.PROVIDER_TOKEN_ENDPOINT,
body=json.dumps({'error': 'yes'}),
content_type='text/json', status=400)
middleware = OIDCRefreshIDTokenMiddleware(lambda r: 'OK')
middleware(request)
assert not request.user.is_authenticated
示例6: test_run_respects_feed_type
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_run_respects_feed_type(self):
httpretty.register_uri(httpretty.GET, "http://rss.mock/rss.xml",
body=self.RSS_CONTENT)
messy = threatingestor.sources.rss.Plugin('myrss', 'http://rss.mock/rss.xml', 'messy')
clean = threatingestor.sources.rss.Plugin('testrss', 'http://rss.mock/rss.xml', 'clean')
afterioc = threatingestor.sources.rss.Plugin('rsss', 'http://rss.mock/rss.xml', 'afterioc')
saved_state, artifacts = messy.run(None)
self.assertEqual(len(artifacts), 8)
self.assertIn('http://example.com/bad/url/10', [str(x) for x in artifacts])
self.assertNotIn('http://example.com/good/url', [str(x) for x in artifacts])
self.assertNotIn('http://example.com/bad/url/00', [str(x) for x in artifacts])
saved_state, artifacts = clean.run(None)
self.assertEqual(len(artifacts), 14)
self.assertIn('http://example.com/bad/url/10', [str(x) for x in artifacts])
self.assertIn('http://example.com/good/url', [str(x) for x in artifacts])
self.assertIn('http://example.com/bad/url/00', [str(x) for x in artifacts])
saved_state, artifacts = afterioc.run(None)
self.assertEqual(len(artifacts), 12)
self.assertIn('http://example.com/bad/url/10', [str(x) for x in artifacts])
self.assertNotIn('http://example.com/good/url', [str(x) for x in artifacts])
self.assertIn('http://example.com/bad/url/00', [str(x) for x in artifacts])
示例7: test_run_supports_both_link_url
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_run_supports_both_link_url(self):
httpretty.register_uri(httpretty.GET, "http://rss.mock/rss.xml",
body=self.RSS_CONTENT)
saved_state, artifacts = self.rss.run(None)
# link
self.assertIn('http://example.com/bad/url/10', [str(x) for x in artifacts])
self.assertIn('https://www.rss.mock/some/url', [a.reference_link for a in artifacts])
afterioc = threatingestor.sources.rss.Plugin('test', 'http://rss.mock/rss.xml', 'afterioc')
saved_state, artifacts = afterioc.run(None)
# fallback to url
self.assertIn('http://example.com/bad/url/00', [str(x) for x in artifacts])
self.assertEqual(artifacts[0].reference_link, 'http://rss.mock/rss.xml')
示例8: test_descriptor_dereferencing_uri_remote
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_descriptor_dereferencing_uri_remote():
# Mocks
httpretty.register_uri(httpretty.GET,
'http://example.com/schema', body='{"fields": [{"name": "name"}]}')
httpretty.register_uri(httpretty.GET,
'https://example.com/dialect', body='{"delimiter": ","}')
# Tests
descriptor = {
'resources': [
{'name': 'name1', 'data': 'data', 'schema': 'http://example.com/schema'},
{'name': 'name2', 'data': 'data', 'dialect': 'https://example.com/dialect'},
],
}
package = Package(descriptor)
assert package.descriptor['resources'] == list(map(helpers.expand_resource_descriptor, [
{'name': 'name1', 'data': 'data', 'schema': {'fields': [{'name': 'name'}]}},
{'name': 'name2', 'data': 'data', 'dialect': {'delimiter': ','}},
]))
示例9: test_init_accepts_urls
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_init_accepts_urls():
url = 'http://some-place.com/registry.json'
body = """
[
{
"id": "data-package",
"title": "Data Package",
"schema": "https://specs.frictionlessdata.io/schemas/data-package.json",
"schema_path": "data-package.json",
"specification": "https://specs.frictionlessdata.io/data-package/"
}
]
"""
httpretty.register_uri(httpretty.GET, url, body=body)
registry = datapackage.registry.Registry(url)
assert len(registry.available_profiles) == 1
assert registry.available_profiles.get('data-package') == {
'id': 'data-package',
'title': 'Data Package',
'schema': 'https://specs.frictionlessdata.io/schemas/data-package.json',
'schema_path': 'data-package.json',
'specification': 'https://specs.frictionlessdata.io/data-package/',
}
示例10: test_get_loads_remote_file_if_local_copy_doesnt_exist
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_get_loads_remote_file_if_local_copy_doesnt_exist():
registry_body = """
[
{
"id": "data-package",
"title": "Data Package",
"schema": "http://example.com/one.json",
"schema_path": "data-package.json",
"specification": "https://specs.frictionlessdata.io/data-package/"
}
]
"""
profile_url = 'http://example.com/one.json'
profile_body = '{ "profile": "data-package" }'
httpretty.register_uri(httpretty.GET, profile_url, body=profile_body)
with tempfile.NamedTemporaryFile(suffix='.csv') as tmpfile:
tmpfile.write(registry_body.encode('utf-8'))
tmpfile.flush()
registry = datapackage.registry.Registry(tmpfile.name)
base_profile = registry.get('data-package')
assert base_profile is not None
assert base_profile == {'profile': 'data-package'}
示例11: test_get_raises_if_remote_profile_file_doesnt_exist
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_get_raises_if_remote_profile_file_doesnt_exist():
registry_url = 'http://example.com/registry.json'
registry_body = """
[
{
"id": "data-package",
"title": "Data Package",
"schema": "http://example.com/one.json",
"schema_path": "data-package.json",
"specification": "http://example.com"
}
]
"""
httpretty.register_uri(httpretty.GET, registry_url, body=registry_body)
profile_url = 'http://example.com/one.json'
httpretty.register_uri(httpretty.GET, profile_url, status=404)
registry = datapackage.registry.Registry(registry_url)
with pytest.raises(RegistryError):
registry.get('data-package')
示例12: test_get_raises_if_local_profile_file_doesnt_exist
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_get_raises_if_local_profile_file_doesnt_exist():
registry_body = """
[
{
"id": "data-package",
"title": "Data Package",
"schema": "http://example.com/one.json",
"schema_path": "inexistent.json",
"specification": "http://example.com"
}
]
"""
with tempfile.NamedTemporaryFile(suffix='.csv') as tmpfile:
tmpfile.write(registry_body.encode('utf-8'))
tmpfile.flush()
registry = datapackage.registry.Registry(tmpfile.name)
profile_url = 'http://example.com/one.json'
httpretty.register_uri(httpretty.GET, profile_url, status=404)
with pytest.raises(RegistryError):
registry.get('data-package')
示例13: test_answer_distribution_response
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_answer_distribution_response(self):
""" Verifies that answer distribution responds with the expected values. """
body = [
{
'course_id': self.course_id,
'module_id': self.module_id,
'part_id': self.module_id.replace('/', '-') + '-part1',
'correct': True,
'first_response_count': 2,
'last_response_count': 2,
'value_id': 'choice_4',
'answer_value_text': 'User chose this answer.',
'answer_value_numeric': None,
'variant': 123,
'created': "2014-01-01T00:01:00"
}
]
uri = self.get_api_url('problems/{0}/answer_distribution/'.format(self.module_id))
httpretty.register_uri(httpretty.GET, uri, body=json.dumps(body))
self.assertEqual(body, self.module.answer_distribution())
示例14: test_grade_distribution_response
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def test_grade_distribution_response(self):
""" Verifies that the grade distribution responds with the expected values. """
body = [
{
'module_id': self.module_id,
'course_id': self.course_id,
'grade': 0,
'max_grade': 1,
'count': 1,
'created': '2014-01-01T00:01:00'
}
]
uri = self.get_api_url('problems/{0}/grade_distribution/'.format(self.module_id))
httpretty.register_uri(httpretty.GET, uri, body=json.dumps(body))
self.assertEqual(body, self.module.grade_distribution())
示例15: assertCorrectEnrollmentUrl
# 需要导入模块: import httpretty [as 别名]
# 或者: from httpretty import register_uri [as 别名]
def assertCorrectEnrollmentUrl(self, course, demographic=None):
""" Verifies that the enrollment URL is correct. """
uri = self.get_api_url('courses/{0}/enrollment/'.format(course.course_id))
if demographic:
uri += '%s/' % demographic
httpretty.register_uri(httpretty.GET, uri, body='{}')
course.enrollment(demographic)
date = '2014-01-01'
httpretty.reset()
httpretty.register_uri(httpretty.GET, '{0}?start_date={1}'.format(uri, date), body='{}')
course.enrollment(demographic, start_date=date)
httpretty.reset()
httpretty.register_uri(httpretty.GET, '{0}?end_date={1}'.format(uri, date), body='{}')
course.enrollment(demographic, end_date=date)
httpretty.reset()
httpretty.register_uri(httpretty.GET, '{0}?start_date={1}&end_date={1}'.format(uri, date), body='{}')
course.enrollment(demographic, start_date=date, end_date=date)