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'))
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)
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'))
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"))
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'))
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')
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)
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")
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') )
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)
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)
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')
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_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_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)])")
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)
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"))