Skip to content

Commit

Permalink
added ability to specify routes in the initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Jan 30, 2019
1 parent 89b98ef commit 36701e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
24 changes: 18 additions & 6 deletions masonite/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,59 +360,71 @@ def compile_route_to_regex(self, router):
class Get(BaseHttpRoute):
"""Class for specifying GET requests."""

def __init__(self):
def __init__(self, route=None, output=None):
"""Get constructor."""
self.method_type = ['GET']
self.list_middleware = []
if route and output:
self.route(route, output)


class Post(BaseHttpRoute):
"""Class for specifying POST requests."""

def __init__(self):
def __init__(self, route=None, output=None):
"""Post constructor."""
self.method_type = ['POST']
self.list_middleware = []
if route and output:
self.route(route, output)


class Match(BaseHttpRoute):
"""Class for specifying POST requests."""

def __init__(self, method_type=['GET']):
def __init__(self, method_type=['GET'], route=None, output=None):
"""Post constructor."""
if not isinstance(method_type, list):
raise RouteException("Method type needs to be a list. Got '{}'".format(method_type))

# Make all method types in list uppercase
self.method_type = [x.upper() for x in method_type]
self.list_middleware = []
if route and output:
self.route(route, output)


class Put(BaseHttpRoute):
"""Class for specifying PUT requests."""

def __init__(self):
def __init__(self, route=None, output=None):
"""Put constructor."""
self.method_type = ['PUT']
self.list_middleware = []
if route and output:
self.route(route, output)


class Patch(BaseHttpRoute):
"""Class for specifying Patch requests."""

def __init__(self):
def __init__(self, route=None, output=None):
"""Patch constructor."""
self.method_type = ['PATCH']
self.list_middleware = []
if route and output:
self.route(route, output)


class Delete(BaseHttpRoute):
"""Class for specifying Delete requests."""

def __init__(self):
def __init__(self, route=None, output=None):
"""Delete constructor."""
self.method_type = ['DELETE']
self.list_middleware = []
if route and output:
self.route(route, output)


class ViewRoute(BaseHttpRoute):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ def test_route_gets_controllers(self):
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
Expand Down

0 comments on commit 36701e5

Please sign in to comment.