Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additional test for 100% coverage of pull request 85 #97

Closed
wants to merge 8 commits into from
27 changes: 20 additions & 7 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,17 +829,28 @@ def test_blank__str_post_data_for_unsupported_ctype(self):
'/', content_type='application/json', POST={})

def test_blank__post_urlencoded(self):
request = self._blankOne('/', POST={'first':1, 'second':2})
from webob.multidict import MultiDict
POST = MultiDict()
POST["first"] = 1
POST["second"] = 2

request = self._blankOne('/', POST=POST)
self.assertEqual(request.method, 'POST')
self.assertEqual(request.content_type,
'application/x-www-form-urlencoded')
self.assertEqual(request.body, b'first=1&second=2')
self.assertEqual(request.content_length, 16)

def test_blank__post_multipart(self):
request = self._blankOne(
'/', POST={'first':'1', 'second':'2'},
content_type='multipart/form-data; boundary=boundary')
from webob.multidict import MultiDict
POST = MultiDict()
POST["first"] = "1"
POST["second"] = "2"


request = self._blankOne('/',
POST=POST,
content_type='multipart/form-data; boundary=boundary')
self.assertEqual(request.method, 'POST')
self.assertEqual(request.content_type, 'multipart/form-data')
expected = (
Expand All @@ -856,9 +867,11 @@ def test_blank__post_multipart(self):
def test_blank__post_files(self):
import cgi
from webob.request import _get_multipart_boundary
POST = {'first':('filename1', BytesIO(b'1')),
'second':('filename2', '2'),
'third': '3'}
from webob.multidict import MultiDict
POST = MultiDict()
POST["first"] = ('filename1', BytesIO(b'1'))
POST["second"] = ('filename2', '2')
POST["third"] = "3"
request = self._blankOne('/', POST=POST)
self.assertEqual(request.method, 'POST')
self.assertEqual(request.content_type, 'multipart/form-data')
Expand Down
14 changes: 12 additions & 2 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,6 @@ def test_encode_content_gzip_notyet_gzipped():
eq_(res.content_length, 23)
eq_(res.app_iter, [
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff',
b'',
b'K\xcb\xcf\x07\x00',
b'!es\x8c\x03\x00\x00\x00'
])
Expand All @@ -954,11 +953,22 @@ def test_encode_content_gzip_notyet_gzipped_lazy():
eq_(res.content_length, None)
eq_(list(res.app_iter), [
b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff',
b'',
b'K\xcb\xcf\x07\x00',
b'!es\x8c\x03\x00\x00\x00'
])

def test_encode_content_gzip_buffer_coverage():
"""this test is to provide 100% coverage of
response.Response.encode_content was necessary in order to get
pull request https://github.com/Pylons/webob/pull/85 into upstream
"""
res = Response()
DATA = b"abcdefghijklmnopqrstuvwxyz0123456789" * 1000000
res.app_iter = io.BytesIO(DATA)
res.encode_content('gzip')
result = list(res.app_iter)
assert len("".join(result)) < len(DATA)

def test_decode_content_identity():
res = Response()
res.content_encoding = 'identity'
Expand Down
5 changes: 2 additions & 3 deletions webob/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,8 +1455,7 @@ def environ_add_POST(env, data, content_type=None):
env['REQUEST_METHOD'] = 'POST'
has_files = False
if hasattr(data, 'items'):
data = sorted(data.items())
for k, v in data:
for k, v in data.items():
if isinstance(v, (tuple, list)):
has_files = True
break
Expand All @@ -1467,7 +1466,7 @@ def environ_add_POST(env, data, content_type=None):
content_type = 'application/x-www-form-urlencoded'
if content_type.startswith('multipart/form-data'):
if not isinstance(data, bytes):
content_type, data = _encode_multipart(data, content_type)
content_type, data = _encode_multipart(data.items(), content_type)
elif content_type.startswith('application/x-www-form-urlencoded'):
if has_files:
raise ValueError('Submiting files is not allowed for'
Expand Down
14 changes: 12 additions & 2 deletions webob/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,8 +1277,18 @@ def gzip_app_iter(app_iter):
for item in app_iter:
size += len(item)
crc = zlib.crc32(item, crc) & 0xffffffff
yield compress.compress(item)
yield compress.flush()

# The compress function may return zero length bytes if the input is
# small enough; it buffers the input for the next iteration or for a
# flush.
result = compress.compress(item)
if result:
yield result

# Similarly, flush may also not yield a value.
result = compress.flush()
if result:
yield result
yield struct.pack("<2L", crc, size & 0xffffffff)

def _error_unicode_in_app_iter(app_iter, body):
Expand Down