Skip to content

Commit

Permalink
Merge 02b204b into 4df7335
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Sep 21, 2018
2 parents 4df7335 + 02b204b commit 89aaf65
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
3 changes: 3 additions & 0 deletions app/http/controllers/ControllerTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ def returns_a_view(self, view: View):

def returns_a_dict(self):
return {'id': 1}

def param(self):
return self.request.param('id')
2 changes: 1 addition & 1 deletion masonite/helpers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def compile_route_to_regex(route):
regex += r'([a-zA-Z]+)'
else:
# default
regex += r'(\w+)'
regex += r'([\w.-]+)'
regex += r'\/'

# append the variable name passed @(variable):int to a list
Expand Down
4 changes: 1 addition & 3 deletions masonite/providers/RouteProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def register(self):
pass

def boot(self, router: Route, request: Request):

# All routes joined
for route in self.app.make('WebRoutes'):

Expand Down Expand Up @@ -49,8 +48,7 @@ def boot(self, router: Route, request: Request):
try:
parameter_dict = {}
for index, value in enumerate(matchurl.match(router.url).groups()):
parameter_dict[router.generated_url_list()[
index]] = value
parameter_dict[router.generated_url_list()[index]] = value
request.set_params(parameter_dict)
except AttributeError:
pass
Expand Down
3 changes: 1 addition & 2 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,7 @@ def compile_route_to_url(self, route, params={}):
if url:
# if the url contains a parameter variable like @id:int
if '@' in url:
url = url.replace('@', '').replace(
':int', '').replace(':string', '')
url = url.replace('@', '').split(':')[0]
compiled_url += str(params[url]) + '/'
else:
compiled_url += url + '/'
Expand Down
4 changes: 1 addition & 3 deletions masonite/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ def compile_route_to_regex(self, router):

# Split the route
split_given_route = self.route_url.split('/')

# compile the provided url into regex
url_list = []
regex = '^'
Expand All @@ -360,8 +359,7 @@ def compile_route_to_regex(self, router):

# append the variable name passed @(variable):int to a list
url_list.append(
regex_route.replace('@', '').replace(
':int', '').replace(':string', '')
regex_route.replace('@', '').split(':')[0]
)
else:
regex += regex_route + r'\/'
Expand Down
23 changes: 23 additions & 0 deletions tests/providers/test_route_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ def test_url_with_dashes_finds_route(self):

assert self.app.make('Request').param('endpoint') == 'user-endpoint'

def test_param_returns_param(self):
self.app.make('Route').url = '/test/1'
self.app.bind('WebRoutes', [get('/test/@id', ControllerTest.param)])

self.provider.boot(
self.app.make('Route'),
self.app.make('Request')
)

assert self.app.make('Response') == '1'

def test_custom_route_compiler_returns_param(self):
self.app.make('Route').url = '/test/1'
self.app.make('Route').compile('signed', r'([\w.-]+)')
self.app.bind('WebRoutes', [get('/test/@id:signed', ControllerTest.param)])

self.provider.boot(
self.app.make('Route'),
self.app.make('Request')
)

assert self.app.make('Response') == '1'

def test_route_subdomain_ignores_routes(self):
self.app.make('Route').url = '/test'
self.app.make('Environ')['HTTP_HOST'] = 'subb.domain.com'
Expand Down
18 changes: 17 additions & 1 deletion tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ def test_redirect_compiles_url_with_multiple_parameters(self):
'id': '1',
'test': 'user',
}
assert request.compile_route_to_url(route, params) == '/test/1/user'

def test_request_compiles_custom_route_compiler(self):
app = App()
app.bind('Request', self.request)
request = app.make('Request').load_app(app)

route = 'test/@id:signed'
params = {
'id': '1',
}
assert request.compile_route_to_url(route, params) == '/test/1'


def test_redirect_compiles_url_with_http(self):
Expand Down Expand Up @@ -459,7 +471,11 @@ def test_contains_for_path_with_digit_and_wrong_contains(self):

def test_contains_for_path_with_alpha_contains(self):
self.request.path = '/test/path/joe'
assert self.request.contains('/test/path/*:string')
assert self.request.contains('/test/path/*:string')

def test_contains_for_route_compilers(self):
self.request.path = '/test/path/joe'
assert self.request.contains('/test/path/*:signed')

def test_contains_multiple_asteriks(self):
self.request.path = '/dashboard/user/edit/1'
Expand Down

0 comments on commit 89aaf65

Please sign in to comment.