Skip to content

Commit

Permalink
when creating Response.body from app_iter handle content_length w/ mo…
Browse files Browse the repository at this point in the history
…re case
  • Loading branch information
maluke committed Nov 8, 2009
1 parent cdcd023 commit 721d27d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
34 changes: 33 additions & 1 deletion tests/test_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from StringIO import StringIO
from nose.tools import eq_, ok_
from nose.tools import eq_, ok_, assert_raises
from webob import *

def simple_app(environ, start_response):
Expand Down Expand Up @@ -48,6 +48,38 @@ def test_HEAD_closes():
eq_(res.body, '')
ok_(app_iter.closed)

def test_content_length():
r0 = Response('x'*10, content_length=10)

req_head = Request.blank('/', method='HEAD')
r1 = req_head.get_response(r0)
eq_(r1.status_int, 200)
eq_(r1.body, '')
eq_(r1.content_length, 10)

req_get = Request.blank('/')
r2 = req_get.get_response(r0)
eq_(r2.status_int, 200)
eq_(r2.body, 'x'*10)
eq_(r2.content_length, 10)

r3 = Response(app_iter=['x']*10)
eq_(r3.content_length, None)
eq_(r3.body, 'x'*10)
eq_(r3.content_length, 10)

r4 = Response(app_iter=['x']*10, content_length=20) # wrong content_length
eq_(r4.content_length, 20)
assert_raises(AssertionError, lambda: r4.body)

req_range = Request.blank('/', range=(0,5))
r0.conditional_response = True
r5 = req_range.get_response(r0)
eq_(r5.status_int, 206)
eq_(r5.body, 'xxxxx')
eq_(r5.content_length, 5)


def test_app_iter_range():
req = Request.blank('/', range=(2,5))
for app_iter in [
Expand Down
24 changes: 17 additions & 7 deletions webob/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ def _content_type_params__del(self, value):
self.headers['Content-Type'] = self.headers.get('Content-Type', '').split(';', 1)[0]

content_type_params = property(
_content_type_params__get,
_content_type_params__set,
_content_type_params__del,
_content_type_params__get,
_content_type_params__set,
_content_type_params__del,
doc=_content_type_params__get.__doc__
)

Expand All @@ -321,15 +321,25 @@ def _body__get(self):
"""
if self._body is None:
if self._app_iter is None:
raise AttributeError(
"No body has been set")
raise AttributeError("No body has been set")
try:
self._body = ''.join(self._app_iter)
body = self._body = ''.join(self._app_iter)
finally:
if hasattr(self._app_iter, 'close'):
self._app_iter.close()
self._app_iter = None
self.content_length = len(self._body)
if self._request is not None and self._request.method == 'HEAD':
assert len(body) == 0, "HEAD responses must be empty"
elif len(body) == 0:
# if body-length is zero, we assume it's a HEAD response and leave content_lenght alone
pass
elif self.content_length is None:
self.content_length = len(body)
elif self.content_length != len(body):
raise AssertionError(
"Content-Length is different from actual app_iter length (%r!=%r)"
% (self.content_length, len(body))
)
return self._body

def _body__set(self, value):
Expand Down

0 comments on commit 721d27d

Please sign in to comment.