Skip to content

Commit

Permalink
Merge branch 'pr/192'
Browse files Browse the repository at this point in the history
Request.POST was destroying the original request body, thereby making
the following a destructive sequence:

r1 = req.body
r2 = req.POST
r3 = req.body

r1 would not be qual to r3, with this patchset that is once again true.
  • Loading branch information
digitalresistor committed Apr 13, 2015
2 parents 90052d9 + 7f3c67d commit 759cc3f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
26 changes: 21 additions & 5 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,21 @@ def test_POST_missing_content_type(self):
result = req.POST
self.assertEqual(result['var1'], 'value1')

def test_POST_json_no_content_type(self):
data = b'{"password": "last centurion", "email": "rory@wiggy.net"}'
INPUT = BytesIO(data)
environ = {'wsgi.input': INPUT,
'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH':len(data),
'webob.is_body_seekable': True,
}
req = self._makeOne(environ)
r_1 = req.body
r_2 = req.POST
r_3 = req.body
self.assertEqual(r_1, b'{"password": "last centurion", "email": "rory@wiggy.net"}')
self.assertEqual(r_3, b'{"password": "last centurion", "email": "rory@wiggy.net"}')

def test_PUT_bad_content_type(self):
from webob.multidict import NoVars
data = b'input'
Expand Down Expand Up @@ -2980,14 +2995,10 @@ def test_post_does_not_reparse(self):
content_type='multipart/form-data; boundary=boundary',
POST=_cgi_escaping_body
)
f0 = req.body_file_raw
post1 = req.POST
f1 = req.body_file_raw
self.assertTrue(f1 is not f0)
self.assertTrue('webob._parsed_post_vars' in req.environ)
post2 = req.POST
f2 = req.body_file_raw
self.assertTrue(post1 is post2)
self.assertTrue(f1 is f2)


def test_middleware_body(self):
Expand Down Expand Up @@ -3391,6 +3402,11 @@ def test_read_urlencoded(self):
'application/x-www-form-urlencoded')
self.assertEqual(body.read(), b'bananas=bananas')

def test_readable(self):
from webob.request import FakeCGIBody
body = FakeCGIBody({'bananas': 'bananas'}, 'application/something')
self.assertTrue(body.readable())


class Test_cgi_FieldStorage__repr__patch(unittest.TestCase):
def _callFUT(self, fake):
Expand Down
11 changes: 4 additions & 7 deletions webob/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,10 @@ def POST(self):
return NoVars('Not an HTML form submission (Content-Type: %s)'
% content_type)
self._check_charset()
if self.is_body_seekable:
self.body_file_raw.seek(0)

self.make_body_seekable()
self.body_file_raw.seek(0)

fs_environ = env.copy()
# FieldStorage assumes a missing CONTENT_LENGTH, but a
# default of 0 is better:
Expand All @@ -806,11 +808,6 @@ def POST(self):
keep_blank_values=True)
vars = MultiDict.from_fieldstorage(fs)


#ctype = self.content_type or 'application/x-www-form-urlencoded'
ctype = self._content_type_raw or 'application/x-www-form-urlencoded'
f = FakeCGIBody(vars, ctype)
self.body_file = io.BufferedReader(f)
env['webob._parsed_post_vars'] = (vars, self.body_file_raw)
return vars

Expand Down

0 comments on commit 759cc3f

Please sign in to comment.