def test_can_add_routes(self): routes = Route.group([ Route.get("/home", "WelcomeController"), Route.post("/login", "WelcomeController"), ]) self.assertEqual(len(routes), 2)
def test_compile_year(self): Route.compile("year", r"[0-9]{4}") router = Router(Route.get("/year/@date:year", "WelcomeController@show")) route = router.find("/year/2005", "GET") self.assertTrue(route)
def test_can_add_routes_after(self): router = RouteCapsule(Route.get("/home", "WelcomeController")) router.add(Route.get("/added", None)) route = router.find("/added", "GET") self.assertTrue(route)
def test_can_add_routes(self): router = RouteCapsule( Route.get("/home", "WelcomeController"), Route.post("/login", "WelcomeController"), ) self.assertEqual(len(router.routes), 2)
def register_routes(self): Route.set_controller_locations( self.application.make("controllers.location")) self.application.bind("routes.location", "tests/integrations/web") self.application.make("router").add( Route.group( load(self.application.make("routes.location"), "ROUTES", []), middleware=["web"], ))
def test_can_make_route_group(self): router = Router( Route.group( Route.get("/group", "WelcomeController@show"), Route.post("/login", "WelcomeController@show"), prefix="/testing", )) route = router.find("/testing/group", "GET") self.assertTrue(route)
def test_group_naming(self): router = Router( Route.group( Route.get("/group", "WelcomeController@show").name(".index"), Route.post("/login", "WelcomeController@show").name(".index"), prefix="/testing", name="dashboard", )) route = router.find_by_name("dashboard.index") self.assertTrue(route)
def setUp(self): super().setUp() self.gate = self.application.make("gate") self.make_request() self.make_response() self.addRoutes( Route.get("/not-authorized", "WelcomeController@not_authorized"), Route.get("/authorize-helper", "WelcomeController@use_authorization_helper"), Route.get("/authorizations", "WelcomeController@authorizations"), )
def test_can_make_base_route_group(self): router = Router( Route.group( [ Route.get("", "WelcomeController@show"), ], prefix="/testing", )) route = router.find("/testing", "GET") self.assertTrue(route)
def test_can_compile_url_from_route_name(self): router = RouteCapsule( Route.get("/home/@id", "WelcomeController").name("home"), Route.get("/dashboard/@id/@user", "WelcomeController").name("dashboard"), ) url = router.route("home", {"id": 1}) self.assertEqual(url, "/home/1") url = router.route("dashboard", {"user": 2, "id": 1}) self.assertEqual(url, "/dashboard/1/2") url = router.route("dashboard", [2, 1]) self.assertEqual(url, "/dashboard/2/1")
def test_extract_parameters(self): router = RouteCapsule( Route.get("/params/@id", "WelcomeController@show").name("testparam") ) route = router.find_by_name("testparam") self.assertEqual(route.extract_parameters("/params/2")["id"], "2")
def test_gets_input_and_query_with_non_get_request(self): app = App() for method in ['POST', 'PUT', 'DELETE']: wsgi_environ = generate_wsgi() wsgi_environ['REQUEST_METHOD'] = method wsgi_environ[ 'QUERY_STRING'] = 'param=1¶m=2¶m=3&foo=bar&q=yes' wsgi_environ['wsgi.input'] = MockWsgiInput( '{"param": "hey", "foo": [9, 8, 7, 6], "bar": "baz"}') wsgi_environ['CONTENT_TYPE'] = 'application/json' route_class = Route(wsgi_environ) request_class = Request(wsgi_environ) app.bind('Request', request_class) app.bind('Route', route_class) request = app.make('Request').load_app(app) self.assertEqual(request.input('foo'), [9, 8, 7, 6]) self.assertEqual(request.input('param'), 'hey') self.assertEqual(request.input('not-exist', default=2), 2) self.assertEqual(request.input('q', default='default'), 'default') self.assertEqual(request.input('bar', default='default'), 'baz') self.assertEqual(request.query('foo'), 'bar') self.assertEqual(request.query('param'), '1') self.assertEqual(request.query('param', multi=True), ['1', '2', '3']) self.assertEqual(request.query('not-exist', default=2), 2) self.assertEqual(request.query('not-exist', default=2, multi=True), 2) self.assertEqual(request.query('q', default='default'), 'yes')
def test_gets_input_and_query_with_get_request(self): app = App() wsgi_environ = generate_wsgi() wsgi_environ['QUERY_STRING'] = 'param=1¶m=2¶m=3&foo=bar&q=yes' wsgi_environ['wsgi.input'] = { 'param': 'hey', 'foo': [9, 8, 7, 6], 'bar': 'baz' } wsgi_environ['REQUEST_METHOD'] = 'GET' route_class = Route(wsgi_environ) request_class = Request(wsgi_environ) app.bind('Request', request_class) app.bind('Route', route_class) request = app.make('Request').load_app(app) self.assertEqual(request.query('param'), '1') self.assertEqual(request.all_query()['param'], ['1', '2', '3']) self.assertEqual(request.query('foo'), 'bar') self.assertEqual(request.query('param', multi=True), ['1', '2', '3']) self.assertEqual(request.query('not-exist', default=2), 2) self.assertEqual(request.query('not-exist', default=2, multi=True), 2) self.assertEqual(request.query('q', default='no'), 'yes') self.assertEqual(request.input('foo'), 'bar') self.assertEqual(request.input('param'), '1') self.assertEqual(request.input('q', default='no'), 'yes') self.assertEqual(request.input('bar', default='default'), 'default')
def test_can_find_route_optional_params(self): router = RouteCapsule(Route.get("/home/?id", "WelcomeController")) route = router.find("/home/1", "GET") self.assertTrue(route) route = router.find("/home", "GET") self.assertTrue(route)
def test_can_find_route_compiler(self): router = RouteCapsule(Route.get("/route/@id:int", "WelcomeController")) route = router.find("/route/1", "GET") self.assertTrue(route) route = router.find("/route/string", "GET") self.assertFalse(route)
def test_find_by_name(self): router = RouteCapsule( Route.get("/getname", "WelcomeController@show").name("testname") ) route = router.find_by_name("testname") self.assertTrue(route)
def test_finds_correct_methods(self): router = RouteCapsule(Route.get("/test/1", "WelcomeController@show")) route = router.find("/test/1", "get") self.assertTrue(route) route = router.find("/test/1", "post") self.assertIsNone(route)
def test_can_make_route_group_deep_module_nested(self): router = Router( Route.get( "/test/deep", "tests.integrations.controllers.api.TestController@show")) route = router.find("/test/deep", "GET") self.assertTrue(route)
def test_correctly_parses_json_with_list(self): environ = generate_wsgi() environ['CONTENT_TYPE'] = 'application/json' environ['REQUEST_METHOD'] = 'POST' environ['wsgi.input'] = WsgiInputTestClass().load( b'{\n "options": ["foo", "bar"]\n}') route = Route(environ) self.assertEqual(route.environ['QUERY_STRING'], {"options": ["foo", "bar"]})
def test_domain(self): router = Router( Route.get("/domain/@id", "WelcomeController@show").domain("sub")) route = router.find("/domain/2", "get") self.assertIsNone(route) route = router.find("/domain/2", "get", "sub") self.assertTrue(route)
def test_get_json_input(self): json_wsgi = wsgi_request json_wsgi['REQUEST_METHOD'] = 'POST' json_wsgi['CONTENT_TYPE'] = 'application/json' json_wsgi['POST_DATA'] = '' json_wsgi['wsgi.input'] = MockWsgiInput('{"id": 1, "test": "testing"}') Route(json_wsgi) request_obj = Request(json_wsgi) self.assertIsInstance(request_obj.request_variables, dict) self.assertEqual(request_obj.input('id'), 1) self.assertEqual(request_obj.input('test'), 'testing')
def setUp(self): LoadEnvironment("testing") from wsgi import application self.application = application self.application.bind( "router", RouteCapsule( Route.set_controller_module_location( "tests.integrations.controllers").get( "/", "WelcomeController@show"), Route.set_controller_module_location( "tests.integrations.controllers").post( "/", "WelcomeController@show"), ), ) self.original_class_mocks = {} self._test_cookies = {} self._test_headers = {} if hasattr(self, "startTestRun"): self.startTestRun()
def test_correctly_parses_json_with_dictionary(self): environ = generate_wsgi() environ['CONTENT_TYPE'] = 'application/json' environ['REQUEST_METHOD'] = 'POST' environ['wsgi.input'] = WsgiInputTestClass().load( b'{\n "conta_corrente": {\n "ocultar": false,\n "visao_geral": true,\n "extrato": true\n }\n}' ) route = Route(environ) self.assertEqual( route.environ['QUERY_STRING'], { "conta_corrente": { "ocultar": False, "visao_geral": True, "extrato": True } })
def test_can_make_route_group_nested(self): router = Router( Route.group( Route.get("/group", "WelcomeController@show"), Route.post("/login", "WelcomeController@show"), Route.group( Route.get("/api/user", "WelcomeController@show"), Route.group(Route.get("/api/test", None), prefix="/v1"), ), prefix="/testing", )) route = router.find("/testing/api/user", "GET") self.assertTrue(route) route = router.find("/testing/v1/api/test", "GET") self.assertTrue(route)
def test_list_as_root_payload_reset_between_requests(self): app = App() wsgi_environ = generate_wsgi() wsgi_environ['REQUEST_METHOD'] = 'POST' wsgi_environ['CONTENT_TYPE'] = 'application/json' route_class = Route() request_class = Request() app.bind('Request', request_class) app.bind('Route', route_class) route = app.make('Route') request = app.make('Request').load_app(app) wsgi_environ['wsgi.input'] = MockWsgiInput('[1, 2]') route.load_environ(wsgi_environ) request.load_environ(wsgi_environ) self.assertEqual(request.all(), [1, 2]) wsgi_environ['wsgi.input'] = MockWsgiInput('{"key": "val"}') route.load_environ(wsgi_environ) request.load_environ(wsgi_environ) self.assertEqual(request.all(), {"key": "val"})
class TestRoutes(unittest.TestCase): def setUp(self): self.route = Route(generate_wsgi()) self.request = Request(generate_wsgi()) def test_route_is_callable(self): self.assertTrue(callable(Get)) self.assertTrue(callable(Head)) self.assertTrue(callable(Post)) self.assertTrue(callable(Match)) self.assertTrue(callable(Put)) self.assertTrue(callable(Patch)) self.assertTrue(callable(Delete)) self.assertTrue(callable(Connect)) self.assertTrue(callable(Options)) self.assertTrue(callable(Trace)) def test_route_prefixes_forward_slash(self): self.assertEqual( Get().route('some/url', 'TestController@show').route_url, '/some/url') def test_route_is_not_post(self): self.assertEqual(self.route.is_post(), False) def test_route_is_post(self): self.route.environ['REQUEST_METHOD'] = 'POST' self.assertEqual(self.route.is_post(), True) def test_compile_route_to_regex(self): get_route = Get().route('test/route', None) self.assertEqual(get_route.compile_route_to_regex(), r'^\/test\/route\/$') get_route = Get().route('test/@route', None) self.assertEqual(get_route.compile_route_to_regex(), r'^\/test\/([\w.-]+)\/$') get_route = Get().route('test/@route:int', None) self.assertEqual(get_route.compile_route_to_regex(), r'^\/test\/(\d+)\/$') get_route = Get().route('test/@route:string', None) self.assertEqual(get_route.compile_route_to_regex(), r'^\/test\/([a-zA-Z]+)\/$') def test_route_can_add_compilers(self): get_route = Get().route('test/@route:int', None) self.assertEqual(get_route.compile_route_to_regex(), r'^\/test\/(\d+)\/$') self.route.compile('year', r'[0-9]{4}') get_route = Get().route('test/@route:year', None) self.assertEqual(get_route.compile_route_to_regex(), r'^\/test\/[0-9]{4}\/$') with self.assertRaises(InvalidRouteCompileException): get_route = Get().route('test/@route:none', None) create_matchurl('/test/1', get_route) def test_route_gets_controllers(self): self.assertTrue(Get().route('test/url', 'TestController@show')) self.assertTrue(Get().route( 'test/url', '/app.http.test_controllers.TestController@show')) def test_route_doesnt_break_on_incorrect_controller(self): self.assertTrue(Get().route('test/url', 'BreakController@show')) def test_route_can_pass_route_values_in_constructor(self): route = Get('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Head('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Post('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Put('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Patch('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Delete('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Connect('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Options('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') route = Trace('test/url', 'BreakController@show') self.assertEqual(route.route_url, '/test/url') def test_route_can_pass_route_values_in_constructor_and_use_middleware( self): route = Get('test/url', 'BreakController@show').middleware('auth') self.assertEqual(route.route_url, '/test/url') self.assertEqual(route.list_middleware, ['auth']) def test_route_gets_deeper_module_controller(self): route = Get().route('test/url', 'subdirectory.SubController@show') self.assertTrue(route.controller) self.assertIsInstance(route.controller, SubController.__class__) def test_route_can_have_multiple_routes(self): self.assertEqual( Match(['GET', 'POST']).route('test/url', 'TestController@show').method_type, ['GET', 'POST']) def test_match_routes_convert_lowercase_to_uppercase(self): self.assertEqual( Match(['Get', 'Post']).route('test/url', 'TestController@show').method_type, ['GET', 'POST']) def test_match_routes_raises_exception_with_non_list_method_types(self): with self.assertRaises(RouteException): self.assertEqual( Match('get').route('test/url', 'TestController@show').method_type, ['GET', 'POST']) def test_group_route(self): routes = group('/example', [ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ]) self.assertEqual(routes[0].route_url, '/example/test/1') self.assertEqual(routes[1].route_url, '/example/test/2') def test_group_route_sets_middleware(self): routes = RouteGroup([ Get().route('/test/1', 'TestController@show').middleware('another'), Get().route('/test/2', 'TestController@show'), RouteGroup([ Get().route('/test/3', 'TestController@show'), Get().route('/test/4', 'TestController@show') ], middleware=('test', 'test2')) ], middleware=('auth', 'user')) self.assertIsInstance(routes, list) self.assertEqual(['another', 'auth', 'user'], routes[0].list_middleware) self.assertEqual(['auth', 'user'], routes[1].list_middleware) self.assertEqual(['test', 'test2', 'auth', 'user'], routes[2].list_middleware) def test_group_route_sets_domain(self): routes = RouteGroup([ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ], domain=['www']) self.assertEqual(routes[0].required_domain, ['www']) def test_group_adds_methods(self): routes = RouteGroup([ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ], add_methods=['OPTIONS']) self.assertEqual(routes[0].method_type, ['GET', 'OPTIONS']) def test_group_route_sets_prefix(self): routes = RouteGroup([ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ], prefix='/dashboard') self.assertEqual(routes[0].route_url, '/dashboard/test/1') def test_group_route_sets_prefix_no_route(self): routes = RouteGroup([ Get('', 'TestController@show'), ], prefix='/dashboard') self.assertEqual(routes[0].route_url, '/dashboard') def test_group_route_sets_name(self): RouteGroup([ Get().route('/test/1', 'TestController@show').name('create'), Get().route('/test/2', 'TestController@show').name('edit') ], name='post.') def test_group_route_sets_name_for_none_route(self): routes = RouteGroup([ Get().route('/test/1', 'TestController@show').name('create'), Get().route('/test/2', 'TestController@show') ], name='post.') self.assertEqual(routes[0].named_route, 'post.create') self.assertEqual(routes[1].named_route, None) def test_flatten_flattens_multiple_lists(self): routes = [ Get().route('/test/1', 'TestController@show').name('create'), RouteGroup([ Get().route('/test/1', 'TestController@show').name('create'), Get().route('/test/2', 'TestController@show').name('edit'), RouteGroup([ Get().route('/test/1', 'TestController@show').name('update'), Get().route('/test/2', 'TestController@show').name('delete'), RouteGroup([ Get().route('/test/3', 'TestController@show').name('update'), Get().route('/test/4', 'TestController@show').name('delete'), ], middleware=('auth')), ], name='post.') ], prefix='/dashboard') ] routes = flatten_routes(routes) self.assertEqual(routes[3].route_url, '/dashboard/test/1') self.assertEqual(routes[3].named_route, 'post.update') def test_correctly_parses_json_with_dictionary(self): environ = generate_wsgi() environ['CONTENT_TYPE'] = 'application/json' environ['REQUEST_METHOD'] = 'POST' environ['wsgi.input'] = WsgiInputTestClass().load( b'{\n "conta_corrente": {\n "ocultar": false,\n "visao_geral": true,\n "extrato": true\n }\n}' ) route = Route(environ) self.assertEqual( route.environ['QUERY_STRING'], { "conta_corrente": { "ocultar": False, "visao_geral": True, "extrato": True } }) def test_correctly_parses_json_with_list(self): environ = generate_wsgi() environ['CONTENT_TYPE'] = 'application/json' environ['REQUEST_METHOD'] = 'POST' environ['wsgi.input'] = WsgiInputTestClass().load( b'{\n "options": ["foo", "bar"]\n}') route = Route(environ) self.assertEqual(route.environ['QUERY_STRING'], {"options": ["foo", "bar"]}) def test_redirect_route(self): route = Redirect('/test1', '/test2') request = Request(generate_wsgi()) route.load_request(request) request.load_app(App()) route.get_response() self.assertTrue(request.is_status(302)) self.assertEqual(request.redirect_url, '/test2') def test_redirect_can_use_301(self): request = Request(generate_wsgi()) route = Redirect('/test1', '/test3', status=301) route.load_request(request) request.load_app(App()) route.get_response() self.assertTrue(request.is_status(301)) self.assertEqual(request.redirect_url, '/test3') def test_redirect_can_change_method_type(self): route = Redirect('/test1', '/test3', methods=['POST', 'PUT']) self.assertEqual(route.method_type, ['POST', 'PUT'])
def setUp(self): Route.set_controller_module_location("tests.integrations.controllers") pass
def test_can_find_route_with_parameter(self): router = RouteCapsule(Route.get("/home/@id", "WelcomeController")) route = router.find("/home/1", "GET") self.assertTrue(route)
def test_can_find_route(self): router = RouteCapsule(Route.get("/home", "WelcomeController")) route = router.find("/home/", "GET") self.assertTrue(route)
def setUp(self): self.route = Route(generate_wsgi()) self.request = Request(generate_wsgi())