Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Used FakePayload in requests tests

  • Loading branch information...
commit cfb3eb2e3d1af6d4f6ba852f50f57865e34ece38 1 parent c2e19e2
Claude Paroz claudep authored
Showing with 24 additions and 23 deletions.
  1. +24 −23 tests/regressiontests/requests/tests.py
47 tests/regressiontests/requests/tests.py
View
@@ -9,6 +9,7 @@
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 @@ def test_limited_stream(self):
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 @@ def test_read_after_value(self):
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 @@ def test_value_after_read(self):
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 @@ def test_body_after_POST_multipart(self):
# 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 @@ def test_POST_multipart_with_content_length_zero(self):
# 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 @@ def test_POST_binary_only(self):
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 @@ def test_POST_after_body_read_and_stream_read(self):
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 @@ def test_POST_after_body_read_and_stream_read_multipart(self):
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 @@ def test_raw_post_data_returns_body(self):
"""
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):
Please sign in to comment.
Something went wrong with that request. Please try again.