Skip to content

Commit

Permalink
fix(errors): make title for HTTPNotAcceptable overridable (#1498)
Browse files Browse the repository at this point in the history
* fix(errors): overridable title for HTTPNotAcceptable and HTTPUnsupportedMediaType

* fixed quotes

* fix(errors): fixed tests

* fix(errors): fixed tests

* fixed tests

* fixed tests
  • Loading branch information
Coykto authored and vytas7 committed May 22, 2019
1 parent ac739b7 commit 3b8ca25
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
8 changes: 4 additions & 4 deletions falcon/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ class HTTPNotAcceptable(HTTPError):
base articles related to this error (default ``None``).
"""

def __init__(self, description=None, headers=None, **kwargs):
def __init__(self, title=None, description=None, headers=None, **kwargs):
super(HTTPNotAcceptable, self).__init__(status.HTTP_406,
'Media type not acceptable',
title,
description, headers,
**kwargs)

Expand Down Expand Up @@ -763,9 +763,9 @@ class HTTPUnsupportedMediaType(HTTPError):
base articles related to this error (default ``None``).
"""

def __init__(self, description=None, headers=None, **kwargs):
def __init__(self, title=None, description=None, headers=None, **kwargs):
super(HTTPUnsupportedMediaType, self).__init__(
status.HTTP_415, 'Unsupported media type',
status.HTTP_415, title,
description, headers, **kwargs)


Expand Down
2 changes: 1 addition & 1 deletion falcon/media/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def find_by_media_type(self, media_type, default):

if not resolved:
raise errors.HTTPUnsupportedMediaType(
'{0} is an unsupported media type.'.format(media_type)
description='{0} is an unsupported media type.'.format(media_type)
)

return self.data[resolved]
14 changes: 14 additions & 0 deletions tests/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ def test_http_not_acceptable_no_title_and_desc_and_challenges():
assert e.description is None


def test_http_not_acceptable_with_title():
try:
raise falcon.HTTPNotAcceptable(title='test title')
except falcon.HTTPNotAcceptable as e:
assert e.title == 'test title'


def test_http_not_acceptable_with_title_and_desc_and_challenges():
try:
raise falcon.HTTPNotAcceptable(description='Testdescription')
Expand All @@ -153,6 +160,13 @@ def test_http_unsupported_media_type_no_title_and_desc_and_challenges():
assert e.description is None


def test_http_unsupported_media_type_with_title():
try:
raise falcon.HTTPUnsupportedMediaType(title='test title')
except falcon.HTTPUnsupportedMediaType as e:
assert e.title == 'test title'


def test_http_unsupported_media_type_with_title_and_desc_and_challenges():
try:
raise falcon.HTTPUnsupportedMediaType(description='boom')
Expand Down
6 changes: 4 additions & 2 deletions tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ class RequireJSON:
def process_request(self, req, resp):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
title='406 Not Acceptable',
description='This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')

if req.method in ('POST', 'PUT'):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
title='415 Unsupported Media Type',
description='This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')


Expand Down
6 changes: 4 additions & 2 deletions tests/test_request_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ def test_unknown_media_type(media_type):
with pytest.raises(errors.HTTPUnsupportedMediaType) as err:
client.resource.captured_req.media

msg = '{} is an unsupported media type.'.format(media_type)
assert err.value.description == msg
title_msg = '415 Unsupported Media Type'
description_msg = '{} is an unsupported media type.'.format(media_type)
assert err.value.title == title_msg
assert err.value.description == description_msg


@pytest.mark.parametrize('media_type', [
Expand Down

0 comments on commit 3b8ca25

Please sign in to comment.