2008-03-20 14:50:54 +08:00
|
|
|
import urllib
|
2007-02-11 08:23:31 +08:00
|
|
|
import sys
|
2006-08-27 20:24:59 +08:00
|
|
|
from cStringIO import StringIO
|
2007-02-09 21:47:36 +08:00
|
|
|
from django.conf import settings
|
2007-05-05 23:16:15 +08:00
|
|
|
from django.contrib.auth import authenticate, login
|
2006-08-27 20:24:59 +08:00
|
|
|
from django.core.handlers.base import BaseHandler
|
|
|
|
from django.core.handlers.wsgi import WSGIRequest
|
2007-02-11 08:23:31 +08:00
|
|
|
from django.core.signals import got_request_exception
|
2006-08-27 20:24:59 +08:00
|
|
|
from django.dispatch import dispatcher
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.http import SimpleCookie, HttpRequest
|
2007-08-28 21:03:22 +08:00
|
|
|
from django.template import TemplateDoesNotExist
|
2006-09-02 17:26:24 +08:00
|
|
|
from django.test import signals
|
2006-08-27 20:24:59 +08:00
|
|
|
from django.utils.functional import curry
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.encoding import smart_str
|
|
|
|
from django.utils.http import urlencode
|
2007-09-15 03:55:24 +08:00
|
|
|
from django.utils.itercompat import is_iterable
|
2006-08-27 20:24:59 +08:00
|
|
|
|
2007-02-17 08:23:09 +08:00
|
|
|
BOUNDARY = 'BoUnDaRyStRiNg'
|
|
|
|
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
class ClientHandler(BaseHandler):
|
|
|
|
"""
|
2006-09-28 09:56:02 +08:00
|
|
|
A HTTP Handler that can be used for testing purposes.
|
2006-08-27 20:24:59 +08:00
|
|
|
Uses the WSGI interface to compose requests, but returns
|
|
|
|
the raw HttpResponse object
|
|
|
|
"""
|
|
|
|
def __call__(self, environ):
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core import signals
|
|
|
|
|
|
|
|
# Set up middleware if needed. We couldn't do this earlier, because
|
|
|
|
# settings weren't available.
|
|
|
|
if self._request_middleware is None:
|
|
|
|
self.load_middleware()
|
|
|
|
|
|
|
|
dispatcher.send(signal=signals.request_started)
|
|
|
|
try:
|
|
|
|
request = WSGIRequest(environ)
|
2006-09-28 09:56:02 +08:00
|
|
|
response = self.get_response(request)
|
2006-08-27 20:24:59 +08:00
|
|
|
|
|
|
|
# Apply response middleware
|
|
|
|
for middleware_method in self._response_middleware:
|
|
|
|
response = middleware_method(request, response)
|
2007-11-11 11:55:44 +08:00
|
|
|
response = self.apply_response_fixes(request, response)
|
2006-08-27 20:24:59 +08:00
|
|
|
finally:
|
|
|
|
dispatcher.send(signal=signals.request_finished)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
def store_rendered_templates(store, signal, sender, template, context):
|
|
|
|
"A utility function for storing templates and contexts that are rendered"
|
|
|
|
store.setdefault('template',[]).append(template)
|
|
|
|
store.setdefault('context',[]).append(context)
|
|
|
|
|
|
|
|
def encode_multipart(boundary, data):
|
|
|
|
"""
|
|
|
|
A simple method for encoding multipart POST data from a dictionary of
|
|
|
|
form values.
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
The key will be used as the form data name; the value will be transmitted
|
|
|
|
as content. If the value is a file, the contents of the file will be sent
|
|
|
|
as an application/octet-stream; otherwise, str(value) will be sent.
|
|
|
|
"""
|
|
|
|
lines = []
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
|
2006-08-27 20:24:59 +08:00
|
|
|
for (key, value) in data.items():
|
|
|
|
if isinstance(value, file):
|
|
|
|
lines.extend([
|
|
|
|
'--' + boundary,
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
'Content-Disposition: form-data; name="%s"; filename="%s"' % (to_str(key), to_str(value.name)),
|
2006-08-27 20:24:59 +08:00
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
value.read()
|
|
|
|
])
|
2007-09-15 03:55:24 +08:00
|
|
|
else:
|
|
|
|
if not isinstance(value, basestring) and is_iterable(value):
|
|
|
|
for item in value:
|
|
|
|
lines.extend([
|
|
|
|
'--' + boundary,
|
|
|
|
'Content-Disposition: form-data; name="%s"' % to_str(key),
|
|
|
|
'',
|
|
|
|
to_str(item)
|
|
|
|
])
|
|
|
|
else:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
lines.extend([
|
|
|
|
'--' + boundary,
|
|
|
|
'Content-Disposition: form-data; name="%s"' % to_str(key),
|
|
|
|
'',
|
2007-09-15 03:55:24 +08:00
|
|
|
to_str(value)
|
2007-03-22 18:23:52 +08:00
|
|
|
])
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
lines.extend([
|
|
|
|
'--' + boundary + '--',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
return '\r\n'.join(lines)
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
"""
|
2006-09-28 09:56:02 +08:00
|
|
|
A class that can act as a client for testing purposes.
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
It allows the user to compose GET and POST requests, and
|
|
|
|
obtain the response that the server gave to those requests.
|
|
|
|
The server Response objects are annotated with the details
|
|
|
|
of the contexts and templates that were rendered during the
|
|
|
|
process of serving the request.
|
|
|
|
|
|
|
|
Client objects are stateful - they will retain cookie (and
|
|
|
|
thus session) details for the lifetime of the Client instance.
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
This is not intended as a replacement for Twill/Selenium or
|
|
|
|
the like - it is here to allow testing against the
|
|
|
|
contexts and templates produced by a view, rather than the
|
|
|
|
HTML rendered to the end-user.
|
|
|
|
"""
|
|
|
|
def __init__(self, **defaults):
|
2006-09-02 17:26:24 +08:00
|
|
|
self.handler = ClientHandler()
|
2006-08-27 20:24:59 +08:00
|
|
|
self.defaults = defaults
|
2007-02-09 21:47:36 +08:00
|
|
|
self.cookies = SimpleCookie()
|
2007-02-11 08:23:31 +08:00
|
|
|
self.exc_info = None
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-02-11 08:23:31 +08:00
|
|
|
def store_exc_info(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Utility method that can be used to store exceptions when they are
|
|
|
|
generated by a view.
|
|
|
|
"""
|
|
|
|
self.exc_info = sys.exc_info()
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2007-05-05 23:16:15 +08:00
|
|
|
def _session(self):
|
|
|
|
"Obtain the current session variables"
|
|
|
|
if 'django.contrib.sessions' in settings.INSTALLED_APPS:
|
2007-09-16 05:29:14 +08:00
|
|
|
engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
|
2007-05-05 23:16:15 +08:00
|
|
|
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
|
|
|
|
if cookie:
|
2007-09-16 05:44:05 +08:00
|
|
|
return engine.SessionStore(cookie.value)
|
2007-05-05 23:16:15 +08:00
|
|
|
return {}
|
|
|
|
session = property(_session)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
def request(self, **request):
|
|
|
|
"""
|
2006-09-28 09:56:02 +08:00
|
|
|
The master request method. Composes the environment dictionary
|
2006-08-27 20:24:59 +08:00
|
|
|
and passes to the handler, returning the result of the handler.
|
|
|
|
Assumes defaults for the query environment, which can be overridden
|
|
|
|
using the arguments to the request.
|
|
|
|
"""
|
|
|
|
|
|
|
|
environ = {
|
2007-02-09 21:47:36 +08:00
|
|
|
'HTTP_COOKIE': self.cookies,
|
2006-08-27 20:24:59 +08:00
|
|
|
'PATH_INFO': '/',
|
|
|
|
'QUERY_STRING': '',
|
|
|
|
'REQUEST_METHOD': 'GET',
|
2006-09-28 09:56:02 +08:00
|
|
|
'SCRIPT_NAME': None,
|
2006-08-27 20:24:59 +08:00
|
|
|
'SERVER_NAME': 'testserver',
|
|
|
|
'SERVER_PORT': 80,
|
|
|
|
'SERVER_PROTOCOL': 'HTTP/1.1',
|
2006-09-28 09:56:02 +08:00
|
|
|
}
|
2006-08-27 20:24:59 +08:00
|
|
|
environ.update(self.defaults)
|
2006-09-28 09:56:02 +08:00
|
|
|
environ.update(request)
|
2006-08-27 20:24:59 +08:00
|
|
|
|
|
|
|
# Curry a data dictionary into an instance of
|
|
|
|
# the template renderer callback function
|
|
|
|
data = {}
|
|
|
|
on_template_render = curry(store_rendered_templates, data)
|
|
|
|
dispatcher.connect(on_template_render, signal=signals.template_rendered)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2007-02-11 08:23:31 +08:00
|
|
|
# Capture exceptions created by the handler
|
|
|
|
dispatcher.connect(self.store_exc_info, signal=got_request_exception)
|
|
|
|
|
2007-08-28 21:03:22 +08:00
|
|
|
try:
|
|
|
|
response = self.handler(environ)
|
|
|
|
except TemplateDoesNotExist, e:
|
|
|
|
# If the view raises an exception, Django will attempt to show
|
|
|
|
# the 500.html template. If that template is not available,
|
|
|
|
# we should ignore the error in favor of re-raising the
|
|
|
|
# underlying exception that caused the 500 error. Any other
|
|
|
|
# template found to be missing during view error handling
|
|
|
|
# should be reported as-is.
|
|
|
|
if e.args != ('500.html',):
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Look for a signalled exception and reraise it
|
|
|
|
if self.exc_info:
|
|
|
|
raise self.exc_info[1], None, self.exc_info[2]
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2007-09-03 19:21:40 +08:00
|
|
|
# Save the client and request that stimulated the response
|
|
|
|
response.client = self
|
|
|
|
response.request = request
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
# Add any rendered template detail to the response
|
2006-09-28 09:56:02 +08:00
|
|
|
# If there was only one template rendered (the most likely case),
|
2006-08-27 20:24:59 +08:00
|
|
|
# flatten the list to a single element
|
|
|
|
for detail in ('template', 'context'):
|
|
|
|
if data.get(detail):
|
|
|
|
if len(data[detail]) == 1:
|
|
|
|
setattr(response, detail, data[detail][0]);
|
|
|
|
else:
|
|
|
|
setattr(response, detail, data[detail])
|
|
|
|
else:
|
|
|
|
setattr(response, detail, None)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2007-05-05 23:16:15 +08:00
|
|
|
# Update persistent cookie data
|
2006-08-27 20:24:59 +08:00
|
|
|
if response.cookies:
|
2007-02-09 21:47:36 +08:00
|
|
|
self.cookies.update(response.cookies)
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
return response
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
def get(self, path, data={}, **extra):
|
|
|
|
"Request a response from the server using GET."
|
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': None,
|
|
|
|
'CONTENT_TYPE': 'text/html; charset=utf-8',
|
2008-03-20 14:50:54 +08:00
|
|
|
'PATH_INFO': urllib.unquote(path),
|
2007-07-21 13:17:20 +08:00
|
|
|
'QUERY_STRING': urlencode(data, doseq=True),
|
2006-08-27 20:24:59 +08:00
|
|
|
'REQUEST_METHOD': 'GET',
|
|
|
|
}
|
|
|
|
r.update(extra)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
return self.request(**r)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2007-02-17 08:23:09 +08:00
|
|
|
def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
|
2006-08-27 20:24:59 +08:00
|
|
|
"Request a response from the server using POST."
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2007-02-17 08:23:09 +08:00
|
|
|
if content_type is MULTIPART_CONTENT:
|
|
|
|
post_data = encode_multipart(BOUNDARY, data)
|
|
|
|
else:
|
|
|
|
post_data = data
|
2006-08-27 20:24:59 +08:00
|
|
|
|
|
|
|
r = {
|
2007-02-17 08:23:09 +08:00
|
|
|
'CONTENT_LENGTH': len(post_data),
|
|
|
|
'CONTENT_TYPE': content_type,
|
2008-03-20 14:50:54 +08:00
|
|
|
'PATH_INFO': urllib.unquote(path),
|
2006-08-27 20:24:59 +08:00
|
|
|
'REQUEST_METHOD': 'POST',
|
2007-02-17 08:23:09 +08:00
|
|
|
'wsgi.input': StringIO(post_data),
|
2006-08-27 20:24:59 +08:00
|
|
|
}
|
|
|
|
r.update(extra)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
return self.request(**r)
|
|
|
|
|
2007-05-05 23:16:15 +08:00
|
|
|
def login(self, **credentials):
|
|
|
|
"""Set the Client to appear as if it has sucessfully logged into a site.
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2007-05-05 23:16:15 +08:00
|
|
|
Returns True if login is possible; False if the provided credentials
|
2007-07-15 01:04:30 +08:00
|
|
|
are incorrect, or the user is inactive, or if the sessions framework is
|
2007-07-12 23:26:37 +08:00
|
|
|
not available.
|
2006-08-27 20:24:59 +08:00
|
|
|
"""
|
2007-05-05 23:16:15 +08:00
|
|
|
user = authenticate(**credentials)
|
2007-07-12 23:26:37 +08:00
|
|
|
if user and user.is_active and 'django.contrib.sessions' in settings.INSTALLED_APPS:
|
2007-09-16 05:29:14 +08:00
|
|
|
engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
|
2007-05-05 23:16:15 +08:00
|
|
|
|
|
|
|
# Create a fake request to store login details
|
|
|
|
request = HttpRequest()
|
2007-09-16 05:44:05 +08:00
|
|
|
request.session = engine.SessionStore()
|
2007-05-05 23:16:15 +08:00
|
|
|
login(request, user)
|
|
|
|
|
|
|
|
# Set the cookie to represent the session
|
2007-09-16 05:29:14 +08:00
|
|
|
self.cookies[settings.SESSION_COOKIE_NAME] = request.session.session_key
|
2007-05-05 23:16:15 +08:00
|
|
|
self.cookies[settings.SESSION_COOKIE_NAME]['max-age'] = None
|
|
|
|
self.cookies[settings.SESSION_COOKIE_NAME]['path'] = '/'
|
|
|
|
self.cookies[settings.SESSION_COOKIE_NAME]['domain'] = settings.SESSION_COOKIE_DOMAIN
|
|
|
|
self.cookies[settings.SESSION_COOKIE_NAME]['secure'] = settings.SESSION_COOKIE_SECURE or None
|
|
|
|
self.cookies[settings.SESSION_COOKIE_NAME]['expires'] = None
|
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
# Save the session values
|
2008-02-03 09:37:29 +08:00
|
|
|
request.session.save()
|
2007-05-05 23:16:15 +08:00
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
2006-09-02 17:26:24 +08:00
|
|
|
return False
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-08-17 22:20:25 +08:00
|
|
|
def logout(self):
|
|
|
|
"""Removes the authenticated user's cookies.
|
|
|
|
|
|
|
|
Causes the authenticated user to be logged out.
|
|
|
|
"""
|
2007-09-16 05:44:05 +08:00
|
|
|
session = __import__(settings.SESSION_ENGINE, {}, {}, ['']).SessionStore()
|
2008-02-03 09:37:29 +08:00
|
|
|
session.delete(session_key=self.cookies[settings.SESSION_COOKIE_NAME].value)
|
2007-08-17 22:20:25 +08:00
|
|
|
self.cookies = SimpleCookie()
|