Skip to content

Commit

Permalink
fixed route group middleware issue
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Aug 30, 2018
1 parent d3a9f79 commit af76aa3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
16 changes: 10 additions & 6 deletions masonite/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class BaseHttpRoute:
named_route = None
required_domain = None
module_location = 'app.http.controllers'
list_middleware = []
list_middleware = None

def route(self, route, output):
"""Loads the route into the class. This also looks for the controller and attaches it to the route.
Expand Down Expand Up @@ -323,7 +323,10 @@ def middleware(self, *args):
self
"""

self.list_middleware = args
for arg in args:
if arg not in self.list_middleware:
self.list_middleware.append(arg)

return self

def run_middleware(self, type_of_middleware):
Expand Down Expand Up @@ -366,6 +369,7 @@ def __init__(self):
"""

self.method_type = 'GET'
self.list_middleware = []


class Post(BaseHttpRoute):
Expand All @@ -377,6 +381,7 @@ def __init__(self):
"""

self.method_type = 'POST'
self.list_middleware = []


class Put(BaseHttpRoute):
Expand All @@ -388,7 +393,7 @@ def __init__(self):
"""

self.method_type = 'PUT'

self.list_middleware = []

class Patch(BaseHttpRoute):
"""Class for specifying Patch requests
Expand All @@ -399,7 +404,7 @@ def __init__(self):
"""

self.method_type = 'PATCH'

self.list_middleware = []

class Delete(BaseHttpRoute):
"""Class for specifying Delete requests
Expand All @@ -410,7 +415,7 @@ def __init__(self):
"""

self.method_type = 'DELETE'

self.list_middleware = []

class RouteGroup():
"""Class for specifying Route Groups
Expand All @@ -431,7 +436,6 @@ def __new__(self, routes=[], middleware=[], domain=[], prefix='', name=''):
"""

from masonite.helpers.routes import flatten_routes

self.routes = flatten_routes(routes)

if middleware:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_request_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_loads_request(self):
def test_loads_middleware(self):
get = Get().route('test', None).middleware('auth', 'middleware')

assert get.list_middleware == ('auth', 'middleware')
assert get.list_middleware == ['auth', 'middleware']


def test_method_type(self):
Expand Down
22 changes: 10 additions & 12 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,19 @@ def test_group_route(self):
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'])
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 routes[0].list_middleware == ('auth', 'user')
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([
Expand Down

0 comments on commit af76aa3

Please sign in to comment.