def test_simple_variables(self): router = webapp2.Router([(r"/(\d{4})/(\d{2})", "my_handler")]) matched_route, args, kwargs = router.match(webapp2.Request.blank("/2007/10")) self.assertEqual(args, ("2007", "10")) self.assertEqual(kwargs, {})
def test_route_redirect_to(self): route = RedirectRoute("/foo", redirect_to="/bar") router = webapp2.Router([route]) route_match, args, kwargs = router.match(webapp2.Request.blank("/foo")) self.assertEqual(route_match, route) self.assertEqual(args, ()) self.assertEqual(kwargs, {"_uri": "/bar"})
def test_route_redirect_to(self): route = RedirectRoute('/foo', redirect_to='/bar') router = webapp2.Router([route]) route_match, args, kwargs = router.match(webapp2.Request.blank('/foo')) self.assertEqual(route_match, route) self.assertEqual(args, ()) self.assertEqual(kwargs, {'_uri': '/bar'})
def test_with_variables_name_and_handler(self): router = webapp2.Router([ PathPrefixRoute('/user/<username:\w+>', [ HandlerPrefixRoute('apps.users.', [ NamePrefixRoute('user-', [ webapp2.Route('/', 'UserOverviewHandler', 'overview'), webapp2.Route('/profile', 'UserProfileHandler', 'profile'), webapp2.Route('/projects', 'UserProjectsHandler', 'projects'), ]), ]), ]) ]) path = '/user/calvin/' match = ((), {'username': '******'}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'user-overview', match[0], match[1]), path) path = '/user/calvin/profile' match = ((), {'username': '******'}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'user-profile', match[0], match[1]), path) path = '/user/calvin/projects' match = ((), {'username': '******'}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'user-projects', match[0], match[1]), path)
def test_simple_variables(self): router = webapp2.Router([(r'/(\d{4})/(\d{2})', 'my_handler')]) matched_route, args, kwargs = router.match( webapp2.Request.blank('/2007/10')) self.assertEqual(args, ('2007', '10')) self.assertEqual(kwargs, {})
def test_simple(self): router = webapp2.Router( [ DomainRoute( "<subdomain>.<:.*>", [ webapp2.Route("/foo", "FooHandler", "subdomain-thingie"), ], ) ] ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank("/foo") ) match = router.match( webapp2.Request.blank("http://my-subdomain.app-id.appspot.com/foo") ) self.assertEqual(match[1:], ((), {"subdomain": "my-subdomain"})) match = router.match( webapp2.Request.blank("http://another-subdomain.app-id.appspot.com/foo") ) self.assertEqual(match[1:], ((), {"subdomain": "another-subdomain"})) url = router.build( webapp2.Request.blank("/"), "subdomain-thingie", (), {"_netloc": "another-subdomain.app-id.appspot.com"}, ) self.assertEqual(url, "http://another-subdomain.app-id.appspot.com/foo")
def test_with_variables_name_and_handler(self): router = webapp2.Router([ DomainRoute('<subdomain>.<:.*>', [ PathPrefixRoute(r'/user/<username:\w+>', [ HandlerPrefixRoute('apps.users.', [ NamePrefixRoute('user-', [ webapp2.Route( '/', 'UserOverviewHandler', 'overview' ), webapp2.Route( '/profile', 'UserProfileHandler', 'profile' ), webapp2.Route( '/projects', 'UserProjectsHandler', 'projects' ), ]), ]), ]) ]), ]) path = 'http://my-subdomain.app-id.appspot.com/user/calvin/' match = ((), {'username': '******', 'subdomain': 'my-subdomain'}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) match[1].pop('subdomain') match[1]['_netloc'] = 'my-subdomain.app-id.appspot.com' self.assertEqual( router.build(webapp2.Request.blank('/'), 'user-overview', match[0], match[1]), path ) path = 'http://my-subdomain.app-id.appspot.com/user/calvin/profile' match = ((), {'username': '******', 'subdomain': 'my-subdomain'}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) match[1].pop('subdomain') match[1]['_netloc'] = 'my-subdomain.app-id.appspot.com' self.assertEqual( router.build(webapp2.Request.blank('/'), 'user-profile', match[0], match[1]), path ) path = 'http://my-subdomain.app-id.appspot.com/user/calvin/projects' match = ((), {'username': '******', 'subdomain': 'my-subdomain'}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) match[1].pop('subdomain') match[1]['_netloc'] = 'my-subdomain.app-id.appspot.com' self.assertEqual( router.build(webapp2.Request.blank('/'), 'user-projects', match[0], match[1]), path )
def test_simple(self): router = webapp2.Router([ DomainRoute('<subdomain>.<:.*>', [ webapp2.Route('/foo', 'FooHandler', 'subdomain-thingie'), ]) ]) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank('/foo')) match = router.match( webapp2.Request.blank( 'http://my-subdomain.app-id.appspot.com/foo')) self.assertEqual(match[1:], ((), {'subdomain': 'my-subdomain'})) match = router.match( webapp2.Request.blank( 'http://another-subdomain.app-id.appspot.com/foo')) self.assertEqual(match[1:], ((), {'subdomain': 'another-subdomain'})) url = router.build( webapp2.Request.blank('/'), 'subdomain-thingie', (), {'_netloc': 'another-subdomain.app-id.appspot.com'} ) self.assertEqual( url, 'http://another-subdomain.app-id.appspot.com/foo' )
def testStrictHandlerMethodRouting(self): """Checks that handler functions properly limit applicable HTTP methods.""" router = webapp2.Router(app_routes._USER_ROUTES + app_routes._AJAX_ROUTES + app_routes._ADMIN_ROUTES + app_routes._ADMIN_AJAX_ROUTES) routes = router.match_routes + router.build_routes.values() failed_routes = [] while routes: route = routes.pop() if issubclass(route.__class__, webapp2_extras.routes.MultiRoute): routes += list(route.get_routes()) continue if issubclass(route.handler, webapp2.RedirectHandler): continue if route.handler_method and not route.methods: failed_routes.append('%s (%s)' % (route.template, route.handler.__name__)) if failed_routes: self.fail( 'Some handlers specify a handler_method but are missing a ' 'methods" attribute and may be vulnerable to XSRF via GET ' 'requests:\n * ' + '\n * '.join(failed_routes))
def _VerifyInheritance(self, routes_list, base_class): """Checks that the handlers of the given routes inherit from base_class.""" router = webapp2.Router(routes_list) routes = router.match_routes + router.build_routes.values() for route in routes: self.assertTrue(issubclass(route.handler, base_class), msg='%s in does not inherit from %s.' % (route.handler.__name__, base_class.__name__))
def test_simple(self): router = webapp2.Router([ PathPrefixRoute('/a', [ webapp2.Route('/', 'a', 'name-a'), webapp2.Route('/b', 'a/b', 'name-a/b'), webapp2.Route('/c', 'a/c', 'name-a/c'), PathPrefixRoute('/d', [ webapp2.Route('/', 'a/d', 'name-a/d'), webapp2.Route('/b', 'a/d/b', 'name-a/d/b'), webapp2.Route('/c', 'a/d/c', 'name-a/d/c'), ]), ]) ]) path = '/a/' match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'name-a', match[0], match[1]), path) path = '/a/b' match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'name-a/b', match[0], match[1]), path) path = '/a/c' match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'name-a/c', match[0], match[1]), path) path = '/a/d/' match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'name-a/d', match[0], match[1]), path) path = '/a/d/b' match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'name-a/d/b', match[0], match[1]), path) path = '/a/d/c' match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank('/'), 'name-a/d/c', match[0], match[1]), path)
def _VerifyInheritance(self, routes_list, base_class): """Checks that the handlers of the given routes inherit from base_class.""" router = webapp2.Router(routes_list) routes = router.match_routes + router.build_routes.values() inheritance_errors = '' for route in routes: if issubclass(route.__class__, webapp2_extras.routes.MultiRoute): self._VerifyInheritance(list(route.get_routes()), base_class) continue if issubclass(route.handler, webapp2.RedirectHandler): continue if not issubclass(route.handler, base_class): inheritance_errors += '* %s does not inherit from %s.\n' % ( route.handler.__name__, base_class.__name__) return inheritance_errors
def test_simple(self): SUBDOMAIN_RE = '^(?P<subdomain>[^.]+)\.app-id\.appspot\.com$' router = webapp2.Router(None, [ DomainRoute(SUBDOMAIN_RE, [ webapp2.Route('/foo', 'FooHandler', 'subdomain-thingie'), ]) ]) match = router.match(webapp2.Request.blank('/foo')) self.assertEqual(match, None) match = router.match(webapp2.Request.blank('http://my-subdomain.app-id.appspot.com/foo')) self.assertEqual(match[1:], ((), {'subdomain': 'my-subdomain'})) match = router.match(webapp2.Request.blank('http://another-subdomain.app-id.appspot.com/foo')) self.assertEqual(match[1:], ((), {'subdomain': 'another-subdomain'})) url = router.build(webapp2.Request.blank('/'), 'subdomain-thingie', (), {'_netloc': 'another-subdomain.app-id.appspot.com'}) self.assertEqual(url, 'http://another-subdomain.app-id.appspot.com/foo')
def test_simple(self): router = webapp2.Router( [ PathPrefixRoute( "/a", [ webapp2.Route("/", "a", "name-a"), webapp2.Route("/b", "a/b", "name-a/b"), webapp2.Route("/c", "a/c", "name-a/c"), PathPrefixRoute( "/d", [ webapp2.Route("/", "a/d", "name-a/d"), webapp2.Route("/b", "a/d/b", "name-a/d/b"), webapp2.Route("/c", "a/d/c", "name-a/d/c"), ], ), ], ) ] ) path = "/a/" match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank("/"), "name-a", match[0], match[1]), path ) path = "/a/b" match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank("/"), "name-a/b", match[0], match[1]), path, ) path = "/a/c" match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank("/"), "name-a/c", match[0], match[1]), path, ) path = "/a/d/" match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank("/"), "name-a/d", match[0], match[1]), path, ) path = "/a/d/b" match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank("/"), "name-a/d/b", match[0], match[1]), path, ) path = "/a/d/c" match = ((), {}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build(webapp2.Request.blank("/"), "name-a/d/c", match[0], match[1]), path, )
def __init__(self, routes): webapp2.BaseRoute.__init__(self, None) self.router = webapp2.Router(routes)
def test_with_variables_name_and_handler(self): router = webapp2.Router( [ PathPrefixRoute( r"/user/<username:\w+>", [ HandlerPrefixRoute( "apps.users.", [ NamePrefixRoute( "user-", [ webapp2.Route( "/", "UserOverviewHandler", "overview" ), webapp2.Route( "/profile", "UserProfileHandler", "profile" ), webapp2.Route( "/projects", "UserProjectsHandler", "projects", ), ], ), ], ), ], ) ] ) path = "/user/calvin/" match = ((), {"username": "******"}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build( webapp2.Request.blank("/"), "user-overview", match[0], match[1] ), path, ) path = "/user/calvin/profile" match = ((), {"username": "******"}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build( webapp2.Request.blank("/"), "user-profile", match[0], match[1] ), path, ) path = "/user/calvin/projects" match = ((), {"username": "******"}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) self.assertEqual( router.build( webapp2.Request.blank("/"), "user-projects", match[0], match[1] ), path, )
def test_no_variable(self): router = webapp2.Router([(r'/', 'my_handler')]) matched_route, args, kwargs = router.match(webapp2.Request.blank('/')) self.assertEqual(args, ()) self.assertEqual(kwargs, {})
def test_with_variables_name_and_handler(self): router = webapp2.Router( [ DomainRoute( "<subdomain>.<:.*>", [ PathPrefixRoute( r"/user/<username:\w+>", [ HandlerPrefixRoute( "apps.users.", [ NamePrefixRoute( "user-", [ webapp2.Route( "/", "UserOverviewHandler", "overview", ), webapp2.Route( "/profile", "UserProfileHandler", "profile", ), webapp2.Route( "/projects", "UserProjectsHandler", "projects", ), ], ), ], ), ], ) ], ), ] ) path = "http://my-subdomain.app-id.appspot.com/user/calvin/" match = ((), {"username": "******", "subdomain": "my-subdomain"}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) match[1].pop("subdomain") match[1]["_netloc"] = "my-subdomain.app-id.appspot.com" self.assertEqual( router.build( webapp2.Request.blank("/"), "user-overview", match[0], match[1] ), path, ) path = "http://my-subdomain.app-id.appspot.com/user/calvin/profile" match = ((), {"username": "******", "subdomain": "my-subdomain"}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) match[1].pop("subdomain") match[1]["_netloc"] = "my-subdomain.app-id.appspot.com" self.assertEqual( router.build( webapp2.Request.blank("/"), "user-profile", match[0], match[1] ), path, ) path = "http://my-subdomain.app-id.appspot.com/user/calvin/projects" match = ((), {"username": "******", "subdomain": "my-subdomain"}) self.assertEqual(router.match(webapp2.Request.blank(path))[1:], match) match[1].pop("subdomain") match[1]["_netloc"] = "my-subdomain.app-id.appspot.com" self.assertEqual( router.build( webapp2.Request.blank("/"), "user-projects", match[0], match[1] ), path, )
def test_guide_examples(self): router = webapp2.Router([ DomainRoute(r'www.mydomain.com', [ webapp2.Route('/path1', 'Path1', 'path1'), ]), DomainRoute(r'<subdomain:(?!www\.)[^.]+>.mydomain.com', [ webapp2.Route('/path2', 'Path2', 'path2'), ]), DomainRoute(r'<:(app-id\.appspot\.com|www\.mydomain\.com)>', [ webapp2.Route('/path3', 'Path3', 'path3'), ]), DomainRoute( r'<subdomain:(?!www)[^.]' r'+>.<:(app-id\.appspot\.com|mydomain\.com)>', [ webapp2.Route('/path4', 'Path4', 'path4'), ]), ]) uri1a = 'http://www.mydomain.com/path1' uri1b = 'http://sub.mydomain.com/path1' uri1c = 'http://www.mydomain.com/invalid-path' uri2a = 'http://sub.mydomain.com/path2' uri2b = 'http://www.mydomain.com/path2' uri2c = 'http://sub.mydomain.com/invalid-path' uri2d = 'http://www.mydomain.com/invalid-path' uri3a = 'http://app-id.appspot.com/path3' uri3b = 'http://www.mydomain.com/path3' uri3c = 'http://sub.app-id.appspot.com/path3' uri3d = 'http://sub.mydomain.com/path3' uri3e = 'http://app-id.appspot.com/invalid-path' uri3f = 'http://www.mydomain.com/invalid-path' uri4a = 'http://sub.app-id.appspot.com/path4' uri4b = 'http://sub.mydomain.com/path4' uri4c = 'http://app-id.appspot.com/path4' uri4d = 'http://www.app-id.appspot.com/path4' uri4e = 'http://www.mydomain.com/path4' uri4f = 'http://sub.app-id.appspot.com/invalid-path' uri4g = 'http://sub.mydomain.com/invalid-path' self.assertEqual( router.match(webapp2.Request.blank(uri1a))[1:], ((), {})) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri1b)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri1c)) self.assertEqual( router.match(webapp2.Request.blank(uri2a))[1:], ((), { 'subdomain': 'sub' })) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri2b)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri2c)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri2d)) self.assertEqual( router.match(webapp2.Request.blank(uri3a))[1:], ((), {})) self.assertEqual( router.match(webapp2.Request.blank(uri3b))[1:], ((), {})) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3c)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3d)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3e)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3f)) self.assertEqual( router.match(webapp2.Request.blank(uri4a))[1:], ((), { 'subdomain': 'sub' })) self.assertEqual( router.match(webapp2.Request.blank(uri4b))[1:], ((), { 'subdomain': 'sub' })) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4c)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4d)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4e)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4f)) self.assertRaises(webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4g))
def test_guide_examples(self): router = webapp2.Router( [ DomainRoute( r"www.mydomain.com", [ webapp2.Route("/path1", "Path1", "path1"), ], ), DomainRoute( r"<subdomain:(?!www\.)[^.]+>.mydomain.com", [ webapp2.Route("/path2", "Path2", "path2"), ], ), DomainRoute( r"<:(app-id\.appspot\.com|www\.mydomain\.com)>", [ webapp2.Route("/path3", "Path3", "path3"), ], ), DomainRoute( r"<subdomain:(?!www)[^.]" r"+>.<:(app-id\.appspot\.com|mydomain\.com)>", [ webapp2.Route("/path4", "Path4", "path4"), ], ), ] ) uri1a = "http://www.mydomain.com/path1" uri1b = "http://sub.mydomain.com/path1" uri1c = "http://www.mydomain.com/invalid-path" uri2a = "http://sub.mydomain.com/path2" uri2b = "http://www.mydomain.com/path2" uri2c = "http://sub.mydomain.com/invalid-path" uri2d = "http://www.mydomain.com/invalid-path" uri3a = "http://app-id.appspot.com/path3" uri3b = "http://www.mydomain.com/path3" uri3c = "http://sub.app-id.appspot.com/path3" uri3d = "http://sub.mydomain.com/path3" uri3e = "http://app-id.appspot.com/invalid-path" uri3f = "http://www.mydomain.com/invalid-path" uri4a = "http://sub.app-id.appspot.com/path4" uri4b = "http://sub.mydomain.com/path4" uri4c = "http://app-id.appspot.com/path4" uri4d = "http://www.app-id.appspot.com/path4" uri4e = "http://www.mydomain.com/path4" uri4f = "http://sub.app-id.appspot.com/invalid-path" uri4g = "http://sub.mydomain.com/invalid-path" self.assertEqual(router.match(webapp2.Request.blank(uri1a))[1:], ((), {})) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri1b) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri1c) ) self.assertEqual( router.match(webapp2.Request.blank(uri2a))[1:], ((), {"subdomain": "sub"}) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri2b) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri2c) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri2d) ) self.assertEqual(router.match(webapp2.Request.blank(uri3a))[1:], ((), {})) self.assertEqual(router.match(webapp2.Request.blank(uri3b))[1:], ((), {})) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3c) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3d) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3e) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri3f) ) self.assertEqual( router.match(webapp2.Request.blank(uri4a))[1:], ((), {"subdomain": "sub"}) ) self.assertEqual( router.match(webapp2.Request.blank(uri4b))[1:], ((), {"subdomain": "sub"}) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4c) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4d) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4e) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4f) ) self.assertRaises( webapp2.exc.HTTPNotFound, router.match, webapp2.Request.blank(uri4g) )