Skip to content

Commit

Permalink
added ability to set status code in controller (#540)
Browse files Browse the repository at this point in the history
* added ability to set status code in controller

* flake8
  • Loading branch information
josephmancuso committed Jan 27, 2019
1 parent 71d9016 commit 5ede5d8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/http/controllers/TestController.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def change_header(self, request: Request):
request.header('Content-Type', 'application/xml')
return 'test'

def change_status(self, request: Request):
request.status(203)
return 'test'

def testing(self):
return 'test'

Expand Down
3 changes: 2 additions & 1 deletion masonite/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def view(self, view, status=200):
Returns:
string|dict|list -- Returns the data to be returned.
"""
self.request.status(status)
if self.request.get_status() in (404,):
self.request.status(status)

if isinstance(view, dict) or isinstance(view, list):
return self.json(view)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def setup_method(self):
self.app = App()
self.request = Request(generate_wsgi()).load_app(self.app)
self.app.bind('Request', self.request)
self.app.bind('StatusCode', '404 Not Found')
self.response = Response(self.app)

def test_can_set_json(self):
Expand Down Expand Up @@ -48,3 +49,8 @@ def test_view_can_return_integer_as_string(self):

assert self.app.make('Response') == '1'
assert self.request.is_status(200)

def test_view_can_set_own_status_code(self):

self.response.view(self.app.resolve(ControllerTest().change_status))
assert self.request.is_status(203)

0 comments on commit 5ede5d8

Please sign in to comment.