summaryrefslogtreecommitdiff
path: root/tests/test_request_nose.py
diff options
context:
space:
mode:
authorSergey Schetinin <sergey@maluke.com>2011-03-22 02:24:57 +0200
committerSergey Schetinin <sergey@maluke.com>2011-03-22 02:24:57 +0200
commitadf2fa1072585f2b6f6177ff03e38cf484894836 (patch)
tree500152ef301f2d1955c5940d3cca703e7e50696b /tests/test_request_nose.py
parent3aedd9cec4aac075705d300535f861287ed73b4f (diff)
downloadwebob-adf2fa1072585f2b6f6177ff03e38cf484894836.tar.gz
Add req.is_body_readable / env['webob.is_body_readable'] flag and only read wsgi.input if either CONTENT_LENGTH is present or that flag is set.
This partially reverts the change made in 1.0.2 in how the missing CONTENT_LENGTH is interpreted -- in 1.0.1 and before the body would be assumed to be empty, in 1.0.2-1.0.5 webob would attempt to read it. This restores compatibility with some servers (CherryPy 3.2, wsgiref and possibly more). Note that with this new flag we can support FakeCGIBody and chunked input streams without the env['CONTENT_LENGTH'] = '-1' hack. For related discussion see https://bitbucket.org/ianb/webob/issue/6
Diffstat (limited to 'tests/test_request_nose.py')
-rw-r--r--tests/test_request_nose.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/test_request_nose.py b/tests/test_request_nose.py
new file mode 100644
index 0000000..172c1de
--- /dev/null
+++ b/tests/test_request_nose.py
@@ -0,0 +1,54 @@
+from webob import Request
+
+
+def test_request_read_no_content_length():
+ req, input = _make_read_tracked_request('abc')
+ assert req.content_length is None
+ assert req.body == ''
+ assert not input.was_read
+
+def test_request_read_no_flag_but_content_length_is_present():
+ req, input = _make_read_tracked_request('abc')
+ req.content_length = 3
+ assert req.body == 'abc'
+ assert input.was_read
+
+def test_request_read_no_content_length_but_flagged_readable():
+ req, input = _make_read_tracked_request('abc')
+ req.is_body_readable = True
+ assert req.body == 'abc'
+ assert input.was_read
+
+def test_request_read_after_setting_body_file():
+ req = _make_read_tracked_request()[0]
+ input = req.body_file = ReadTracker('abc')
+ assert req.content_length is None
+ assert not req.is_body_seekable
+ assert req.body == 'abc'
+ # reading body made the input seekable and set the clen
+ assert req.content_length == 3
+ assert req.is_body_seekable
+ assert input.was_read
+
+
+def _make_read_tracked_request(data=''):
+ input = ReadTracker(data)
+ env = {
+ 'HTTP_METHOD': 'PUT',
+ 'wsgi.input': input,
+ }
+ return Request(env), input
+
+class ReadTracker(object):
+ """
+ Helper object to determine if the input was read or not
+ """
+ def __init__(self, data):
+ self.data = data
+ self.was_read = False
+ def read(self, size=-1):
+ if size < 0:
+ size = len(self.data)
+ assert size == len(self.data)
+ self.was_read = True
+ return self.data