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 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_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_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_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_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_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)