summaryrefslogtreecommitdiff
path: root/tests/regressiontests
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2011-12-16 23:40:32 +0000
committerAdrian Holovaty <adrian@holovaty.com>2011-12-16 23:40:32 +0000
commit3f003a3c4b5338915e3889da8bf10577296459b3 (patch)
treebca5e19c862746c69057e4819e754254d31e5a17 /tests/regressiontests
parent61f0aff811aa596fa62136852c59d47f988d1185 (diff)
Fixed #17323 -- Renamed HttpRequest.raw_post_data to request.body. Thanks for the patch, dstufft
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17210 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
-rw-r--r--tests/regressiontests/requests/tests.py52
-rw-r--r--tests/regressiontests/test_client_regress/models.py9
-rw-r--r--tests/regressiontests/test_client_regress/urls.py2
-rw-r--r--tests/regressiontests/test_client_regress/views.py10
4 files changed, 47 insertions, 26 deletions
diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
index e96f3129be..7927d27098 100644
--- a/tests/regressiontests/requests/tests.py
+++ b/tests/regressiontests/requests/tests.py
@@ -1,4 +1,5 @@
import time
+import warnings
from datetime import datetime, timedelta
from StringIO import StringIO
@@ -6,6 +7,7 @@ from django.conf import settings
from django.core.handlers.modpython import ModPythonRequest
from django.core.handlers.wsgi import WSGIRequest, LimitedStream
from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr
+from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils import unittest
from django.utils.http import cookie_date
@@ -294,19 +296,19 @@ class RequestsTests(unittest.TestCase):
def test_read_after_value(self):
"""
Reading from request is allowed after accessing request contents as
- POST or raw_post_data.
+ POST or body.
"""
payload = 'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)})
self.assertEqual(request.POST, {u'name': [u'value']})
- self.assertEqual(request.raw_post_data, 'name=value')
+ self.assertEqual(request.body, 'name=value')
self.assertEqual(request.read(), 'name=value')
def test_value_after_read(self):
"""
- Construction of POST or raw_post_data is not allowed after reading
+ Construction of POST or body is not allowed after reading
from request.
"""
payload = 'name=value'
@@ -314,16 +316,16 @@ class RequestsTests(unittest.TestCase):
'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)})
self.assertEqual(request.read(2), 'na')
- self.assertRaises(Exception, lambda: request.raw_post_data)
+ self.assertRaises(Exception, lambda: request.body)
self.assertEqual(request.POST, {})
- def test_raw_post_data_after_POST_multipart(self):
+ def test_body_after_POST_multipart(self):
"""
- Reading raw_post_data after parsing multipart is not allowed
+ Reading body 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.
+ # silence the error by setting body = '' either.
payload = "\r\n".join([
'--boundary',
'Content-Disposition: form-data; name="name"',
@@ -336,7 +338,7 @@ class RequestsTests(unittest.TestCase):
'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)})
self.assertEqual(request.POST, {u'name': [u'value']})
- self.assertRaises(Exception, lambda: request.raw_post_data)
+ self.assertRaises(Exception, lambda: request.body)
def test_POST_multipart_with_content_length_zero(self):
"""
@@ -366,33 +368,33 @@ class RequestsTests(unittest.TestCase):
'wsgi.input': StringIO(payload)})
self.assertEqual(list(request), ['name=value'])
- def test_POST_after_raw_post_data_read(self):
+ def test_POST_after_body_read(self):
"""
- POST should be populated even if raw_post_data is read first
+ POST should be populated even if body is read first
"""
payload = 'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)})
- raw_data = request.raw_post_data
+ raw_data = request.body
self.assertEqual(request.POST, {u'name': [u'value']})
- def test_POST_after_raw_post_data_read_and_stream_read(self):
+ def test_POST_after_body_read_and_stream_read(self):
"""
- POST should be populated even if raw_post_data is read first, and then
+ POST should be populated even if body is read first, and then
the stream is read second.
"""
payload = 'name=value'
request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)})
- raw_data = request.raw_post_data
+ raw_data = request.body
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):
+ def test_POST_after_body_read_and_stream_read_multipart(self):
"""
- POST should be populated even if raw_post_data is read first, and then
+ POST should be populated even if body is read first, and then
the stream is read second. Using multipart/form-data instead of urlencoded.
"""
payload = "\r\n".join([
@@ -406,7 +408,23 @@ class RequestsTests(unittest.TestCase):
'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
'CONTENT_LENGTH': len(payload),
'wsgi.input': StringIO(payload)})
- raw_data = request.raw_post_data
+ raw_data = request.body
# 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']})
+
+ def test_raw_post_data_returns_body(self):
+ """
+ HttpRequest.raw_post_body should be the same as HttpRequest.body
+ """
+ payload = 'Hello There!'
+ request = WSGIRequest({
+ 'REQUEST_METHOD': 'POST',
+ 'CONTENT_LENGTH': len(payload),
+ 'wsgi.input': StringIO(payload)
+ })
+
+ warnings_state = get_warnings_state()
+ warnings.filterwarnings('ignore', category=DeprecationWarning, module='django.http')
+ self.assertEqual(request.body, request.raw_post_data)
+ restore_warnings_state(warnings_state)
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
index 7d0b0e412f..4ddd957055 100644
--- a/tests/regressiontests/test_client_regress/models.py
+++ b/tests/regressiontests/test_client_regress/models.py
@@ -975,15 +975,16 @@ class ResponseTemplateDeprecationTests(TestCase):
class ReadLimitedStreamTest(TestCase):
"""
- Tests that ensure that HttpRequest.raw_post_data, HttpRequest.read() and
+ Tests that ensure that HttpRequest.body, HttpRequest.read() and
HttpRequest.read(BUFFER) have proper LimitedStream behaviour.
Refs #14753, #15785
"""
- def test_raw_post_data_from_empty_request(self):
- """HttpRequest.raw_post_data on a test client GET request should return
+
+ def test_body_from_empty_request(self):
+ """HttpRequest.body on a test client GET request should return
the empty string."""
- self.assertEquals(self.client.get("/test_client_regress/raw_post_data/").content, '')
+ self.assertEquals(self.client.get("/test_client_regress/body/").content, '')
def test_read_from_empty_request(self):
"""HttpRequest.read() on a test client GET request should return the
diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py
index 93f7a2e2f2..d869c234eb 100644
--- a/tests/regressiontests/test_client_regress/urls.py
+++ b/tests/regressiontests/test_client_regress/urls.py
@@ -31,7 +31,7 @@ urlpatterns = patterns('',
(r'^parse_unicode_json/$', views.return_json_file),
(r'^check_headers/$', views.check_headers),
(r'^check_headers_redirect/$', RedirectView.as_view(url='/test_client_regress/check_headers/')),
- (r'^raw_post_data/$', views.raw_post_data),
+ (r'^body/$', views.body),
(r'^read_all/$', views.read_all),
(r'^read_buffer/$', views.read_buffer),
(r'^request_context_view/$', views.request_context_view),
diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py
index b3982938b7..ebb68c4c82 100644
--- a/tests/regressiontests/test_client_regress/views.py
+++ b/tests/regressiontests/test_client_regress/views.py
@@ -1,3 +1,5 @@
+import warnings
+
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
@@ -79,7 +81,7 @@ def return_json_file(request):
charset = settings.DEFAULT_CHARSET
# This just checks that the uploaded data is JSON
- obj_dict = simplejson.loads(request.raw_post_data.decode(charset))
+ obj_dict = simplejson.loads(request.body.decode(charset))
obj_json = simplejson.dumps(obj_dict, encoding=charset,
cls=DjangoJSONEncoder,
ensure_ascii=False)
@@ -92,9 +94,9 @@ def check_headers(request):
"A view that responds with value of the X-ARG-CHECK header"
return HttpResponse('HTTP_X_ARG_CHECK: %s' % request.META.get('HTTP_X_ARG_CHECK', 'Undefined'))
-def raw_post_data(request):
- "A view that is requested with GET and accesses request.raw_post_data. Refs #14753."
- return HttpResponse(request.raw_post_data)
+def body(request):
+ "A view that is requested with GET and accesses request.body. Refs #14753."
+ return HttpResponse(request.body)
def read_all(request):
"A view that is requested with accesses request.read()."