Skip to content

Commit

Permalink
Merge b702165 into e845591
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Jun 30, 2018
2 parents e845591 + b702165 commit 094d72b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
33 changes: 33 additions & 0 deletions masonite/helpers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,36 @@ def group(url, route_list):
route.route_url = url + route.route_url

return route_list

def compile_route_to_regex(route):
# Split the route
split_given_route = route.split('/')

# compile the provided url into regex
url_list = []
regex = '^'
for regex_route in split_given_route:
if '*' in regex_route or '@' 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'\/'

if regex.endswith('/') and not route.endswith('/'):
regex = regex[:-2]

regex += '$'

return regex
5 changes: 5 additions & 0 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
of this class.
"""
from urllib.parse import parse_qs
import re
from masonite.helpers.routes import compile_route_to_regex
from http import cookies

import tldextract
Expand Down Expand Up @@ -340,6 +342,9 @@ def back(self, input_parameter='back'):
self.redirect(self.input(input_parameter))
return self

def contains(self, route):
return re.match(compile_route_to_regex(route), self.path)

def compile_route_to_url(self, route, params={}):
"""
Compile the route url into a usable url
Expand Down
23 changes: 23 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,26 @@ def test_request_sets_request_method(self):
assert request.input('__method') == 'PUT'
assert request.get_request_method() == 'PUT'

def test_contains_for_path_detection(self):
self.request.path = '/test/path'
assert self.request.contains('/test/*')
assert self.request.contains('/test/path')
assert not self.request.contains('/test/wrong')

def test_contains_for_path_with_digit(self):
self.request.path = '/test/path/1'
assert self.request.contains('/test/path/*')
assert self.request.contains('/test/path/*:int')

def test_contains_for_path_with_digit_and_wrong_contains(self):
self.request.path = '/test/path/joe'
assert not self.request.contains('/test/path/*:int')

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

def test_contains_multiple_asteriks(self):
self.request.path = '/dashboard/user/edit/1'
assert self.request.contains('/dashboard/user/*:string/*:int')

0 comments on commit 094d72b

Please sign in to comment.