Skip to content

Commit

Permalink
Merge 64945e6 into a6536c6
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Nov 16, 2018
2 parents a6536c6 + 64945e6 commit 126812f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
4 changes: 2 additions & 2 deletions masonite/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def resolve(self, obj, *resolving_arguments):
" constructor using the 'resolve_parameters=True' keyword argument.")
try:
return obj(*provider_list)
except TypeError:
raise ContainerError('Tried resolving the too many dependencies.')
except TypeError as e:
raise ContainerError('Either Tried resolving the too many dependencies or {}'.format(str(e)))

def collect(self, search):
"""Fetch a dictionary of objects using a search query.
Expand Down
2 changes: 1 addition & 1 deletion masonite/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def handle(self, exception):
}
).rendered_template
self._app.bind('Response', rendered_view)
request.header('Content-Type', str(len(rendered_view)))
request.header('Content-Length', str(len(rendered_view)))


class DD:
Expand Down
5 changes: 5 additions & 0 deletions masonite/providers/RouteProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ def boot(self, router: Route, request: Request):

"""No Response was found in the for loop so let's set an arbitrary response now.
"""
request.status(404)
self.app.bind('Response', 'Route not found. Error 404')
# If the route exists but not the method is incorrect
if request.is_status(404) and request.route_exists(request.path):
self.app.bind('Response', 'Method not allowed')
request.status(405)
4 changes: 2 additions & 2 deletions masonite/providers/StatusCodeProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def register(self):

def boot(self):
request = self.app.make('Request')
if request.get_status_code() == '200 OK':
if request.is_status(200):
return

if request.get_status_code() in ('500 Internal Server Error', '404 Not Found', '503 Service Unavailable'):
if request.get_status() not in (200, 301, 302):
if self.app.make('ViewClass').exists('errors/{}'.format(request.get_status_code().split(' ')[0])):
rendered_view = self.app.make('View')(
'errors/{}'.format(request.get_status_code().split(' ')[0])).rendered_template
Expand Down
22 changes: 22 additions & 0 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,28 @@ def get_status_code(self):
"""
return self.app().make('StatusCode')

def is_status(self, code):
return self._get_status_code_by_value(self.get_status_code()) == code

def route_exists(self, url):
web_routes = self.container.make('WebRoutes')

for route in web_routes:
if route.route_url == url:
return True

return False

def _get_status_code_by_value(self, value):
for key, status in self.statuses.items():
if status == value:
return key

return None

def get_status(self):
return self._get_status_code_by_value(self.get_status_code())

def get_request_method(self):
"""Get the current request method.
Expand Down
36 changes: 36 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,30 @@ def test_request_sets_int_status_code(self):
request.status(500)
assert request.get_status_code() == '500 Internal Server Error'

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

request.status(500)
assert request.get_status() == 500

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

request.status(500)
assert request._get_status_code_by_value('500 Internal Server Error') == 500

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

request.status(500)
assert request.is_status(500) == True

def test_request_sets_invalid_int_status_code(self):
with pytest.raises(InvalidHTTPStatusCode):
app = App()
Expand Down Expand Up @@ -446,6 +470,18 @@ def test_is_named_route(self):
request.path = '/test/url/1'
assert request.is_named_route('test.id', {'id': 1})

def test_route_exists(self):
app = App()
app.bind('Request', self.request)
app.bind('WebRoutes', [
get('/test/url', None).name('test.url'),
get('/test/url/@id', None).name('test.id')
])
request = app.make('Request').load_app(app)

assert request.route_exists('/test/url') == True
assert request.route_exists('/test/Not') == False

def test_request_url_from_controller(self):
app = App()
app.bind('Request', self.request)
Expand Down

0 comments on commit 126812f

Please sign in to comment.