当前位置: 首页>>代码示例>>Python>>正文


Python routing.DefaultRouter类代码示例

本文整理汇总了Python中falcon.routing.DefaultRouter的典型用法代码示例。如果您正苦于以下问题:Python DefaultRouter类的具体用法?Python DefaultRouter怎么用?Python DefaultRouter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DefaultRouter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_match_entire_path

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
开发者ID:jmvrbanac,项目名称:falcon,代码行数:7,代码来源:test_default_router.py

示例2: test_user_regression_special_chars

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
开发者ID:jmvrbanac,项目名称:falcon,代码行数:11,代码来源:test_default_router.py

示例3: test_root_path

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
开发者ID:jmvrbanac,项目名称:falcon,代码行数:20,代码来源:test_default_router.py

示例4: test_user_regression_versioned_url

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
开发者ID:jmvrbanac,项目名称:falcon,代码行数:20,代码来源:test_default_router.py

示例5: test_user_regression_recipes

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
开发者ID:falconry,项目名称:falcon,代码行数:19,代码来源:test_default_router.py

示例6: before

    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))
开发者ID:TimSimpsonR,项目名称:falcon,代码行数:40,代码来源:test_default_router.py

示例7: TestComplexRouting

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)
#.........这里部分代码省略.........
开发者ID:enzzc,项目名称:falcon,代码行数:101,代码来源:test_default_router.py

示例8: before

 def before(self):
     self.router = DefaultRouter()
开发者ID:enzzc,项目名称:falcon,代码行数:2,代码来源:test_default_router.py

示例9: TestRegressionCases

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)
开发者ID:enzzc,项目名称:falcon,代码行数:40,代码来源:test_default_router.py

示例10: before

 def before(self):
     from falcon.routing import DefaultRouter
     self.router = DefaultRouter()
     setup_routes(self.router)
开发者ID:xiaclo,项目名称:falcon,代码行数:4,代码来源:test_default_router.py

示例11: TestStandaloneRouter

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)
#.........这里部分代码省略.........
开发者ID:xiaclo,项目名称:falcon,代码行数:101,代码来源:test_default_router.py

示例12: before

    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))
        self.router.add_route(
            '/users/{id:int}', {}, ResourceWithId(10))
        self.router.add_route(
            '/nearby/{lat:float}/{lon:float}', {}, ResourceWithId(10))
        self.router.add_route(
            'static/{path:path}', {}, ResourceWithId(10))
        self.router.add_route(
            'numbers/{numbers:re:[0-9]+}', {}, ResourceWithId(10))
        self.router.add_route(
            'letters/{numbers:re:[a-z]+}', {}, ResourceWithId(10))

        # 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))
开发者ID:lindseybrockman,项目名称:falcon,代码行数:75,代码来源:test_default_router.py

示例13: test_duplicate_field_names

def test_duplicate_field_names(uri_template):
    router = DefaultRouter()
    with pytest.raises(ValueError):
        router.add_route(uri_template, {}, ResourceWithId(1))
开发者ID:jmvrbanac,项目名称:falcon,代码行数:4,代码来源:test_default_router.py

示例14: router

def router():
    router = DefaultRouter()

    router.add_route(
        '/repos', {}, ResourceWithId(1))
    router.add_route(
        '/repos/{org}', {}, ResourceWithId(2))
    router.add_route(
        '/repos/{org}/{repo}', {}, ResourceWithId(3))
    router.add_route(
        '/repos/{org}/{repo}/commits', {}, ResourceWithId(4))
    router.add_route(
        u'/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}',
        {}, ResourceWithId(5))

    router.add_route(
        '/teams/{id}', {}, ResourceWithId(6))
    router.add_route(
        '/teams/{id}/members', {}, ResourceWithId(7))

    router.add_route(
        '/teams/default', {}, ResourceWithId(19))
    router.add_route(
        '/teams/default/members/thing', {}, ResourceWithId(19))

    router.add_route(
        '/user/memberships', {}, ResourceWithId(8))
    router.add_route(
        '/emojis', {}, ResourceWithId(9))
    router.add_route(
        '/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/full',
        {}, ResourceWithId(10))
    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.
    router.add_route(
        '/emojis/signs/0', {}, ResourceWithId(12))
    router.add_route(
        '/emojis/signs/{id}', {}, ResourceWithId(13))
    router.add_route(
        '/emojis/signs/42', {}, ResourceWithId(14))
    router.add_route(
        '/emojis/signs/42/small.jpg', {}, ResourceWithId(23))
    router.add_route(
        '/emojis/signs/78/small.png', {}, ResourceWithId(24))

    # Test some more special chars
    router.add_route(
        '/emojis/signs/78/small(png)', {}, ResourceWithId(25))
    router.add_route(
        '/emojis/signs/78/small_png', {}, ResourceWithId(26))
    router.add_route('/images/{id}.gif', {}, ResourceWithId(27))

    router.add_route(
        '/repos/{org}/{repo}/compare/{usr0}:{branch0}...{usr1}:{branch1}/part',
        {}, ResourceWithId(15))
    router.add_route(
        '/repos/{org}/{repo}/compare/{usr0}:{branch0}',
        {}, ResourceWithId(16))
    router.add_route(
        '/repos/{org}/{repo}/compare/{usr0}:{branch0}/full',
        {}, ResourceWithId(17))

    router.add_route(
        '/gists/{id}/{representation}', {}, ResourceWithId(21))
    router.add_route(
        '/gists/{id}/raw', {}, ResourceWithId(18))
    router.add_route(
        '/gists/first', {}, ResourceWithId(20))

    router.add_route('/item/{q}', {}, ResourceWithId(28))

    # ----------------------------------------------------------------
    # Routes with field converters
    # ----------------------------------------------------------------

    router.add_route(
        '/cvt/teams/{id:int(min=7)}', {}, ResourceWithId(29))
    router.add_route(
        '/cvt/teams/{id:int(min=7)}/members', {}, ResourceWithId(30))
    router.add_route(
        '/cvt/teams/default', {}, ResourceWithId(31))
    router.add_route(
        '/cvt/teams/default/members/{id:int}-{tenure:int}', {}, ResourceWithId(32))

    router.add_route(
        '/cvt/repos/{org}/{repo}/compare/{usr0}:{branch0:int}...{usr1}:{branch1:int}/part',
        {}, ResourceWithId(33))
    router.add_route(
        '/cvt/repos/{org}/{repo}/compare/{usr0}:{branch0:int}',
        {}, ResourceWithId(34))
    router.add_route(
        '/cvt/repos/{org}/{repo}/compare/{usr0}:{branch0:int}/full',
        {}, ResourceWithId(35))

    return router
开发者ID:jmvrbanac,项目名称:falcon,代码行数:99,代码来源:test_default_router.py

示例15: TestComplexRouting

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):
#.........这里部分代码省略.........
开发者ID:TimSimpsonR,项目名称:falcon,代码行数:101,代码来源:test_default_router.py


注:本文中的falcon.routing.DefaultRouter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。