本文整理汇总了Python中falcon.routing.DefaultRouter.find方法的典型用法代码示例。如果您正苦于以下问题:Python DefaultRouter.find方法的具体用法?Python DefaultRouter.find怎么用?Python DefaultRouter.find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类falcon.routing.DefaultRouter
的用法示例。
在下文中一共展示了DefaultRouter.find方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_match_entire_path
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
def test_match_entire_path(uri_template, path):
router = DefaultRouter()
router.add_route(uri_template, {}, ResourceWithId(1))
route = router.find(path)
assert route is None
示例2: test_user_regression_recipes
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
def test_user_regression_recipes():
router = DefaultRouter()
router.add_route(
'/recipes/{activity}/{type_id}',
ResourceWithId(1)
)
router.add_route(
'/recipes/baking',
ResourceWithId(2)
)
resource, __, __, __ = router.find('/recipes/baking/4242')
assert resource.resource_id == 1
resource, __, __, __ = router.find('/recipes/baking')
assert resource.resource_id == 2
route = router.find('/recipes/grilling')
assert route is None
示例3: test_user_regression_special_chars
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
def test_user_regression_special_chars(uri_template, path, expected_params):
router = DefaultRouter()
router.add_route(uri_template, {}, ResourceWithId(1))
route = router.find(path)
assert route is not None
resource, __, params, __ = route
assert resource.resource_id == 1
assert params == expected_params
示例4: test_user_regression_versioned_url
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
def test_user_regression_versioned_url():
router = DefaultRouter()
router.add_route('/{version}/messages', {}, ResourceWithId(2))
resource, __, __, __ = router.find('/v2/messages')
assert resource.resource_id == 2
router.add_route('/v2', {}, ResourceWithId(1))
resource, __, __, __ = router.find('/v2')
assert resource.resource_id == 1
resource, __, __, __ = router.find('/v2/messages')
assert resource.resource_id == 2
resource, __, __, __ = router.find('/v1/messages')
assert resource.resource_id == 2
route = router.find('/v1')
assert route is None
示例5: TestRegressionCases
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
class TestRegressionCases(testing.TestBase):
"""Test specific repros reported by users of the framework."""
def before(self):
self.router = DefaultRouter()
def test_versioned_url(self):
self.router.add_route('/{version}/messages', {}, ResourceWithId(2))
resource, method_map, params = self.router.find('/v2/messages')
self.assertEqual(resource.resource_id, 2)
self.router.add_route('/v2', {}, ResourceWithId(1))
resource, method_map, params = self.router.find('/v2')
self.assertEqual(resource.resource_id, 1)
resource, method_map, params = self.router.find('/v2/messages')
self.assertEqual(resource.resource_id, 2)
resource, method_map, params = self.router.find('/v1/messages')
self.assertEqual(resource.resource_id, 2)
route = self.router.find('/v1')
self.assertIs(route, None)
def test_recipes(self):
self.router.add_route(
'/recipes/{activity}/{type_id}', {}, ResourceWithId(1))
self.router.add_route(
'/recipes/baking', {}, ResourceWithId(2))
resource, method_map, params = self.router.find('/recipes/baking/4242')
self.assertEqual(resource.resource_id, 1)
resource, method_map, params = self.router.find('/recipes/baking')
self.assertEqual(resource.resource_id, 2)
route = self.router.find('/recipes/grilling')
self.assertIs(route, None)
示例6: test_root_path
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
def test_root_path():
router = DefaultRouter()
router.add_route('/', {}, ResourceWithId(42))
resource, __, __, __ = router.find('/')
assert resource.resource_id == 42
expected_src = textwrap.dedent("""
def find(path, return_values, patterns, converters, params):
path_len = len(path)
if path_len > 0:
if path[0] == '':
if path_len == 1:
return return_values[0]
return None
return None
return None
""").strip()
assert router.finder_src == expected_src
示例7: TestComplexRouting
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
class TestComplexRouting(testing.TestBase):
def before(self):
self.router = DefaultRouter()
self.router.add_route(
'/repos', {}, ResourceWithId(1))
self.router.add_route(
'/repos/{org}', {}, ResourceWithId(2))
self.router.add_route(
'/repos/{org}/{repo}', {}, ResourceWithId(3))
self.router.add_route(
'/repos/{org}/{repo}/commits', {}, ResourceWithId(4))
self.router.add_route(
'/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}',
{}, ResourceWithId(5))
self.router.add_route(
'/teams/{id}', {}, ResourceWithId(6))
self.router.add_route(
'/teams/{id}/members', {}, ResourceWithId(7))
self.router.add_route(
'/teams/default', {}, ResourceWithId(19))
self.router.add_route(
'/teams/default/members/thing', {}, ResourceWithId(19))
self.router.add_route(
'/user/memberships', {}, ResourceWithId(8))
self.router.add_route(
'/emojis', {}, ResourceWithId(9))
self.router.add_route(
'/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/full',
{}, ResourceWithId(10))
self.router.add_route(
'/repos/{org}/{repo}/compare/all', {}, ResourceWithId(11))
# NOTE(kgriffs): The ordering of these calls is significant; we
# need to test that the {id} field does not match the other routes,
# regardless of the order they are added.
self.router.add_route(
'/emojis/signs/0', {}, ResourceWithId(12))
self.router.add_route(
'/emojis/signs/{id}', {}, ResourceWithId(13))
self.router.add_route(
'/emojis/signs/42', {}, ResourceWithId(14))
self.router.add_route(
'/emojis/signs/42/small', {}, ResourceWithId(14.1))
self.router.add_route(
'/emojis/signs/78/small', {}, ResourceWithId(22))
self.router.add_route(
'/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/part',
{}, ResourceWithId(15))
self.router.add_route(
'/repos/{org}/{repo}/compare/{usr0}:{branch0}',
{}, ResourceWithId(16))
self.router.add_route(
'/repos/{org}/{repo}/compare/{usr0}:{branch0}/full',
{}, ResourceWithId(17))
self.router.add_route(
'/gists/{id}/{representation}', {}, ResourceWithId(21))
self.router.add_route(
'/gists/{id}/raw', {}, ResourceWithId(18))
self.router.add_route(
'/gists/first', {}, ResourceWithId(20))
@ddt.data(
'/teams/{collision}', # simple vs simple
'/emojis/signs/{id_too}', # another simple vs simple
'/repos/{org}/{repo}/compare/{complex}:{vs}...{complex2}:{collision}',
)
def test_collision(self, template):
self.assertRaises(
ValueError,
self.router.add_route, template, {}, ResourceWithId(-1)
)
@ddt.data(
'/repos/{complex}.{vs}.{simple}',
'/repos/{org}/{repo}/compare/{complex}:{vs}...{complex2}/full',
)
def test_non_collision(self, template):
self.router.add_route(template, {}, ResourceWithId(-1))
def test_dump(self):
print(self.router._src)
def test_override(self):
self.router.add_route('/emojis/signs/0', {}, ResourceWithId(-1))
resource, method_map, params = self.router.find('/emojis/signs/0')
self.assertEqual(resource.resource_id, -1)
def test_literal_segment(self):
resource, method_map, params = self.router.find('/emojis/signs/0')
self.assertEqual(resource.resource_id, 12)
resource, method_map, params = self.router.find('/emojis/signs/1')
self.assertEqual(resource.resource_id, 13)
#.........这里部分代码省略.........
示例8: TestStandaloneRouter
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
class TestStandaloneRouter(testing.TestBase):
def before(self):
from falcon.routing import DefaultRouter
self.router = DefaultRouter()
setup_routes(self.router)
@ddt.data(
'/teams/{collision}',
'/repos/{org}/{repo}/compare/{simple-collision}',
'/emojis/signs/1',
)
def test_collision(self, template):
self.assertRaises(
ValueError,
self.router.add_route, template, {}, ResourceWithId(6)
)
def test_missing(self):
resource, method_map, params = self.router.find('/this/does/not/exist')
self.assertIs(resource, None)
resource, method_map, params = self.router.find('/user/bogus')
self.assertIs(resource, None)
resource, method_map, params = self.router.find('/teams/1234/bogus')
self.assertIs(resource, None)
resource, method_map, params = self.router.find(
'/repos/racker/falcon/compare/johndoe:master...janedoe:dev/bogus')
self.assertIs(resource, None)
def test_literal_segment(self):
resource, method_map, params = self.router.find('/emojis/signs/0')
self.assertEqual(resource.resource_id, 12)
resource, method_map, params = self.router.find('/emojis/signs/1')
self.assertEqual(resource.resource_id, 13)
def test_dead_segment(self):
resource, method_map, params = self.router.find('/teams')
self.assertIs(resource, None)
resource, method_map, params = self.router.find('/emojis/signs')
self.assertIs(resource, None)
resource, method_map, params = self.router.find('/emojis/signs/stop')
self.assertEqual(params, {
'id': 'stop',
})
def test_malformed_pattern(self):
resource, method_map, params = self.router.find(
'/repos/racker/falcon/compare/foo')
self.assertIs(resource, None)
resource, method_map, params = self.router.find(
'/repos/racker/falcon/compare/foo/full')
self.assertIs(resource, None)
def test_literal(self):
resource, method_map, params = self.router.find('/user/memberships')
self.assertEqual(resource.resource_id, 8)
def test_variable(self):
resource, method_map, params = self.router.find('/teams/42')
self.assertEqual(resource.resource_id, 6)
self.assertEqual(params, {'id': '42'})
def test_multivar(self):
resource, method_map, params = self.router.find(
'/repos/racker/falcon/commits')
self.assertEqual(resource.resource_id, 4)
self.assertEqual(params, {'org': 'racker', 'repo': 'falcon'})
resource, method_map, params = self.router.find(
'/repos/racker/falcon/compare/all')
self.assertEqual(resource.resource_id, 11)
self.assertEqual(params, {'org': 'racker', 'repo': 'falcon'})
@ddt.data(('', 5), ('/full', 10), ('/part', 14))
@ddt.unpack
def test_complex(self, url_postfix, resource_id):
uri = '/repos/racker/falcon/compare/johndoe:master...janedoe:dev'
resource, method_map, params = self.router.find(uri + url_postfix)
self.assertEqual(resource.resource_id, resource_id)
self.assertEqual(params, {
'org': 'racker',
'repo': 'falcon',
'usr0': 'johndoe',
'branch0': 'master',
'usr1': 'janedoe',
'branch1': 'dev'
})
@ddt.data(('', 15), ('/full', 16))
@ddt.unpack
def test_complex_alt(self, url_postfix, resource_id):
uri = '/repos/falconry/falcon/compare/johndoe:master'
resource, method_map, params = self.router.find(uri + url_postfix)
#.........这里部分代码省略.........
示例9: TestComplexRouting
# 需要导入模块: from falcon.routing import DefaultRouter [as 别名]
# 或者: from falcon.routing.DefaultRouter import find [as 别名]
class TestComplexRouting(testing.TestBase):
def before(self):
self.router = DefaultRouter()
self.router.add_route("/repos", {}, ResourceWithId(1))
self.router.add_route("/repos/{org}", {}, ResourceWithId(2))
self.router.add_route("/repos/{org}/{repo}", {}, ResourceWithId(3))
self.router.add_route("/repos/{org}/{repo}/commits", {}, ResourceWithId(4))
self.router.add_route("/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}", {}, ResourceWithId(5))
self.router.add_route("/teams/{id}", {}, ResourceWithId(6))
self.router.add_route("/teams/{id}/members", {}, ResourceWithId(7))
self.router.add_route("/teams/default", {}, ResourceWithId(19))
self.router.add_route("/teams/default/members/thing", {}, ResourceWithId(19))
self.router.add_route("/user/memberships", {}, ResourceWithId(8))
self.router.add_route("/emojis", {}, ResourceWithId(9))
self.router.add_route(
"/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/full", {}, ResourceWithId(10)
)
self.router.add_route("/repos/{org}/{repo}/compare/all", {}, ResourceWithId(11))
# NOTE(kgriffs): The ordering of these calls is significant; we
# need to test that the {id} field does not match the other routes,
# regardless of the order they are added.
self.router.add_route("/emojis/signs/0", {}, ResourceWithId(12))
self.router.add_route("/emojis/signs/{id}", {}, ResourceWithId(13))
self.router.add_route("/emojis/signs/42", {}, ResourceWithId(14))
self.router.add_route("/emojis/signs/42/small", {}, ResourceWithId(14.1))
self.router.add_route("/emojis/signs/78/small", {}, ResourceWithId(22))
self.router.add_route(
"/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/part", {}, ResourceWithId(15)
)
self.router.add_route("/repos/{org}/{repo}/compare/{usr0}:{branch0}", {}, ResourceWithId(16))
self.router.add_route("/repos/{org}/{repo}/compare/{usr0}:{branch0}/full", {}, ResourceWithId(17))
self.router.add_route("/gists/{id}/{representation}", {}, ResourceWithId(21))
self.router.add_route("/gists/{id}/raw", {}, ResourceWithId(18))
self.router.add_route("/gists/first", {}, ResourceWithId(20))
@ddt.data(
"/teams/{collision}", # simple vs simple
"/emojis/signs/{id_too}", # another simple vs simple
"/repos/{org}/{repo}/compare/{complex}:{vs}...{complex2}:{collision}",
)
def test_collision(self, template):
self.assertRaises(ValueError, self.router.add_route, template, {}, ResourceWithId(-1))
@ddt.data(
"/repos/{org}/{repo}/compare/{simple-vs-complex}",
"/repos/{complex}.{vs}.{simple}",
"/repos/{org}/{repo}/compare/{complex}:{vs}...{complex2}/full",
)
def test_non_collision(self, template):
self.router.add_route(template, {}, ResourceWithId(-1))
def test_dump(self):
print(self.router._src)
def test_override(self):
self.router.add_route("/emojis/signs/0", {}, ResourceWithId(-1))
resource, method_map, params = self.router.find("/emojis/signs/0")
self.assertEqual(resource.resource_id, -1)
def test_literal_segment(self):
resource, method_map, params = self.router.find("/emojis/signs/0")
self.assertEqual(resource.resource_id, 12)
resource, method_map, params = self.router.find("/emojis/signs/1")
self.assertEqual(resource.resource_id, 13)
resource, method_map, params = self.router.find("/emojis/signs/42")
self.assertEqual(resource.resource_id, 14)
resource, method_map, params = self.router.find("/emojis/signs/42/small")
self.assertEqual(resource.resource_id, 14.1)
route = self.router.find("/emojis/signs/1/small")
self.assertEqual(route, None)
@ddt.data("/teams", "/emojis/signs", "/gists", "/gists/42")
def test_dead_segment(self, template):
route = self.router.find(template)
self.assertIs(route, None)
def test_malformed_pattern(self):
route = self.router.find("/repos/racker/falcon/compare/foo")
self.assertIs(route, None)
route = self.router.find("/repos/racker/falcon/compare/foo/full")
self.assertIs(route, None)
def test_literal(self):
resource, method_map, params = self.router.find("/user/memberships")
self.assertEqual(resource.resource_id, 8)
def test_variable(self):
#.........这里部分代码省略.........