summaryrefslogtreecommitdiff
path: root/tests/regressiontests/requests/tests.py
diff options
context:
space:
mode:
authorLuke Plant <L.Plant.98@cantab.net>2011-03-28 16:11:40 +0000
committerLuke Plant <L.Plant.98@cantab.net>2011-03-28 16:11:40 +0000
commit8bb46d8b7c808bb366c7e15c46e5476e2b2b51a1 (patch)
tree9d9235a1701f8bb08fbbc815ec538632b149d43f /tests/regressiontests/requests/tests.py
parent728770a5f9bbdcd4131f3a2552666aef54764aba (diff)
Fixed #15679 - regression in HttpRequest.POST and raw_post_data access.
Thanks to vkryachko for the report. This also fixes a slight inconsistency with raw_post_data after parsing of a multipart request, and adds a test for that. (Previously accessing raw_post_data would have returned the empty string rather than raising an Exception). git-svn-id: http://code.djangoproject.com/svn/django/trunk@15938 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/requests/tests.py')
-rw-r--r--tests/regressiontests/requests/tests.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
index a80ee9d571..b68201494b 100644
--- a/tests/regressiontests/requests/tests.py
+++ b/tests/regressiontests/requests/tests.py
@@ -179,6 +179,66 @@ class RequestsTests(unittest.TestCase):
self.assertRaises(Exception, lambda: request.raw_post_data)
self.assertEqual(request.POST, {})
+ def test_raw_post_data_after_POST_multipart(self):
+ """
+ Reading raw_post_data after parsing multipart is not allowed
+ """
+ # Because multipart is used for large amounts fo data i.e. file uploads,
+ # we don't want the data held in memory twice, and we don't want to
+ # silence the error by setting raw_post_data = '' either.
+ payload = "\r\n".join([
+ '--boundary',
+ 'Content-Disposition: form-data; name="name"',
+ '',
+ 'value',
+ '--boundary--'
+ ''])
+ request = WSGIRequest({'REQUEST_METHOD': 'POST',
+ 'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
+ 'CONTENT_LENGTH': len(payload),
+ 'wsgi.input': StringIO(payload)})
+ self.assertEqual(request.POST, {u'name': [u'value']})
+ self.assertRaises(Exception, lambda: request.raw_post_data)
+
def test_read_by_lines(self):
request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
self.assertEqual(list(request), ['name=value'])
+
+ def test_POST_after_raw_post_data_read(self):
+ """
+ POST should be populated even if raw_post_data is read first
+ """
+ request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
+ raw_data = request.raw_post_data
+ self.assertEqual(request.POST, {u'name': [u'value']})
+
+ def test_POST_after_raw_post_data_read_and_stream_read(self):
+ """
+ POST should be populated even if raw_post_data is read first, and then
+ the stream is read second.
+ """
+ request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
+ raw_data = request.raw_post_data
+ self.assertEqual(request.read(1), u'n')
+ self.assertEqual(request.POST, {u'name': [u'value']})
+
+ def test_POST_after_raw_post_data_read_and_stream_read_multipart(self):
+ """
+ POST should be populated even if raw_post_data is read first, and then
+ the stream is read second. Using multipart/form-data instead of urlencoded.
+ """
+ payload = "\r\n".join([
+ '--boundary',
+ 'Content-Disposition: form-data; name="name"',
+ '',
+ 'value',
+ '--boundary--'
+ ''])
+ request = WSGIRequest({'REQUEST_METHOD': 'POST',
+ 'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
+ 'CONTENT_LENGTH': len(payload),
+ 'wsgi.input': StringIO(payload)})
+ raw_data = request.raw_post_data
+ # Consume enough data to mess up the parsing:
+ self.assertEqual(request.read(13), u'--boundary\r\nC')
+ self.assertEqual(request.POST, {u'name': [u'value']})