Commit cfb3eb2e authored by Claude Paroz's avatar Claude Paroz
Browse files

Used FakePayload in requests tests

parent c2e19e26
Loading
Loading
Loading
Loading
+24 −23
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.core.handlers.wsgi import WSGIRequest, LimitedStream
from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr, UnreadablePostError
from django.test.client import FakePayload
from django.test.utils import str_prefix
from django.utils import unittest
from django.utils.http import cookie_date
@@ -328,11 +329,11 @@ class RequestsTests(unittest.TestCase):
        self.assertEqual(stream.read(), b'')

    def test_stream(self):
        payload = b'name=value'
        payload = FakePayload('name=value')
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        self.assertEqual(request.read(), b'name=value')

    def test_read_after_value(self):
@@ -340,11 +341,11 @@ class RequestsTests(unittest.TestCase):
        Reading from request is allowed after accessing request contents as
        POST or body.
        """
        payload = b'name=value'
        payload = FakePayload('name=value')
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        self.assertEqual(request.POST, {'name': ['value']})
        self.assertEqual(request.body, b'name=value')
        self.assertEqual(request.read(), b'name=value')
@@ -354,11 +355,11 @@ class RequestsTests(unittest.TestCase):
        Construction of POST or body is not allowed after reading
        from request.
        """
        payload = b'name=value'
        payload = FakePayload('name=value')
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        self.assertEqual(request.read(2), b'na')
        self.assertRaises(Exception, lambda: request.body)
        self.assertEqual(request.POST, {})
@@ -370,17 +371,17 @@ class RequestsTests(unittest.TestCase):
        # 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 body = '' either.
        payload = "\r\n".join([
        payload = FakePayload("\r\n".join([
                '--boundary',
                'Content-Disposition: form-data; name="name"',
                '',
                'value',
                '--boundary--'
                '']).encode('utf-8')
                '']))
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        self.assertEqual(request.POST, {'name': ['value']})
        self.assertRaises(Exception, lambda: request.body)

@@ -392,17 +393,17 @@ class RequestsTests(unittest.TestCase):
        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13
        # Every request.POST with Content-Length >= 0 is a valid request,
        # this test ensures that we handle Content-Length == 0.
        payload = "\r\n".join([
        payload = FakePayload("\r\n".join([
                '--boundary',
                'Content-Disposition: form-data; name="name"',
                '',
                'value',
                '--boundary--'
                '']).encode('utf-8')
                '']))
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
                               'CONTENT_LENGTH': 0,
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        self.assertEqual(request.POST, {})

    def test_POST_binary_only(self):
@@ -424,22 +425,22 @@ class RequestsTests(unittest.TestCase):
        self.assertEqual(request.body, payload)

    def test_read_by_lines(self):
        payload = b'name=value'
        payload = FakePayload('name=value')
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        self.assertEqual(list(request), [b'name=value'])

    def test_POST_after_body_read(self):
        """
        POST should be populated even if body is read first
        """
        payload = b'name=value'
        payload = FakePayload('name=value')
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        raw_data = request.body
        self.assertEqual(request.POST, {'name': ['value']})

@@ -448,11 +449,11 @@ class RequestsTests(unittest.TestCase):
        POST should be populated even if body is read first, and then
        the stream is read second.
        """
        payload = b'name=value'
        payload = FakePayload('name=value')
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'application/x-www-form-urlencoded',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        raw_data = request.body
        self.assertEqual(request.read(1), b'n')
        self.assertEqual(request.POST, {'name': ['value']})
@@ -462,17 +463,17 @@ class RequestsTests(unittest.TestCase):
        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([
        payload = FakePayload("\r\n".join([
                '--boundary',
                'Content-Disposition: form-data; name="name"',
                '',
                'value',
                '--boundary--'
                '']).encode('utf-8')
                '']))
        request = WSGIRequest({'REQUEST_METHOD': 'POST',
                               'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
                               'CONTENT_LENGTH': len(payload),
                               'wsgi.input': BytesIO(payload)})
                               'wsgi.input': payload})
        raw_data = request.body
        # Consume enough data to mess up the parsing:
        self.assertEqual(request.read(13), b'--boundary\r\nC')
@@ -482,11 +483,11 @@ class RequestsTests(unittest.TestCase):
        """
        HttpRequest.raw_post_body should be the same as HttpRequest.body
        """
        payload = b'Hello There!'
        payload = FakePayload('Hello There!')
        request = WSGIRequest({
            'REQUEST_METHOD': 'POST',
            'CONTENT_LENGTH': len(payload),
            'wsgi.input': BytesIO(payload)
            'wsgi.input': payload,
        })

        with warnings.catch_warnings(record=True):