summaryrefslogtreecommitdiff
path: root/django/test/client.py
diff options
context:
space:
mode:
authorBrian Rosner <brosner@gmail.com>2008-07-01 15:49:08 +0000
committerBrian Rosner <brosner@gmail.com>2008-07-01 15:49:08 +0000
commit0e8710d5900a75b9a4a1caebb82c939896e99cff (patch)
treef3db8fb3f6b932bfc48526a70c332efce66d5cac /django/test/client.py
parent595e9191f519af9b1c0c4b657fd3923c0997938c (diff)
newforms-admin: Merged from trunk up to [7814].
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7815 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/test/client.py')
-rw-r--r--django/test/client.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/django/test/client.py b/django/test/client.py
index a15876e6f9..6313181d61 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -1,7 +1,10 @@
import urllib
import sys
import os
-from cStringIO import StringIO
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
from django.conf import settings
from django.contrib.auth import authenticate, login
from django.core.handlers.base import BaseHandler
@@ -19,6 +22,25 @@ from django.utils.itercompat import is_iterable
BOUNDARY = 'BoUnDaRyStRiNg'
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
+class FakePayload(object):
+ """
+ A wrapper around StringIO that restricts what can be read since data from
+ the network can't be seeked and cannot be read outside of its content
+ length. This makes sure that views can't do anything under the test client
+ that wouldn't work in Real Life.
+ """
+ def __init__(self, content):
+ self.__content = StringIO(content)
+ self.__len = len(content)
+
+ def read(self, num_bytes=None):
+ if num_bytes is None:
+ num_bytes = self.__len or 1
+ assert self.__len >= num_bytes, "Cannot read more than the available bytes from the HTTP incoming data."
+ content = self.__content.read(num_bytes)
+ self.__len -= num_bytes
+ return content
+
class ClientHandler(BaseHandler):
"""
A HTTP Handler that can be used for testing purposes.
@@ -236,7 +258,7 @@ class Client:
'CONTENT_TYPE': content_type,
'PATH_INFO': urllib.unquote(path),
'REQUEST_METHOD': 'POST',
- 'wsgi.input': StringIO(post_data),
+ 'wsgi.input': FakePayload(post_data),
}
r.update(extra)