Code

Ticket #12522: 12522-tests.diff

File 12522-tests.diff, 2.9 KB (added by ramiro, 3 years ago)

Tests converted to unit tests

Line 
1diff --git a/tests/regressiontests/requests/tests.py b/tests/regressiontests/requests/tests.py
2--- a/tests/regressiontests/requests/tests.py
3+++ b/tests/regressiontests/requests/tests.py
4@@ -5,6 +5,7 @@
5 from django.core.handlers.modpython import ModPythonRequest
6 from django.core.handlers.wsgi import WSGIRequest, LimitedStream
7 from django.http import HttpRequest, HttpResponse, parse_cookie
8+from django.test.client import MULTIPART_CONTENT, BOUNDARY, FakePayload, encode_multipart
9 from django.utils import unittest
10 from django.utils.http import cookie_date
11 
12@@ -133,13 +134,21 @@
13         request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
14         self.assertEqual(request.read(), 'name=value')
15 
16-    def test_read_after_value(self):
17+    def test_read_after_POST(self):
18         """
19         Reading from request is allowed after accessing request contents as
20-        POST or raw_post_data.
21+        POST.
22         """
23         request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
24         self.assertEqual(request.POST, {u'name': [u'value']})
25+        self.assertEqual(request.read(), 'name=value')
26+
27+    def test_read_after_raw_post_data(self):
28+        """
29+        Reading from request is allowed after accessing request contents as
30+        raw_post_data.
31+        """
32+        request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
33         self.assertEqual(request.raw_post_data, 'name=value')
34         self.assertEqual(request.read(), 'name=value')
35 
36@@ -156,3 +165,29 @@
37     def test_read_by_lines(self):
38         request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')})
39         self.assertEqual(list(request), ['name=value'])
40+
41+    def test_POST_after_raw_post_data(self):
42+        post_data = encode_multipart(BOUNDARY,dict(testkey="testvalue"))
43+        env =  {
44+            'SERVER_PROTOCOL': 'HTTP/1.0',
45+            'REQUEST_METHOD': 'POST',
46+            'wsgi.input': FakePayload(post_data),
47+            'CONTENT_TYPE': MULTIPART_CONTENT,
48+            'CONTENT_LENGTH': len(post_data)
49+        }
50+        request = WSGIRequest(env)
51+        nop = request.raw_post_data  # Access the raw data
52+        self.assertEqual(request.POST['testkey'], 'testvalue') # POST still should be accesible
53+
54+    def test_raw_post_data_after_POST(self):
55+        post_data = encode_multipart(BOUNDARY,dict(testkey="testvalue"))
56+        env =  {
57+            'SERVER_PROTOCOL': 'HTTP/1.0',
58+            'REQUEST_METHOD': 'POST',
59+            'wsgi.input': FakePayload(post_data),
60+            'CONTENT_TYPE': MULTIPART_CONTENT,
61+            'CONTENT_LENGTH': len(post_data)
62+        }
63+        request = WSGIRequest(env)
64+        nop = request.POST['testkey'] # Access POST
65+        self.assertEqual(len(request.raw_post_data), len(post_data)) # raw_post_data still should be accesible