Skip to content

Commit

Permalink
Merge pull request #755 from timothycrosley/feature/fix-issue-753
Browse files Browse the repository at this point in the history
Feature/fix issue 753
  • Loading branch information
timothycrosley committed Mar 26, 2019
2 parents d1ad282 + c47d6ae commit 04e6e7b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Ideally, within a virtual environment.
Changelog
=========

### 2.4.5 - TBD
### 2.4.5 - March 25, 2019
- Fixed issue #753 - 404 not found does not respect default output format.
- Documented the `--without-cython` option in `CONTRIBUTING.md`
- Extended documentation for output formats

Expand Down
11 changes: 9 additions & 2 deletions hug/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,16 @@ def handle_404(request, response, *args, **kwargs):
"Here's a definition of the API to help you get going :)")
to_return['documentation'] = self.documentation(base_url, self.determine_version(request, False),
prefix=url_prefix)
response.data = hug.output_format.json(to_return, indent=4, separators=(',', ': '))

if self.output_format == hug.output_format.json:
response.data = hug.output_format.json(to_return, indent=4, separators=(',', ': '))
response.content_type = 'application/json; charset=utf-8'
else:
response.data = self.output_format(to_return)
response.content_type = self.output_format.content_type

response.status = falcon.HTTP_NOT_FOUND
response.content_type = 'application/json; charset=utf-8'

handle_404.interface = True
return handle_404

Expand Down
16 changes: 9 additions & 7 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,30 +307,32 @@ def accepts_get_and_post():
assert 'method not allowed' in hug.test.trace(api, 'accepts_get_and_post').status.lower()


def test_not_found():
def test_not_found(hug_api):
"""Test to ensure the not_found decorator correctly routes 404s to the correct handler"""
@hug.not_found()
@hug.not_found(api=hug_api)
def not_found_handler():
return "Not Found"

result = hug.test.get(api, '/does_not_exist/yet')
result = hug.test.get(hug_api, '/does_not_exist/yet')
assert result.data == "Not Found"
assert result.status == falcon.HTTP_NOT_FOUND

@hug.not_found(versions=10) # noqa
@hug.not_found(versions=10, api=hug_api) # noqa
def not_found_handler(response):
response.status = falcon.HTTP_OK
return {'look': 'elsewhere'}

result = hug.test.get(api, '/v10/does_not_exist/yet')
result = hug.test.get(hug_api, '/v10/does_not_exist/yet')
assert result.data == {'look': 'elsewhere'}
assert result.status == falcon.HTTP_OK

result = hug.test.get(api, '/does_not_exist/yet')
result = hug.test.get(hug_api, '/does_not_exist/yet')
assert result.data == "Not Found"
assert result.status == falcon.HTTP_NOT_FOUND

del api.http._not_found_handlers
hug_api.http.output_format = hug.output_format.text
result = hug.test.get(hug_api, '/v10/does_not_exist/yet')
assert result.data == "{'look': 'elsewhere'}"


def test_not_found_with_extended_api():
Expand Down

0 comments on commit 04e6e7b

Please sign in to comment.