Skip to content

Commit

Permalink
Merge branch 'pr/100': Delete body if response code is 204,205 or 304
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalresistor committed Apr 14, 2015
2 parents 68ec029 + 56a8fd5 commit 88b21f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ def test_set_response_status_str_generic_reason():
assert res.status_code == 299
assert res.status == '299 Success'

def test_set_response_status_204():
# setting the response status to 204 should remove the content_type
req = BaseRequest.blank('/')
res = req.get_response(simple_app)
res.status = 204
assert res.status_code == 204
assert res.status == '204 No Content'
assert res.content_type is None
assert res.content_length is None

def test_set_response_status_code():
req = BaseRequest.blank('/')
res = req.get_response(simple_app)
Expand Down
6 changes: 6 additions & 0 deletions webob/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ def _status_code__set(self, code):
self._status = '%d %s' % (code, status_reasons[code])
except KeyError:
self._status = '%d %s' % (code, status_generic_reasons[code // 100])
# responses with status == 204 must not include a message body,
# so probably should not have a Content-Type header as well.
# this mimics the "empty_body = True" behavior from webob.exc
if code in [204, 205, 304]:
del self.content_type
del self.content_length

status_code = status_int = property(_status_code__get, _status_code__set,
doc=_status_code__get.__doc__)
Expand Down

0 comments on commit 88b21f7

Please sign in to comment.