class TestRoutes: def setup_method(self): self.route = Route(generate_wsgi()) self.request = Request(generate_wsgi()) def test_route_is_callable(self): assert callable(Get) assert callable(Post) assert callable(Put) assert callable(Patch) assert callable(Delete) def test_route_get_returns_output(self): assert self.route.get('url', 'output') == 'output' def test_route_is_not_post(self): assert self.route.is_post() == False def test_route_is_post(self): self.route.environ['REQUEST_METHOD'] = 'POST' assert self.route.is_post() == True def test_compile_route_to_regex(self): assert self.route.compile_route_to_regex(Get().route( 'test/route', None)) == '^test\\/route\\/$' assert self.route.compile_route_to_regex(Get().route( 'test/@route', None)) == '^test\\/(\\w+)\\/$' assert self.route.compile_route_to_regex(Get().route( 'test/@route:int', None)) == '^test\\/(\\d+)\\/$' assert self.route.compile_route_to_regex(Get().route( 'test/@route:string', None)) == '^test\\/([a-zA-Z]+)\\/$' def test_route_gets_controllers(self): assert Get().route('test/url', 'TestController@show') assert Get().route('test/url', '/app.http.test_controllers.TestController@show') def test_route_doesnt_break_on_incorrect_controller(self): assert Get().route('test/url', 'BreakController@show') def test_group_route(self): routes = group('/example', [ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ]) assert routes[0].route_url == '/example/test/1' assert routes[1].route_url == '/example/test/2'
def setUp(self): super().setUp() self.setRoutes( Route.get("home", "TestController@show").name("home"), Route.get("posts", "TestController@show").name("posts.index"), Route.get("posts/@post", "TestController@show").name("posts.show"), Route.get("posts/@post/comments", "TestController@show").name("postComments.index"), Route.get("posts/@post/comments/@comment:int", "TestController@show").name( "postComments.show" ), Route.post("posts", "TestController@show").name("posts.store"), Route.get("admin/users", "TestController@show").name("admin.users.index"), )
def setUp(self): super().setUp() self.setRoutes( Route.get("home", "TestController@show").name("home"), Route.get("posts", "TestController@show").name("posts.index"), Route.get("posts/@post", "TestController@show").name("posts.show"), Route.get("posts/@post/comments", "TestController@show").name("postComments.index"), Route.get("posts/@post/comments/@comment:int", "TestController@show").name("postComments.show"), Route.post("posts", "TestController@show").name("posts.store"), Route.get("admin/users", "TestController@show").name("admin.users.index"), ) self.original_config = { "filters": { "only": [], "except": [], "groups": {} }, "skip_route_function": False, }
class TestRoutes: def setup_method(self): self.route = Route(generate_wsgi()) self.request = Request(generate_wsgi()) def test_route_is_callable(self): assert callable(Get) assert callable(Post) assert callable(Put) assert callable(Patch) assert callable(Delete) def test_route_get_returns_output(self): assert self.route.get('url', 'output') == 'output' def test_route_is_not_post(self): assert self.route.is_post() == False def test_route_is_post(self): self.route.environ['REQUEST_METHOD'] = 'POST' assert self.route.is_post() == True def test_compile_route_to_regex(self): get_route = Get().route('test/route', None) assert get_route.compile_route_to_regex( self.route) == r'^test\/route\/$' get_route = Get().route('test/@route', None) assert get_route.compile_route_to_regex( self.route) == r'^test\/([\w.-]+)\/$' get_route = Get().route('test/@route:int', None) assert get_route.compile_route_to_regex( self.route) == r'^test\/(\d+)\/$' get_route = Get().route('test/@route:string', None) assert get_route.compile_route_to_regex( self.route) == r'^test\/([a-zA-Z]+)\/$' def test_route_can_add_compilers(self): get_route = Get().route('test/@route:int', None) assert get_route.compile_route_to_regex( self.route) == r'^test\/(\d+)\/$' self.route.compile('year', r'[0-9]{4}') get_route = Get().route('test/@route:year', None) assert get_route.compile_route_to_regex( self.route) == r'^test\/[0-9]{4}\/$' get_route = Get().route('test/@route:slug', None) with pytest.raises(InvalidRouteCompileException): get_route.compile_route_to_regex(self.route) def test_route_gets_controllers(self): assert Get().route('test/url', 'TestController@show') assert Get().route('test/url', '/app.http.test_controllers.TestController@show') def test_route_doesnt_break_on_incorrect_controller(self): assert Get().route('test/url', 'BreakController@show') def test_route_can_pass_route_values_in_constructor(self): route = Get('test/url', 'BreakController@show') assert route.route_url == 'test/url' route = Post('test/url', 'BreakController@show') assert route.route_url == 'test/url' route = Put('test/url', 'BreakController@show') assert route.route_url == 'test/url' route = Patch('test/url', 'BreakController@show') assert route.route_url == 'test/url' route = Delete('test/url', 'BreakController@show') assert 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') assert route.route_url == 'test/url' assert route.list_middleware == ['auth'] def test_route_gets_deeper_module_controller(self): route = Get().route('test/url', 'subdirectory.SubController@show') assert route.controller assert isinstance(route.controller, SubController.__class__) def test_route_can_have_multiple_routes(self): assert Match(['GET', 'POST']).route( 'test/url', 'TestController@show').method_type == ['GET', 'POST'] def test_match_routes_convert_lowercase_to_uppercase(self): assert 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 pytest.raises(RouteException): assert 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') ]) assert routes[0].route_url == '/example/test/1' assert 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')) assert isinstance(routes, list) assert ['another', 'auth', 'user'] == routes[0].list_middleware assert ['auth', 'user'] == routes[1].list_middleware assert ['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']) assert 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']) assert 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') assert routes[0].route_url == '/dashboard/test/1' 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.') assert routes[0].named_route == 'post.create' assert 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) assert routes[3].route_url == '/dashboard/test/1' assert 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) assert 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) assert 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() assert request.is_status(302) assert 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() assert request.is_status(301) assert request.redirect_url == '/test3' def test_redirect_can_change_method_type(self): request = Request(generate_wsgi()) route = Redirect('/test1', '/test3', methods=['POST', 'PUT']) assert route.method_type == ['POST', 'PUT']
class TestRoutes: def setup_method(self): self.route = Route(generate_wsgi()) self.request = Request(generate_wsgi()) def test_route_is_callable(self): assert callable(Get) assert callable(Post) assert callable(Put) assert callable(Patch) assert callable(Delete) def test_route_get_returns_output(self): assert self.route.get('url', 'output') == 'output' def test_route_is_not_post(self): assert self.route.is_post() == False def test_route_is_post(self): self.route.environ['REQUEST_METHOD'] = 'POST' assert self.route.is_post() == True def test_compile_route_to_regex(self): assert self.route.compile_route_to_regex(Get().route( 'test/route', None)) == '^test\\/route\\/$' assert self.route.compile_route_to_regex(Get().route( 'test/@route', None)) == '^test\\/([\\w.-]+)\\/$' assert self.route.compile_route_to_regex(Get().route( 'test/@route:int', None)) == '^test\\/(\\d+)\\/$' assert self.route.compile_route_to_regex(Get().route( 'test/@route:string', None)) == '^test\\/([a-zA-Z]+)\\/$' def test_route_gets_controllers(self): assert Get().route('test/url', 'TestController@show') assert Get().route('test/url', '/app.http.test_controllers.TestController@show') def test_route_doesnt_break_on_incorrect_controller(self): assert Get().route('test/url', 'BreakController@show') def test_group_route(self): routes = group('/example', [ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ]) assert routes[0].route_url == '/example/test/1' assert routes[1].route_url == '/example/test/2' def test_group_route_sets_middleware(self): routes = group('/example', [ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ]) assert routes[0].route_url == '/example/test/1' routes = RouteGroup([ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ], middleware=['auth', 'user']) assert isinstance(routes, list) assert routes[0].list_middleware == ('auth', 'user') def test_group_route_sets_domain(self): routes = RouteGroup([ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ], domain=['www']) assert routes[0].required_domain == ['www'] def test_group_route_sets_prefix(self): routes = RouteGroup([ Get().route('/test/1', 'TestController@show'), Get().route('/test/2', 'TestController@show') ], prefix='/dashboard') assert routes[0].route_url == '/dashboard/test/1' def test_group_route_sets_name(self): look_for = [] routes = 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): look_for = [] routes = RouteGroup([ Get().route('/test/1', 'TestController@show').name('create'), Get().route('/test/2', 'TestController@show') ], name='post.') assert routes[0].named_route == 'post.create' assert 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) assert routes[3].route_url == '/dashboard/test/1' assert routes[3].named_route == 'post.update'
"""Web Routes.""" from masonite.routes import Route ROUTES = [ Route.get('/', 'WelcomeController@show').name('welcome'), ]
from masonite.routes import Route ROUTES = [ Route.get("/", "UserController@index"), Route.get("/user/@id:int", "UserController@show"), Route.post("/user", "UserController@create"), ]
from masonite.authentication import Auth from masonite.routes import Route ROUTES = [ Route.get("/", "HomeController@show"), # Blog Routes Route.get("/blog", "BlogController@show"), Route.get("/blog/new", "BlogController@new"), Route.post("/blog/create", "BlogController@create"), Route.get("/blog/@slug", "BlogController@detail"), Route.get("/blog/edit/@id", "BlogController@edit"), Route.post("/blog/update/@id", "BlogController@update"), Route.get("/blog/@id/delete", "BlogController@delete"), ] ROUTES += Auth.routes()
def app(environ, start_response): ''' Framework Engine ''' os.environ.setdefault('REQUEST_METHOD', environ['REQUEST_METHOD']) os.environ.setdefault('URI_PATH', environ['PATH_INFO']) # if this is a post request if environ['REQUEST_METHOD'] == 'POST': get_post_params = int(environ.get('CONTENT_LENGTH')) if environ.get( 'CONTENT_LENGTH') else 0 body = environ['wsgi.input'].read( get_post_params) if get_post_params > 0 else '' environ['QUERY_STRING'] = body.decode('utf-8') router = Route(environ) import routes.web routes = routes.web.ROUTES request = Request(environ) for route in routes: split_given_route = route.route_url.split('/') url_list = [] regex = '^' for regex_route in split_given_route: if '@' in regex_route: if ':int' in regex_route: regex += r'(\d+)' elif ':string' in regex_route: regex += r'([a-zA-Z]+)' else: # default regex += r'(\w+)' regex += r'\/' # append the variable name passed @(variable):int to a list url_list.append( regex_route.replace('@', '').replace(':int', '').replace(':string', '')) else: regex += regex_route + r'\/' regex += '$' if route.route_url.endswith('/'): matchurl = re.compile(regex.replace(r'\/\/$', r'\/$')) else: matchurl = re.compile(regex.replace(r'\/$', r'$')) try: parameter_dict = {} for index, value in enumerate(matchurl.match(router.url).groups()): parameter_dict[url_list[index]] = value request.set_params(parameter_dict) except AttributeError: pass if matchurl.match(router.url) and route.method_type == environ[ 'REQUEST_METHOD'] and route.continueroute is True: print(route.method_type + ' Route: ' + router.url) data = router.get(route.route, route.output(request)) break else: data = 'Route not found. Error 404' if data == 'Route not found. Error 404': # look at the API routes files import routes.api routes = routes.api.ROUTES for route in routes: if route.url in router.url: data = route.fetch(request).output if data: break else: data = 'Route not found. Error 404' else: data = 'Route not found. Error 404' if request.redirect_route: for route in routes: if route.named_route == request.redirect_route: print(route.method_type + ' Named Route: ' + router.url) data = router.get(route.route, route.output(request)) request.redirect_url = route.route_url data = bytes(data, 'utf-8') if not request.redirect_url: start_response("200 OK", [("Content-Type", "text/html; charset=utf-8"), ("Content-Length", str(len(data)))] + request.get_cookies()) else: start_response("302 OK", [('Location', request.redirect_url)] + request.get_cookies()) return iter([data])
from masonite.routes import Route ROUTES = [Route.get("/", "WelcomeController@show")]
""" Web Routes """ from masonite.routes import Route ROUTES = [ Route.get("/", "WelcomeController@show").name("home"), Route.get("posts", "WelcomeController@posts").name("posts.index"), Route.post("posts", "WelcomeController@create_post").name("posts.create"), # Route.match(["get", "post"], "posts", "WelcomeController@posts").name("posts"), Route.get("posts/@id", "WelcomeController@show_post").name("posts.show"), Route.get( "posts/@post_id/comments/@id", "WelcomeController@show_post_comments").name("posts.comments.show"), ]
class TestRoutes: def setup_method(self): self.route = Route(generate_wsgi()) self.request = Request(generate_wsgi()) def test_route_is_callable(self): assert callable(Get) assert callable(Post) assert callable(Put) assert callable(Patch) assert callable(Delete) def test_route_get_returns_output(self): assert self.route.get('url', 'output') == 'output' def test_route_is_not_post(self): assert self.route.is_post() == False def test_route_is_post(self): self.route.environ['REQUEST_METHOD'] = 'POST' assert self.route.is_post() == True def test_compile_route_to_regex(self): get_route = Get().route('test/route', None) assert get_route.compile_route_to_regex(self.route) == '^test\\/route\\/$' get_route = Get().route('test/@route', None) assert get_route.compile_route_to_regex(self.route) == '^test\\/([\\w.-]+)\\/$' get_route = Get().route('test/@route:int', None) assert get_route.compile_route_to_regex(self.route) == '^test\\/(\\d+)\\/$' get_route = Get().route('test/@route:string', None) assert get_route.compile_route_to_regex(self.route) == '^test\\/([a-zA-Z]+)\\/$' def test_route_can_add_compilers(self): get_route = Get().route('test/@route:int', None) assert get_route.compile_route_to_regex(self.route) == '^test\\/(\\d+)\\/$' self.route.compile('year', r'[0-9]{4}') get_route = Get().route('test/@route:year', None) assert get_route.compile_route_to_regex(self.route) == '^test\\/[0-9]{4}\\/$' get_route = Get().route('test/@route:slug', None) with pytest.raises(InvalidRouteCompileException): get_route.compile_route_to_regex(self.route) def test_route_gets_controllers(self): assert Get().route('test/url', 'TestController@show') assert Get().route('test/url', '/app.http.test_controllers.TestController@show') def test_route_doesnt_break_on_incorrect_controller(self): assert Get().route('test/url', 'BreakController@show') def test_route_gets_deeper_module_controller(self): route = Get().route('test/url', 'subdirectory.SubController@show') assert route.controller assert isinstance(route.controller, SubController.__class__) def test_route_can_have_multiple_routes(self): assert Match(['GET', 'POST']).route('test/url', 'TestController@show').method_type == ['GET', 'POST'] def test_match_routes_convert_lowercase_to_uppercase(self): assert 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 pytest.raises(RouteException): assert 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') ]) assert routes[0].route_url == '/example/test/1' assert 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')) assert isinstance(routes, list) assert ['another', 'auth', 'user'] == routes[0].list_middleware assert ['auth', 'user'] == routes[1].list_middleware assert ['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']) assert 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']) assert 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') assert routes[0].route_url == '/dashboard/test/1' 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.') assert routes[0].named_route == 'post.create' assert 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) assert routes[3].route_url == '/dashboard/test/1' assert routes[3].named_route == 'post.update'