Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Allow override 405 response error message
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Sep 30, 2015
1 parent 19081bd commit e815648
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 14 additions & 1 deletion cliquet/tests/test_views_errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mock
from pyramid import httpexceptions

from cliquet.errors import ERRORS
from cliquet.errors import ERRORS, http_error

from .support import BaseWebTest, unittest, authorize, FormattedErrorMixin

Expand Down Expand Up @@ -69,6 +69,19 @@ def test_405_is_valid_formatted_error(self):
response, 405, ERRORS.METHOD_NOT_ALLOWED, "Method Not Allowed",
"Method not allowed on this endpoint.")

def test_405_can_have_custom_message(self):
custom_405 = http_error(httpexceptions.HTTPMethodNotAllowed(),
errno=ERRORS.METHOD_NOT_ALLOWED,
message="Disabled from conf.")
with mock.patch(
'cliquet.tests.testapp.views.Mushroom._extract_filters',
side_effect=custom_405):
response = self.app.get(self.sample_url,
headers=self.headers, status=405)
self.assertFormattedError(
response, 405, ERRORS.METHOD_NOT_ALLOWED, "Method Not Allowed",
"Disabled from conf.")

def test_500_is_valid_formatted_error(self):
with mock.patch(
'cliquet.tests.testapp.views.Mushroom._extract_filters',
Expand Down
6 changes: 4 additions & 2 deletions cliquet/views/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def page_not_found(request):
@view_config(context=httpexceptions.HTTPServiceUnavailable,
permission=NO_PERMISSION_REQUIRED)
def service_unavailable(context, request):

error_msg = "Service unavailable due to high load, please retry later."
response = http_error(httpexceptions.HTTPServiceUnavailable(),
errno=ERRORS.BACKEND,
Expand All @@ -82,7 +81,10 @@ def service_unavailable(context, request):
@view_config(context=httpexceptions.HTTPMethodNotAllowed,
permission=NO_PERMISSION_REQUIRED)
def method_not_allowed(context, request):
response = http_error(httpexceptions.HTTPMethodNotAllowed(),
if context.content_type == 'application/json':
return context

response = http_error(context,
errno=ERRORS.METHOD_NOT_ALLOWED,
message="Method not allowed on this endpoint.")
return reapply_cors(request, response)
Expand Down

0 comments on commit e815648

Please sign in to comment.