Skip to content

Commit

Permalink
Merge pull request #522 from Cornices/521-fix-reponse-content-204
Browse files Browse the repository at this point in the history
Do not return content if response is ``204 No Content`` (fixes #521)
  • Loading branch information
leplatrem committed Nov 13, 2019
2 parents bc127f0 + 23dca84 commit a7a1bea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
3 changes: 1 addition & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ CHANGELOG
3.7.0 (unreleased)
==================

- Nothing changed yet.

- Do not return content if response is ``204 No Content`` (fixes #521)

3.6.0 (2019-07-24)
==================
Expand Down
5 changes: 5 additions & 0 deletions cornice/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def __call__(self, data, context):
response = request.response
registry = request.registry

# Do not return content with ``204 No Content``
if response.status_code == 204:
response.content_type = None
return ""

# Serialise the ``data`` object to a JSON string using the
# JSON renderer registered with Pyramid.
renderer_factory = registry.queryUtility(IRendererFactory, name='json')
Expand Down
32 changes: 20 additions & 12 deletions tests/test_pyramidhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ def update_view(request):
return "updated_view"


@service.delete(permission='write')
@service.patch(permission='write')
def return_yay(request):
return "yay"

@service.delete()
def delete_view(request):
request.response.status = 204


class TemperatureCooler(object):
def __init__(self, request, context=None):
Expand Down Expand Up @@ -119,32 +123,36 @@ def test_405(self):
# calling a unknown verb on an existing resource should return a 405
self.app.post("/service", status=HTTPMethodNotAllowed.code)

def test_acl_support_unauthenticated_service_delete(self):
def test_204(self):
resp = self.app.delete("/service", status=204)
self.assertNotIn("Content-Type", resp.headers)

def test_acl_support_unauthenticated_service_patch(self):
# calling a view with permissions without an auth'd user => 403
self.app.delete('/service', status=HTTPForbidden.code)
self.app.patch('/service', status=HTTPForbidden.code)

def test_acl_support_authenticated_allowed_service_delete(self):
def test_acl_support_authenticated_allowed_service_patch(self):
with mock.patch.object(self.authn_policy, 'unauthenticated_userid',
return_value='bob'):
result = self.app.delete('/service', status=HTTPOk.code)
result = self.app.patch('/service', status=HTTPOk.code)
self.assertEqual("yay", result.json)
# The other user with 'write' permission
with mock.patch.object(self.authn_policy, 'unauthenticated_userid',
return_value='dan'):
result = self.app.delete('/service', status=HTTPOk.code)
result = self.app.patch('/service', status=HTTPOk.code)
self.assertEqual("yay", result.json)

def test_acl_support_authenticated_valid_user_wrong_permission_service_delete(self):
def test_acl_support_authenticated_valid_user_wrong_permission_service_patch(self):
with mock.patch.object(self.authn_policy, 'unauthenticated_userid', return_value='alice'):
self.app.delete('/service', status=HTTPForbidden.code)
self.app.patch('/service', status=HTTPForbidden.code)

def test_acl_support_authenticated_valid_user_permission_denied_service_delete(self):
def test_acl_support_authenticated_valid_user_permission_denied_service_patch(self):
with mock.patch.object(self.authn_policy, 'unauthenticated_userid', return_value='carol'):
self.app.delete('/service', status=HTTPForbidden.code)
self.app.patch('/service', status=HTTPForbidden.code)

def test_acl_support_authenticated_invalid_user_service_delete(self):
def test_acl_support_authenticated_invalid_user_service_patch(self):
with mock.patch.object(self.authn_policy, 'unauthenticated_userid', return_value='mallory'):
self.app.delete('/service', status=HTTPForbidden.code)
self.app.patch('/service', status=HTTPForbidden.code)

def test_acl_support_authenticated_allowed_service_put(self):
with mock.patch.object(self.authn_policy, 'unauthenticated_userid', return_value='dan'):
Expand Down

0 comments on commit a7a1bea

Please sign in to comment.