示例#1
0
 def test_build_full_without_request(self):
     router = Router(None)
     router.add(Route(r'/hello', None, name='hello'))
     self.assertRaises(AttributeError, router.build, None, 'hello', (),
                       dict(_full=True))
     self.assertRaises(AttributeError, router.build, None, 'hello', (),
                       dict(_scheme='https'))
示例#2
0
    def test_set_matcher(self):
        req = Request.blank('/')
        def custom_matcher(router, request):
            self.assertEqual(request, req)

        router = Router(None)
        router.set_matcher(custom_matcher)
        router.match(req)
示例#3
0
    def test_router_build_error(self):
        router = Router()
        router.add(Route('/<year:\d{4}>', None, name='year-page'))

        url = router.build('year-page', Request.blank('/'), (), dict(year='2010'))
        self.assertEqual(url, '/2010')

        self.assertRaises(KeyError, router.build, 'i-dont-exist', Request.blank('/'), (), dict(year='2010'))
示例#4
0
    def test_router_build_error(self):
        router = Router(None)
        router.add(Route("/<year:\d{4}>", None, name="year-page"))

        url = router.build("year-page", Request.blank("/"), (), dict(year="2010"))
        self.assertEqual(url, "/2010")

        self.assertRaises(KeyError, router.build, "i-dont-exist", Request.blank("/"), (), dict(year="2010"))
示例#5
0
    def test_router_build_error(self):
        router = Router(None)
        router.add(Route('/<year:\d{4}>', None, name='year-page'))

        url = router.build(Request.blank('/'), 'year-page', (), dict(year='2010'))
        self.assertEqual(url, '/2010')

        self.assertRaises(KeyError, router.build, Request.blank('/'), 'i-dont-exist', (), dict(year='2010'))
示例#6
0
    def test_set_builder(self):
        req = Request.blank('/')
        def custom_builder(router, request, name, args, kwargs):
            self.assertEqual(request, req)
            return 'http://www.google.com'

        router = Router(None)
        router.set_builder(custom_builder)
        res = router.build(req, '', (), {})
        self.assertEqual(res, 'http://www.google.com')
示例#7
0
 def test_methods(self):
     route = Route(r'/', methods=['GET', 'POST'])
     router = Router([route])
     req = Request.blank('/')
     req.method = 'GET'
     self.assertTrue(router.match(req) is not None)
     req.method = 'POST'
     self.assertTrue(router.match(req) is not None)
     req.method = 'PUT'
     self.assertRaises(webapp2.exc.HTTPMethodNotAllowed, router.match, req)
示例#8
0
 def test_methods(self):
     route = Route(r'/', methods=['GET', 'POST'])
     router = Router([route])
     req = Request.blank('/')
     req.method = 'GET'
     self.assertTrue(router.match(req) is not None)
     req.method = 'POST'
     self.assertTrue(router.match(req) is not None)
     req.method = 'PUT'
     self.assertRaises(webapp2.exc.HTTPMethodNotAllowed, router.match, req)
 def test_methods(self):
     route = Route(r"/", methods=["GET", "POST"])
     router = Router([route])
     req = Request.blank("/")
     req.method = "GET"
     self.assertTrue(router.match(req) is not None)
     req.method = "POST"
     self.assertTrue(router.match(req) is not None)
     req.method = "PUT"
     self.assertRaises(webapp2.exc.HTTPMethodNotAllowed, router.match, req)
示例#10
0
    def test_set_builder(self):
        req = Request.blank('/')
        def custom_builder(router, request, name, args, kwargs):
            self.assertEqual(request, req)
            return 'http://www.google.com'

        router = Router(None)
        router.set_builder(custom_builder)
        res = router.build(req, '', (), {})
        self.assertEqual(res, 'http://www.google.com')
示例#11
0
    def test_set_builder(self):
        def custom_builder(router, request, name, args, kwargs):
            self.assertEqual(request, req)
            return "http://www.google.com"

        req = Request.blank("/")

        router = Router(None)
        router.set_builder(custom_builder)
        res = router.build(req, "", (), {})
        self.assertEqual(res, "http://www.google.com")
示例#12
0
 def test_build_full_without_request(self):
     router = Router(None)
     router.add(Route(r'/hello', None, name='hello'))
     self.assertRaises(
         AttributeError,
         router.build, None, 'hello', (), dict(_full=True)
     )
     self.assertRaises(
         AttributeError,
         router.build, None, 'hello', (), dict(_scheme='https')
     )
示例#13
0
    def test_router_build_error(self):
        router = Router(None)
        router.add(Route(r"/<year:\d{4}>", None, name="year-page"))

        url = router.build(Request.blank("/"), "year-page", (), dict(year="2010"))

        self.assertEqual(url, "/2010")

        self.assertRaises(
            KeyError,
            router.build,
            Request.blank("/"),
            "i-dont-exist",
            (),
            dict(year="2010"),
        )
    def test_with_variables_name_and_handler(self):
        router = Router(None, [
            PathPrefixRoute('/user/<username:\w+>', [
                HandlerPrefixRoute('apps.users.', [
                    NamePrefixRoute('user-', [
                        Route('/', 'UserOverviewHandler', 'overview'),
                        Route('/profile', 'UserProfileHandler', 'profile'),
                        Route('/projects', 'UserProjectsHandler', 'projects'),
                    ]),
                ]),
            ])
        ])

        path = '/user/calvin/'
        match = ('apps.users.UserOverviewHandler', (), {'username': '******'})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('user-overview', Request.blank('/'), match[1], match[2]), path)

        path = '/user/calvin/profile'
        match = ('apps.users.UserProfileHandler', (), {'username': '******'})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('user-profile', Request.blank('/'), match[1], match[2]), path)

        path = '/user/calvin/projects'
        match = ('apps.users.UserProjectsHandler', (), {'username': '******'})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('user-projects', Request.blank('/'), match[1], match[2]), path)
示例#15
0
文件: routes.py 项目: assekalala/gap
def include(prefix, routes):
    if isinstance(routes, StringTypes):
        routes = import_class('%s.routes' % routes)
    elif hasattr(routes, 'routes'):
        routes = routes.routes

    routes = [ Router.route_class(*route) if isinstance(route, tuple) else route for route in routes ]

    return PathPrefixRoute(prefix, routes)
示例#16
0
    def test_router_repr(self):
        router = Router(None)
        router.add(Route(r'/hello', None, name='hello', build_only=True))
        router.add(Route(r'/world', None))

        self.assertEqual(
            router.__repr__(),
            "<Router([<Route('/world', None, name=None, defaults={}, build_only=False)>, <Route('/hello', None, name='hello', defaults={}, build_only=True)>])>"
        )
    def test_simple(self):
        SUBDOMAIN_RE = '^([^.]+)\.app-id\.appspot\.com$'

        router = Router(None, [
            DomainRoute(SUBDOMAIN_RE, [
                Route('/foo', 'FooHandler', 'subdomain-thingie'),
            ])
        ])

        match = router.match(Request.blank('/foo'))
        self.assertEqual(match, None)

        match = router.match(Request.blank('http://my-subdomain.app-id.appspot.com/foo'))
        self.assertEqual(match, ('FooHandler', (), {'_host_match': ('my-subdomain',)}))

        match = router.match(Request.blank('http://another-subdomain.app-id.appspot.com/foo'))
        self.assertEqual(match, ('FooHandler', (), {'_host_match': ('another-subdomain',)}))

        url = router.build('subdomain-thingie', None, (), {'_netloc': 'another-subdomain.app-id.appspot.com'})
        self.assertEqual(url, 'http://another-subdomain.app-id.appspot.com/foo')
示例#18
0
def include(prefix, routes):
    if isinstance(routes, StringTypes):
        routes = import_class('%s.routes' % routes)
    elif hasattr(routes, 'routes'):
        routes = routes.routes

    routes = [
        Router.route_class(*route) if isinstance(route, tuple) else route
        for route in routes
    ]

    return PathPrefixRoute(prefix, routes)
示例#19
0
    def test_set_matcher(self):
        req = Request.blank('/')
        def custom_matcher(router, request):
            self.assertEqual(request, req)

        router = Router(None)
        router.set_matcher(custom_matcher)
        router.match(req)
示例#20
0
    def test_router_repr(self):
        router = Router(None)
        router.add(Route(r"/hello", None, name="hello", build_only=True))
        router.add(Route(r"/world", None))

        self.assertEqual(
            router.__repr__(),
            "<Router([<Route('/world', None, name=None, defaults={}, build_only=False)>, <Route('/hello', None, name='hello', defaults={}, build_only=True)>])>",
        )
示例#21
0
    def test_router_repr(self):
        router = Router(None)
        router.add(Route(r"/hello", None, name="hello", build_only=True))
        router.add(Route(r"/world", None))

        self.assertEqual(
            router.__repr__(), """<Router([<Route('/world', None, """
            """name=None, """
            """defaults={}, """
            """build_only=False)>, """
            """<Route('/hello', None, """
            """name='hello', """
            """defaults={}, """
            """build_only=True)>])>""")
    def test_with_variables_name_and_handler(self):
        SUBDOMAIN_RE = '^([^.]+)\.app-id\.appspot\.com$'

        router = Router(None, [
            DomainRoute(SUBDOMAIN_RE, [
                PathPrefixRoute('/user/<username:\w+>', [
                    HandlerPrefixRoute('apps.users.', [
                        NamePrefixRoute('user-', [
                            Route('/', 'UserOverviewHandler', 'overview'),
                            Route('/profile', 'UserProfileHandler', 'profile'),
                            Route('/projects', 'UserProjectsHandler', 'projects'),
                        ]),
                    ]),
                ])
            ]),
        ])

        path = 'http://my-subdomain.app-id.appspot.com/user/calvin/'
        match = ('apps.users.UserOverviewHandler', (), {'username': '******', '_host_match': ('my-subdomain',)})
        self.assertEqual(router.match(Request.blank(path)), match)
        match[2].pop('_host_match')
        match[2]['_netloc'] = 'my-subdomain.app-id.appspot.com'
        self.assertEqual(router.build('user-overview', Request.blank('/'), match[1], match[2]), path)

        path = 'http://my-subdomain.app-id.appspot.com/user/calvin/profile'
        match = ('apps.users.UserProfileHandler', (), {'username': '******', '_host_match': ('my-subdomain',)})
        self.assertEqual(router.match(Request.blank(path)), match)
        match[2].pop('_host_match')
        match[2]['_netloc'] = 'my-subdomain.app-id.appspot.com'
        self.assertEqual(router.build('user-profile', Request.blank('/'), match[1], match[2]), path)

        path = 'http://my-subdomain.app-id.appspot.com/user/calvin/projects'
        match = ('apps.users.UserProjectsHandler', (), {'username': '******', '_host_match': ('my-subdomain',)})
        self.assertEqual(router.match(Request.blank(path)), match)
        match[2].pop('_host_match')
        match[2]['_netloc'] = 'my-subdomain.app-id.appspot.com'
        self.assertEqual(router.build('user-projects', Request.blank('/'), match[1], match[2]), path)
    def test_no_variable(self):
        router = Router(None, [(r'/', 'my_handler')])

        matched_route, args, kwargs = router.match(Request.blank('/'))
        self.assertEqual(args, ())
        self.assertEqual(kwargs, {})
    def test_simple_variables(self):
        router = Router(None, [(r'/(\d{4})/(\d{2})', 'my_handler')])

        matched_route, args, kwargs = router.match(Request.blank('/2007/10'))
        self.assertEqual(args, ('2007', '10'))
        self.assertEqual(kwargs, {})
    def test_no_variable(self):
        router = Router(None, [(r'/', 'my_handler')])

        matched_route, args, kwargs = router.match(Request.blank('/'))
        self.assertEqual(args, ())
        self.assertEqual(kwargs, {})
    def test_simple_variables(self):
        router = Router(None, [(r'/(\d{4})/(\d{2})', 'my_handler')])

        matched_route, args, kwargs = router.match(Request.blank('/2007/10'))
        self.assertEqual(args, ('2007', '10'))
        self.assertEqual(kwargs, {})
示例#27
0
    def test_router_repr(self):
        router = Router()
        router.add(Route(r'/hello', None, name='hello', build_only=True))
        router.add(Route(r'/world', None))

        self.assertEqual(router.__repr__(), "Router([Route('/world', None, name=None, defaults={}, build_only=False), Route('/hello', None, name='hello', defaults={}, build_only=True)])")
示例#28
0
 def test_build_only_without_name(self):
     router = Router(None)
     self.assertRaises(ValueError, router.add,
                       Route(r'/<foo>', None, build_only=True))
 def test_route_redirect_to(self):
     router = Router(None, [ImprovedRoute('/foo', redirect_to='/bar')])
     handler, args, kwargs = router.match(Request.blank('/foo'))
     self.assertEqual(handler, RedirectHandler)
     self.assertEqual(args, ())
     self.assertEqual(kwargs, {'url': '/bar'})
    def test_simple(self):
        router = Router(None, [
            PathPrefixRoute('/a', [
                Route('/', 'a', 'name-a'),
                Route('/b', 'a/b', 'name-a/b'),
                Route('/c', 'a/c', 'name-a/c'),
                PathPrefixRoute('/d', [
                    Route('/', 'a/d', 'name-a/d'),
                    Route('/b', 'a/d/b', 'name-a/d/b'),
                    Route('/c', 'a/d/c', 'name-a/d/c'),
                ]),
            ])
        ])

        path = '/a/'
        match = ('a', (), {})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('name-' + match[0], Request.blank('/'), match[1], match[2]), path)

        path = '/a/b'
        match = ('a/b', (), {})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('name-' + match[0], Request.blank('/'), match[1], match[2]), path)

        path = '/a/c'
        match = ('a/c', (), {})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('name-' + match[0], Request.blank('/'), match[1], match[2]), path)

        path = '/a/d/'
        match = ('a/d', (), {})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('name-' + match[0], Request.blank('/'), match[1], match[2]), path)

        path = '/a/d/b'
        match = ('a/d/b', (), {})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('name-' + match[0], Request.blank('/'), match[1], match[2]), path)

        path = '/a/d/c'
        match = ('a/d/c', (), {})
        self.assertEqual(router.match(Request.blank(path)), match)
        self.assertEqual(router.build('name-' + match[0], Request.blank('/'), match[1], match[2]), path)
示例#31
0
 def test_build_full_without_request(self):
     router = Router(None)
     router.add(Route(r"/hello", None, name="hello"))
     self.assertRaises(AttributeError, router.build, "hello", None, (), dict(_full=True))
     self.assertRaises(AttributeError, router.build, "hello", None, (), dict(_scheme="https"))