def test_build_missing_argument(self): route = Route(r'/<:\d{4}>', None) self.assertRaises(KeyError, route.build, Request.blank('/'), (), {}) route = Route(r'/<:\d{4}>/<:\d{2}>', None) self.assertRaises(KeyError, route.build, Request.blank('/'), (2010, ), {})
def test_build_extra_keyword(self): route = Route(r'/<year:\d{4}>', None) url = route.build(Request.blank('/'), (), dict(year='2010', foo='bar')) self.assertEqual(url, '/2010?foo=bar') # Arguments are sorted. url = route.build(Request.blank('/'), (), dict(year='2010', foo='bar', baz='ding')) self.assertEqual(url, '/2010?baz=ding&foo=bar')
def test_expr_variable(self): route = Route(r'/<year:\d{4}>', None) self.assertEqual(route.match(Request.blank('/bar')), None) self.assertEqual(route.match(Request.blank('/2010')), (None, (), {'year': '2010'})) self.assertEqual(route.match(Request.blank('/1900')), (None, (), {'year': '1900'})) url = route.build(Request.blank('/'), (), dict(year='2010')) self.assertEqual(url, '/2010')
def test_build_extra_keyword(self): route = Route(r"/<year:\d{4}>", None) url = route.build(Request.blank("/"), (), dict(year="2010", foo="bar")) self.assertEqual(url, "/2010?foo=bar") # Arguments are sorted. url = route.build(Request.blank("/"), (), dict(year="2010", foo="bar", baz="ding")) self.assertEqual(url, "/2010?baz=ding&foo=bar")
def test_expr_variable(self): route = Route(r"/<year:\d{4}>", None) self.assertEqual(route.match(Request.blank("/bar")), None) self.assertEqual(route.match(Request.blank("/2010")), (None, (), {"year": "2010"})) self.assertEqual(route.match(Request.blank("/1900")), (None, (), {"year": "1900"})) url = route.build(Request.blank("/"), (), dict(year="2010")) self.assertEqual(url, "/2010")
def setUp(self): super(ValidatorTest, self).setUp() self.app = create_simple_test_app([ Route( '/test', handler=ValidatorTestHandler, handler_method='test_function', methods=['POST'], ), Route( '/test/extra_validators', handler=ValidatorTestHandler, handler_method='test_function_extra_validators', methods=['POST'], ), Route( '/test/bool_validator', handler=ValidatorTestHandler, handler_method='test_bool_validator', methods=['POST'], ), Route( '/test/decimal_validator', handler=ValidatorTestHandler, handler_method='test_decimal_validator', methods=['POST'], ) ])
def test_route_repr(self): self.assertEqual( Route(r"/<foo>", None).__repr__(), "<Route('/<foo>', None, name=None, defaults={}, build_only=False)>", ) self.assertEqual( Route( r"/<foo>", None, name="bar", defaults={"baz": "ding"}, build_only=True ).__repr__(), """<Route('/<foo>', None, """ """name='bar', """ """defaults={'baz': 'ding'}, """ """build_only=True)>""", ) self.assertEqual( str(Route(r"/<foo>", None)), "<Route('/<foo>', None, name=None, defaults={}, build_only=False)>", ) self.assertEqual( str( Route( r"/<foo>", None, name="bar", defaults={"baz": "ding"}, build_only=True, ) ), """<Route('/<foo>', None, """ """name='bar', """ """defaults={'baz': 'ding'}, """ """build_only=True)>""", )
def ResourceRoutes(route_prefix, Model, **kwargs): handler = create_resource_handler(Model, **kwargs) return PathPrefixRoute('/{}'.format(route_prefix), [ Route('/', handler=handler, handler_method='index', methods=['GET']), Route( '/<uid:[^/]+>', handler=handler, methods=['GET', 'PUT', 'DELETE']), ])
def test_simple_variable(self): route = Route(r'/<foo>', None) self.assertEqual(route.match(Request.blank('/bar')), (route, (), { 'foo': 'bar' })) url = route.build(Request.blank('/'), (), dict(foo='baz')) self.assertEqual(url, '/baz')
def test_expr_variable(self): route = Route(r'/<year:\d{4}>', None) self.assertEqual(route.match(Request.blank('/bar')), None) self.assertEqual(route.match(Request.blank('/2010')), (route, (), {'year': '2010'})) self.assertEqual(route.match(Request.blank('/1900')), (route, (), {'year': '1900'})) url = route.build(Request.blank('/'), (), dict(year='2010')) self.assertEqual(url, '/2010')
def test_route_repr(self): self.assertEqual( Route(r'/<foo>', None).__repr__(), "<Route('/<foo>', None, name=None, defaults={}, build_only=False)>" ) self.assertEqual( Route(r'/<foo>', None, name='bar', defaults={ 'baz': 'ding' }, build_only=True).__repr__(), "<Route('/<foo>', None, name='bar', defaults={'baz': 'ding'}, build_only=True)>" ) self.assertEqual( str(Route(r'/<foo>', None)), "<Route('/<foo>', None, name=None, defaults={}, build_only=False)>" ) self.assertEqual( str( Route(r'/<foo>', None, name='bar', defaults={'baz': 'ding'}, build_only=True)), "<Route('/<foo>', None, name='bar', defaults={'baz': 'ding'}, build_only=True)>" )
def test_simple_variable(self): route = Route(r"/<foo>", None) self.assertEqual( route.match(Request.blank("/bar")), (route, (), {"foo": "bar"}) ) url = route.build(Request.blank("/"), (), dict(foo="baz")) self.assertEqual(url, "/baz")
def test_build_default_keyword(self): route = Route(r"/<year:\d{4}>/<month:\d{2}>", None, defaults={"month": 10}) url = route.build(Request.blank("/"), (), dict(year="2010")) self.assertEqual(url, "/2010/10") route = Route(r"/<year:\d{4}>/<month:\d{2}>", None, defaults={"year": 1900}) url = route.build(Request.blank("/"), (), dict(month="07")) self.assertEqual(url, "/1900/07")
def test_simple_variable(self): route = Route(r'/<foo>', None) self.assertEqual( route.match(Request.blank('/bar')), (route, (), {'foo': 'bar'}) ) url = route.build(Request.blank('/'), (), dict(foo='baz')) self.assertEqual(url, '/baz')
def test_build_with_unnamed_variable(self): route = Route(r'/<:\d{4}>/<month:\d{2}>', None) url = route.build(Request.blank('/'), (2010, ), dict(month=10)) self.assertEqual(url, '/2010/10') url = route.build(Request.blank('/'), ('1999', ), dict(month='07')) self.assertEqual(url, '/1999/07')
def test_build_extra_positional_keyword(self): route = Route(r'/<year:\d{4}>/<:\d{2}>', None) url = route.build(Request.blank('/'), ('08', 'i-should-be-ignored', 'me-too'), dict(year='2010', foo='bar')) self.assertEqual(url, '/2010/08?foo=bar') url = route.build(Request.blank('/'), ('08', 'i-should-be-ignored', 'me-too'), dict(year='2010', foo='bar', baz='ding')) self.assertEqual(url, '/2010/08?baz=ding&foo=bar')
def test_build_with_unnamed_variable(self): route = Route(r'/<:\d{4}>/<month:\d{2}>', None) url = route.build(Request.blank('/'), (2010,), dict(month=10)) self.assertEqual(url, '/2010/10') url = route.build(Request.blank('/'), ('1999',), dict(month='07')) self.assertEqual(url, '/1999/07')
def get_routes(): return [ PathPrefixRoute('/patient', [ Route('/bookings/<patient_key>', patient.ListPatientBookings), Route('/address/<patient_key>', address_handler.PatientEditAddressHandler), ]), ]
def test_build_with_unnamed_variable(self): route = Route(r"/<:\d{4}>/<month:\d{2}>", None) url = route.build(Request.blank("/"), (2010,), dict(month=10)) self.assertEqual(url, "/2010/10") url = route.build(Request.blank("/"), ("1999",), dict(month="07")) self.assertEqual(url, "/1999/07")
def get_rules(): ''' Return URL routing rules. ''' return [ routes.HandlerPrefixRoute('coolapp.handlers.', [ Route('/env', name='landing', handler='hello.Environment'), Route('/', name='landing', handler='hello.Landing') ]) ]
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_build_default_keyword(self): route = Route(r'/<year:\d{4}>/<month:\d{2}>', None, defaults={'month': 10}) url = route.build(Request.blank('/'), (), dict(year='2010')) self.assertEqual(url, '/2010/10') route = Route(r'/<year:\d{4}>/<month:\d{2}>', None, defaults={'year': 1900}) url = route.build(Request.blank('/'), (), dict(month='07')) self.assertEqual(url, '/1900/07')
def test_build_extra_positional_keyword(self): route = Route(r"/<year:\d{4}>/<:\d{2}>", None) url = route.build(Request.blank("/"), ("08", "i-should-be-ignored", "me-too"), dict(year="2010", foo="bar")) self.assertEqual(url, "/2010/08?foo=bar") url = route.build( Request.blank("/"), ("08", "i-should-be-ignored", "me-too"), dict(year="2010", foo="bar", baz="ding") ) self.assertEqual(url, "/2010/08?baz=ding&foo=bar")
def test_build_extra_keyword(self): route = Route(r"/<year:\d{4}>", None) r = Request.blank("/") url = route.build(r, (), dict(year="2010", foo="bar")) self.assertEqual(url, "/2010?foo=bar") # Arguments are sorted. url = route.build( Request.blank("/"), (), dict(year="2010", foo="bar", baz="ding") ) self.assertEqual(url, "/2010?baz=ding&foo=bar")
def GetRoutes(self, args): # pylint: disable=unused-argument return [ Route('/perf_insights/tests', TestListHandler), Route('/perf_insights_examples/run_map_function', RunMapFunctionHandler, defaults={ '_pi_data_dir': os.path.abspath(os.path.expanduser(args.pi_data_dir)) }) ]
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_reverse_template(self): route = Route("/foo", None) route.regex # Access route.regex just to set the lazy properties. self.assertEqual(route.reverse_template, "/foo") route = Route("/foo/<bar>", None) route.regex # Access route.regex just to set the lazy properties. self.assertEqual(route.reverse_template, "/foo/%(bar)s") route = Route(r"/foo/<bar>/<baz:\d>", None) route.regex # Access route.regex just to set the lazy properties. self.assertEqual(route.reverse_template, "/foo/%(bar)s/%(baz)s")
def test_reverse_template(self): route = Route('/foo', None) # Access route.regex just to set the lazy properties. regex = route.regex self.assertEqual(route.reverse_template, '/foo') route = Route('/foo/<bar>', None) # Access route.regex just to set the lazy properties. regex = route.regex self.assertEqual(route.reverse_template, '/foo/%(bar)s') route = Route('/foo/<bar>/<baz:\d>', None) # Access route.regex just to set the lazy properties. regex = route.regex self.assertEqual(route.reverse_template, '/foo/%(bar)s/%(baz)s')
def test_unnamed_variable(self): route = Route(r'/<:\d{4}>', None) self.assertEqual(route.match(Request.blank('/2010')), (route, ('2010',), {})) self.assertEqual(route.match(Request.blank('/aaaa')), None) route = Route(r'/<:\d{2}>.<:\d{2}>', None) self.assertEqual(route.match(Request.blank('/98.99')), (route, ('98', '99'), {})) self.assertEqual(route.match(Request.blank('/aa.aa')), None) route = Route(r'/<:\d{2}>.<:\d{2}>/<foo>', None) self.assertEqual(route.match(Request.blank('/98.99/test')), (route, ('98', '99'), {'foo': 'test'})) self.assertEqual(route.match(Request.blank('/aa.aa/test')), None)
def build_routes_for_handler(handlercls): """ Returns list of routes for a particular handler, to enable methods to be routed, add the ferris.core.handler.auto_route decorator, or simply set the 'route' attr of the function to True. def some_method(self, arg1, arg2, arg3) becomes /handler/some_method/<arg1>/<arg2>/<arg3> """ routes_list = [] methods = inspect.getmembers(handlercls, predicate=inspect.ismethod) methods = [x[1] for x in methods if hasattr(x[1], 'route')] for method in methods: parts = canonical_parts_from_method(method) route_path = path_from_canonical_parts(**parts) route_name = name_from_canonical_parts(**parts) kwargs = dict(template=route_path, handler=handlercls, name=route_name, handler_method=method.__name__) method_args = method.route if isinstance(method_args, tuple): kwargs.update(method_args[1]) routes_list.append(Route(**kwargs)) return routes_list
def setUp(self): super(CSRFUtilitiesTest, self).setUp() self.app = create_simple_test_app([ Route( '/test', handler=SimpleCSRFTestHandler, handler_method='test_get', methods=['GET'], ), Route( '/test', handler=SimpleCSRFTestHandler, handler_method='test_post', methods=['POST'], ) ])
def test_build_extra_positional_keyword(self): route = Route(r"/<year:\d{4}>/<:\d{2}>", None) url = route.build( Request.blank("/"), ("08", "i-should-be-ignored", "me-too"), dict(year="2010", foo="bar"), ) self.assertEqual(url, "/2010/08?foo=bar") url = route.build( Request.blank("/"), ("08", "i-should-be-ignored", "me-too"), dict(year="2010", foo="bar", baz="ding"), ) self.assertEqual(url, "/2010/08?baz=ding&foo=bar")
def test_invalid_template(self): # To break it: # <>foo:><bar<:baz> route = Route('/<foo/<:bar', None) # Access route.regex just to set the lazy properties. regex = route.regex self.assertEqual(route.reverse_template, '/<foo/<:bar')
def setUp(self): super(CSRFProtectedHandlerTest, self).setUp() self.app = create_simple_test_app([ Route( '/test', handler=CSRFProtectedTestHandler, handler_method='test_get', methods=['GET'], ), Route( '/test', handler=CSRFProtectedTestHandler, handler_method='test_post', methods=['POST'], ) ])
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_repetition_operator(self): route = Route(r"/<:\d>", None) self.assertEqual(route.match(Request.blank("/1")), (route, ("1",), {})) self.assertEqual(route.match(Request.blank("/2")), (route, ("2",), {})) route = Route(r"/<:\d{2,3}>", None) self.assertEqual(route.match(Request.blank("/11")), (route, ("11",), {})) self.assertEqual(route.match(Request.blank("/111")), (route, ("111",), {})) self.assertEqual(route.match(Request.blank("/1111")), None)
def test_repetition_operator(self): route = Route(r'/<:\d>', None) self.assertEqual(route.match(Request.blank('/1')), (route, ('1',), {})) self.assertEqual(route.match(Request.blank('/2')), (route, ('2',), {})) route = Route(r'/<:\d{2,3}>', None) self.assertEqual(route.match(Request.blank('/11')), (route, ('11',), {})) self.assertEqual(route.match(Request.blank('/111')), (route, ('111',), {})) self.assertEqual(route.match(Request.blank('/1111')), None)
def _InitFromArgs(self, args): default_tests = dict( (pd.GetName(), pd.GetRunUnitTestsUrl()) for pd in self.pds) routes = [ Route('/tests.html', TestOverviewHandler, defaults={'pds': default_tests}), Route('', RedirectHandler, defaults={'_uri': '/tests.html'}), Route('/', RedirectHandler, defaults={'_uri': '/tests.html'}), ] for pd in self.pds: routes += pd.GetRoutes(args) routes += [ Route('/%s/notify_test_result' % pd.GetName(), TestResultHandler), Route('/%s/notify_tests_completed' % pd.GetName(), TestsCompletedHandler), Route('/%s/notify_test_error' % pd.GetName(), TestsErrorHandler) ] for pd in self.pds: # Test data system. for mapped_path, source_path in pd.GetTestDataPaths(args): self._all_mapped_test_data_paths.append( (mapped_path, source_path)) routes.append( Route('%s__file_list__' % mapped_path, DirectoryListingHandler, defaults={ '_source_path': source_path, '_mapped_path': mapped_path })) routes.append( Route('%s<rest_of_path:.+>' % mapped_path, SimpleDirectoryHandler, defaults={'_top_path': source_path})) # This must go last, because its catch-all. # # Its funky that we have to add in the root path. The long term fix is to # stop with the crazy multi-source-pathing thing. for pd in self.pds: self._all_source_paths += pd.GetSourcePaths(args) routes.append( Route('/<:.+>', SourcePathsHandler, defaults={'_source_paths': self._all_source_paths})) for route in routes: self.router.add(route)
def test_positions(self): template = '/<:\d+>' * 98 args = tuple(str(i) for i in range(98)) url_res = '/' + '/'.join(args) route = Route(template, None) self.assertEqual(route.match(Request.blank(url_res)), (None, args, {})) url = route.build(Request.blank('/'), args, {}) self.assertEqual(url_res, url) args = [str(i) for i in range(1000)] random.shuffle(args) args = tuple(args[:98]) url_res = '/' + '/'.join(args) self.assertEqual(route.match(Request.blank(url_res)), (None, args, {})) url = route.build(Request.blank('/'), args, {}) self.assertEqual(url_res, url)
def test_repetition_operator(self): route = Route(r"/<:\d>", None) self.assertEqual(route.match(Request.blank("/1")), (None, ("1",), {})) self.assertEqual(route.match(Request.blank("/2")), (None, ("2",), {})) route = Route(r"/<:\d{2,3}>", None) self.assertEqual(route.match(Request.blank("/11")), (None, ("11",), {})) self.assertEqual(route.match(Request.blank("/111")), (None, ("111",), {})) self.assertEqual(route.match(Request.blank("/1111")), None)
def test_repetition_operator(self): route = Route(r'/<:\d>', None) self.assertEqual(route.match(Request.blank('/1')), (None, ('1',), {})) self.assertEqual(route.match(Request.blank('/2')), (None, ('2',), {})) route = Route(r'/<:\d{2,3}>', None) self.assertEqual(route.match(Request.blank('/11')), (None, ('11',), {})) self.assertEqual(route.match(Request.blank('/111')), (None, ('111',), {})) self.assertEqual(route.match(Request.blank('/1111')), None)
def test_unnamed_variable(self): route = Route(r'/<:\d{4}>', None) self.assertEqual( route.match(Request.blank('/2010')), (route, ('2010',), {}) ) self.assertEqual(route.match(Request.blank('/aaaa')), None) route = Route(r'/<:\d{2}>.<:\d{2}>', None) self.assertEqual( route.match(Request.blank('/98.99')), (route, ('98', '99'), {}) ) self.assertEqual(route.match(Request.blank('/aa.aa')), None) route = Route(r'/<:\d{2}>.<:\d{2}>/<foo>', None) self.assertEqual( route.match(Request.blank('/98.99/test')), (route, ('98', '99'), {'foo': 'test'}) ) self.assertEqual(route.match(Request.blank('/aa.aa/test')), None)
def test_no_variable(self): route = Route(r"/hello", None) handler, args, kwargs = route.match(Request.blank("/hello")) self.assertEqual(kwargs, {}) url = route.build(Request.blank("/"), (), {}) self.assertEqual(url, "/hello") route = Route(r"/hello/world/", None) handler, args, kwargs = route.match(Request.blank("/hello/world/")) self.assertEqual(kwargs, {}) url = route.build(Request.blank("/"), (), {}) self.assertEqual(url, "/hello/world/")
def test_schemes(self): route = Route(r'/', schemes=['http']) req = Request.blank('http://mydomain.com/') self.assertTrue(route.match(req) is not None) req = Request.blank('https://mydomain.com/') self.assertTrue(route.match(req) is None) route = Route(r'/', schemes=['https']) req = Request.blank('https://mydomain.com/') self.assertTrue(route.match(req) is not None) req = Request.blank('http://mydomain.com/') self.assertTrue(route.match(req) is None)
def test_reverse_template(self): route = Route("/foo", None) template = route.reverse_template or route._reverse_template() self.assertEqual(template, "/foo") route = Route("/foo/<bar>", None) template = route.reverse_template or route._reverse_template() self.assertEqual(template, "/foo/%(bar)s") route = Route("/foo/<bar>/<baz:\d>", None) template = route.reverse_template or route._reverse_template() self.assertEqual(template, "/foo/%(bar)s/%(baz)s")
def test_unnamed_variable(self): route = Route(r"/<:\d{4}>", None) self.assertEqual(route.match(Request.blank("/2010")), (None, ("2010",), {})) self.assertEqual(route.match(Request.blank("/aaaa")), None) route = Route(r"/<:\d{2}>.<:\d{2}>", None) self.assertEqual(route.match(Request.blank("/98.99")), (None, ("98", "99"), {})) self.assertEqual(route.match(Request.blank("/aa.aa")), None) route = Route(r"/<:\d{2}>.<:\d{2}>/<foo>", None) self.assertEqual(route.match(Request.blank("/98.99/test")), (None, ("98", "99"), {"foo": "test"})) self.assertEqual(route.match(Request.blank("/aa.aa/test")), None)
def test_no_variable(self): route = Route(r'/hello', None) handler, args, kwargs = route.match(Request.blank('/hello')) self.assertEqual(kwargs, {}) url = route.build(Request.blank('/'), (), {}) self.assertEqual(url, '/hello') route = Route(r'/hello/world/', None) handler, args, kwargs = route.match(Request.blank('/hello/world/')) self.assertEqual(kwargs, {}) url = route.build(Request.blank('/'), (), {}) self.assertEqual(url, '/hello/world/')
def test_build_int_keyword(self): route = Route(r"/<year:\d{4}>", None) url = route.build(Request.blank("/"), (), dict(year=2010)) self.assertEqual(url, "/2010")
def test_expr_variable2(self): route = Route(r"/<year:\d{4}>/foo/", None) url = route.build(Request.blank("/"), (), dict(year="2010")) self.assertEqual(url, "/2010/foo/")
def test_build_int_variable(self): route = Route(r"/<:\d{4}>", None) url = route.build(Request.blank("/"), (2010,), {}) self.assertEqual(url, "/2010")
def test_expr_variable2(self): route = Route(r'/<year:\d{4}>/foo/', None) url = route.build(Request.blank('/'), (), dict(year='2010')) self.assertEqual(url, '/2010/foo/')
def test_build_int_variable(self): route = Route(r'/<:\d{4}>', None) url = route.build(Request.blank('/'), (2010,), {}) self.assertEqual(url, '/2010')
def test_build_int_keyword(self): route = Route(r'/<year:\d{4}>', None) url = route.build(Request.blank('/'), (), dict(year=2010)) self.assertEqual(url, '/2010')
def test_invalid_template(self): # To break it: # <>foo:><bar<:baz> route = Route("/<foo/<:bar", None) template = route.reverse_template or route._reverse_template() self.assertEqual(template, "/<foo/<:bar")
def test_simple_variable(self): route = Route(r"/<foo>", None) self.assertEqual(route.match(Request.blank("/bar")), (None, (), {"foo": "bar"})) url = route.build(Request.blank("/"), (), dict(foo="baz")) self.assertEqual(url, "/baz")