2008-03-20 14:50:54 +08:00
|
|
|
import urllib
|
2009-02-27 21:14:59 +08:00
|
|
|
from urlparse import urlparse, urlunparse, urlsplit
|
2007-02-11 08:23:31 +08:00
|
|
|
import sys
|
2008-06-06 21:39:42 +08:00
|
|
|
import os
|
2009-04-11 17:20:10 +08:00
|
|
|
import re
|
2008-07-01 23:10:51 +08:00
|
|
|
try:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
except ImportError:
|
|
|
|
from StringIO import StringIO
|
2008-07-02 12:34:05 +08:00
|
|
|
|
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
|
2009-02-27 21:14:59 +08:00
|
|
|
from django.http import SimpleCookie, HttpRequest, QueryDict
|
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
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2007-09-15 03:55:24 +08:00
|
|
|
from django.utils.itercompat import is_iterable
|
2009-01-16 10:30:22 +08:00
|
|
|
from django.db import transaction, close_connection
|
2009-03-18 18:46:55 +08:00
|
|
|
from django.test.utils import ContextList
|
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
|
2009-04-11 17:20:10 +08:00
|
|
|
CONTENT_TYPE_RE = re.compile('.*; charset=([\w\d-]+);?')
|
2008-07-02 12:34:05 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
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
|
|
|
|
|
2008-07-02 12:34:05 +08:00
|
|
|
|
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()
|
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_started.send(sender=self.__class__)
|
2006-08-27 20:24:59 +08:00
|
|
|
try:
|
|
|
|
request = WSGIRequest(environ)
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
# sneaky little hack so that we can easily get round
|
|
|
|
# CsrfViewMiddleware. This makes life easier, and is probably
|
|
|
|
# required for backwards compatibility with external tests against
|
|
|
|
# admin views.
|
|
|
|
request._dont_enforce_csrf_checks = True
|
2006-09-28 09:56:02 +08:00
|
|
|
response = self.get_response(request)
|
2006-08-27 20:24:59 +08:00
|
|
|
|
2008-07-02 12:34:05 +08:00
|
|
|
# Apply response middleware.
|
2006-08-27 20:24:59 +08:00
|
|
|
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:
|
2009-02-21 15:19:12 +08:00
|
|
|
signals.request_finished.disconnect(close_connection)
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.request_finished.send(sender=self.__class__)
|
2009-01-16 10:30:22 +08:00
|
|
|
signals.request_finished.connect(close_connection)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
return response
|
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def store_rendered_templates(store, signal, sender, template, context, **kwargs):
|
2008-07-02 12:34:05 +08:00
|
|
|
"""
|
|
|
|
Stores templates and contexts that are rendered.
|
|
|
|
"""
|
2009-03-18 18:46:55 +08:00
|
|
|
store.setdefault('template', []).append(template)
|
|
|
|
store.setdefault('context', ContextList()).append(context)
|
2006-08-27 20:24:59 +08:00
|
|
|
|
|
|
|
def encode_multipart(boundary, data):
|
|
|
|
"""
|
2008-07-02 12:34:05 +08:00
|
|
|
Encodes 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)
|
2008-07-08 06:06:32 +08:00
|
|
|
|
|
|
|
# Not by any means perfect, but good enough for our purposes.
|
|
|
|
is_file = lambda thing: hasattr(thing, "read") and callable(thing.read)
|
|
|
|
|
|
|
|
# Each bit of the multipart form data could be either a form value or a
|
|
|
|
# file, or a *list* of form values and/or files. Remember that HTTP field
|
|
|
|
# names can be duplicated!
|
2006-08-27 20:24:59 +08:00
|
|
|
for (key, value) in data.items():
|
2008-07-08 06:06:32 +08:00
|
|
|
if is_file(value):
|
|
|
|
lines.extend(encode_file(boundary, key, value))
|
|
|
|
elif not isinstance(value, basestring) and is_iterable(value):
|
|
|
|
for item in value:
|
|
|
|
if is_file(item):
|
|
|
|
lines.extend(encode_file(boundary, key, item))
|
|
|
|
else:
|
2007-09-15 03:55:24 +08:00
|
|
|
lines.extend([
|
|
|
|
'--' + boundary,
|
|
|
|
'Content-Disposition: form-data; name="%s"' % to_str(key),
|
|
|
|
'',
|
|
|
|
to_str(item)
|
|
|
|
])
|
2008-07-08 06:06:32 +08:00
|
|
|
else:
|
|
|
|
lines.extend([
|
|
|
|
'--' + boundary,
|
|
|
|
'Content-Disposition: form-data; name="%s"' % to_str(key),
|
|
|
|
'',
|
|
|
|
to_str(value)
|
|
|
|
])
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
lines.extend([
|
|
|
|
'--' + boundary + '--',
|
|
|
|
'',
|
|
|
|
])
|
|
|
|
return '\r\n'.join(lines)
|
|
|
|
|
2008-07-08 06:06:32 +08:00
|
|
|
def encode_file(boundary, key, file):
|
|
|
|
to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
|
|
|
|
return [
|
|
|
|
'--' + boundary,
|
|
|
|
'Content-Disposition: form-data; name="%s"; filename="%s"' \
|
|
|
|
% (to_str(key), to_str(os.path.basename(file.name))),
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
file.read()
|
|
|
|
]
|
2008-09-17 19:32:11 +08:00
|
|
|
|
|
|
|
class Client(object):
|
2006-08-27 20:24:59 +08:00
|
|
|
"""
|
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
|
2008-11-12 19:19:37 +08:00
|
|
|
self.errors = StringIO()
|
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
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def store_exc_info(self, **kwargs):
|
2007-02-11 08:23:31 +08:00
|
|
|
"""
|
2008-07-02 12:34:05 +08:00
|
|
|
Stores exceptions when they are generated by a view.
|
2007-02-11 08:23:31 +08:00
|
|
|
"""
|
|
|
|
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):
|
2008-07-02 12:34:05 +08:00
|
|
|
"""
|
|
|
|
Obtains the current session variables.
|
|
|
|
"""
|
2007-05-05 23:16:15 +08:00
|
|
|
if 'django.contrib.sessions' in settings.INSTALLED_APPS:
|
2009-03-19 00:55:59 +08:00
|
|
|
engine = import_module(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 = {
|
2010-01-28 22:46:29 +08:00
|
|
|
'HTTP_COOKIE': self.cookies.output(header='', sep='; '),
|
2006-08-27 20:24:59 +08:00
|
|
|
'PATH_INFO': '/',
|
|
|
|
'QUERY_STRING': '',
|
2009-02-21 15:19:12 +08:00
|
|
|
'REMOTE_ADDR': '127.0.0.1',
|
2006-08-27 20:24:59 +08:00
|
|
|
'REQUEST_METHOD': 'GET',
|
2008-07-21 15:57:10 +08:00
|
|
|
'SCRIPT_NAME': '',
|
2006-08-27 20:24:59 +08:00
|
|
|
'SERVER_NAME': 'testserver',
|
2008-08-28 19:52:50 +08:00
|
|
|
'SERVER_PORT': '80',
|
2006-08-27 20:24:59 +08:00
|
|
|
'SERVER_PROTOCOL': 'HTTP/1.1',
|
2008-11-12 19:19:37 +08:00
|
|
|
'wsgi.version': (1,0),
|
|
|
|
'wsgi.url_scheme': 'http',
|
|
|
|
'wsgi.errors': self.errors,
|
|
|
|
'wsgi.multiprocess': True,
|
|
|
|
'wsgi.multithread': False,
|
|
|
|
'wsgi.run_once': False,
|
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
|
|
|
|
2008-07-02 12:34:05 +08:00
|
|
|
# Curry a data dictionary into an instance of the template renderer
|
|
|
|
# callback function.
|
2006-08-27 20:24:59 +08:00
|
|
|
data = {}
|
|
|
|
on_template_render = curry(store_rendered_templates, data)
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.template_rendered.connect(on_template_render)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2008-07-02 12:34:05 +08:00
|
|
|
# Capture exceptions created by the handler.
|
2008-08-06 23:32:46 +08:00
|
|
|
got_request_exception.connect(self.store_exc_info)
|
2007-02-11 08:23:31 +08:00
|
|
|
|
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
|
|
|
|
|
2008-06-07 14:25:59 +08:00
|
|
|
# Look for a signalled exception, clear the current context
|
|
|
|
# exception data, then re-raise the signalled exception.
|
|
|
|
# Also make sure that the signalled exception is cleared from
|
|
|
|
# the local cache!
|
2007-08-28 21:03:22 +08:00
|
|
|
if self.exc_info:
|
2008-06-07 14:25:59 +08:00
|
|
|
exc_info = self.exc_info
|
|
|
|
self.exc_info = None
|
|
|
|
raise exc_info[1], None, exc_info[2]
|
2008-07-02 12:34:05 +08:00
|
|
|
|
|
|
|
# Save the client and request that stimulated the response.
|
2007-09-03 19:21:40 +08:00
|
|
|
response.client = self
|
|
|
|
response.request = request
|
|
|
|
|
2008-07-02 12:34:05 +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),
|
2008-07-02 12:34:05 +08:00
|
|
|
# flatten the list to a single element.
|
2006-08-27 20:24:59 +08:00
|
|
|
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
|
|
|
|
2008-07-02 12:34:05 +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
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def get(self, path, data={}, follow=False, **extra):
|
2008-07-02 12:34:05 +08:00
|
|
|
"""
|
|
|
|
Requests a response from the server using GET.
|
|
|
|
"""
|
2008-11-12 19:22:05 +08:00
|
|
|
parsed = urlparse(path)
|
2006-08-27 20:24:59 +08:00
|
|
|
r = {
|
|
|
|
'CONTENT_TYPE': 'text/html; charset=utf-8',
|
2008-11-16 16:20:25 +08:00
|
|
|
'PATH_INFO': urllib.unquote(parsed[2]),
|
|
|
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
2006-08-27 20:24:59 +08:00
|
|
|
'REQUEST_METHOD': 'GET',
|
2008-11-12 19:19:37 +08:00
|
|
|
'wsgi.input': FakePayload('')
|
2006-08-27 20:24:59 +08:00
|
|
|
}
|
|
|
|
r.update(extra)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
response = self.request(**r)
|
|
|
|
if follow:
|
|
|
|
response = self._handle_redirects(response)
|
|
|
|
return response
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def post(self, path, data={}, content_type=MULTIPART_CONTENT,
|
|
|
|
follow=False, **extra):
|
2008-07-02 12:34:05 +08:00
|
|
|
"""
|
|
|
|
Requests a response from the server using POST.
|
|
|
|
"""
|
2007-02-17 08:23:09 +08:00
|
|
|
if content_type is MULTIPART_CONTENT:
|
|
|
|
post_data = encode_multipart(BOUNDARY, data)
|
|
|
|
else:
|
2009-04-11 17:20:10 +08:00
|
|
|
# Encode the content so that the byte representation is correct.
|
|
|
|
match = CONTENT_TYPE_RE.match(content_type)
|
|
|
|
if match:
|
|
|
|
charset = match.group(1)
|
|
|
|
else:
|
|
|
|
charset = settings.DEFAULT_CHARSET
|
|
|
|
post_data = smart_str(data, encoding=charset)
|
2006-08-27 20:24:59 +08:00
|
|
|
|
2008-11-12 19:22:05 +08:00
|
|
|
parsed = urlparse(path)
|
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-11-16 16:20:25 +08:00
|
|
|
'PATH_INFO': urllib.unquote(parsed[2]),
|
|
|
|
'QUERY_STRING': parsed[4],
|
2006-08-27 20:24:59 +08:00
|
|
|
'REQUEST_METHOD': 'POST',
|
2008-07-01 23:10:51 +08:00
|
|
|
'wsgi.input': FakePayload(post_data),
|
2006-08-27 20:24:59 +08:00
|
|
|
}
|
|
|
|
r.update(extra)
|
2006-09-28 09:56:02 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
response = self.request(**r)
|
|
|
|
if follow:
|
|
|
|
response = self._handle_redirects(response)
|
|
|
|
return response
|
2006-08-27 20:24:59 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def head(self, path, data={}, follow=False, **extra):
|
2008-10-07 17:23:40 +08:00
|
|
|
"""
|
|
|
|
Request a response from the server using HEAD.
|
|
|
|
"""
|
2008-11-12 19:22:05 +08:00
|
|
|
parsed = urlparse(path)
|
2008-10-07 17:23:40 +08:00
|
|
|
r = {
|
|
|
|
'CONTENT_TYPE': 'text/html; charset=utf-8',
|
2008-11-16 16:20:25 +08:00
|
|
|
'PATH_INFO': urllib.unquote(parsed[2]),
|
|
|
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
2008-10-07 17:23:40 +08:00
|
|
|
'REQUEST_METHOD': 'HEAD',
|
2008-11-12 19:19:37 +08:00
|
|
|
'wsgi.input': FakePayload('')
|
2008-10-07 17:23:40 +08:00
|
|
|
}
|
|
|
|
r.update(extra)
|
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
response = self.request(**r)
|
|
|
|
if follow:
|
|
|
|
response = self._handle_redirects(response)
|
|
|
|
return response
|
2008-10-07 17:23:40 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def options(self, path, data={}, follow=False, **extra):
|
2008-10-07 17:23:40 +08:00
|
|
|
"""
|
|
|
|
Request a response from the server using OPTIONS.
|
|
|
|
"""
|
2008-11-12 19:22:05 +08:00
|
|
|
parsed = urlparse(path)
|
2008-10-07 17:23:40 +08:00
|
|
|
r = {
|
2008-11-16 16:20:25 +08:00
|
|
|
'PATH_INFO': urllib.unquote(parsed[2]),
|
|
|
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
2008-10-07 17:23:40 +08:00
|
|
|
'REQUEST_METHOD': 'OPTIONS',
|
2008-11-12 19:19:37 +08:00
|
|
|
'wsgi.input': FakePayload('')
|
2008-10-07 17:23:40 +08:00
|
|
|
}
|
|
|
|
r.update(extra)
|
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
response = self.request(**r)
|
|
|
|
if follow:
|
|
|
|
response = self._handle_redirects(response)
|
|
|
|
return response
|
2008-10-07 17:23:40 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def put(self, path, data={}, content_type=MULTIPART_CONTENT,
|
|
|
|
follow=False, **extra):
|
2008-10-07 17:23:40 +08:00
|
|
|
"""
|
|
|
|
Send a resource to the server using PUT.
|
|
|
|
"""
|
|
|
|
if content_type is MULTIPART_CONTENT:
|
|
|
|
post_data = encode_multipart(BOUNDARY, data)
|
|
|
|
else:
|
|
|
|
post_data = data
|
2008-11-12 19:22:05 +08:00
|
|
|
|
2009-10-26 23:02:54 +08:00
|
|
|
# Make `data` into a querystring only if it's not already a string. If
|
|
|
|
# it is a string, we'll assume that the caller has already encoded it.
|
|
|
|
query_string = None
|
|
|
|
if not isinstance(data, basestring):
|
|
|
|
query_string = urlencode(data, doseq=True)
|
|
|
|
|
2008-11-12 19:22:05 +08:00
|
|
|
parsed = urlparse(path)
|
2008-10-07 17:23:40 +08:00
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(post_data),
|
|
|
|
'CONTENT_TYPE': content_type,
|
2008-11-16 16:20:25 +08:00
|
|
|
'PATH_INFO': urllib.unquote(parsed[2]),
|
2009-10-26 23:02:54 +08:00
|
|
|
'QUERY_STRING': query_string or parsed[4],
|
2008-10-07 17:23:40 +08:00
|
|
|
'REQUEST_METHOD': 'PUT',
|
|
|
|
'wsgi.input': FakePayload(post_data),
|
|
|
|
}
|
|
|
|
r.update(extra)
|
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
response = self.request(**r)
|
|
|
|
if follow:
|
|
|
|
response = self._handle_redirects(response)
|
|
|
|
return response
|
2008-10-07 17:23:40 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def delete(self, path, data={}, follow=False, **extra):
|
2008-10-07 17:23:40 +08:00
|
|
|
"""
|
|
|
|
Send a DELETE request to the server.
|
|
|
|
"""
|
2008-11-12 19:22:05 +08:00
|
|
|
parsed = urlparse(path)
|
2008-10-07 17:23:40 +08:00
|
|
|
r = {
|
2008-11-16 16:20:25 +08:00
|
|
|
'PATH_INFO': urllib.unquote(parsed[2]),
|
|
|
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
2008-10-07 17:23:40 +08:00
|
|
|
'REQUEST_METHOD': 'DELETE',
|
2008-11-12 19:19:37 +08:00
|
|
|
'wsgi.input': FakePayload('')
|
|
|
|
}
|
2008-10-07 17:23:40 +08:00
|
|
|
r.update(extra)
|
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
response = self.request(**r)
|
|
|
|
if follow:
|
|
|
|
response = self._handle_redirects(response)
|
|
|
|
return response
|
2008-10-07 17:23:40 +08:00
|
|
|
|
2007-05-05 23:16:15 +08:00
|
|
|
def login(self, **credentials):
|
2008-07-02 12:34:05 +08:00
|
|
|
"""
|
|
|
|
Sets the Client to appear as if it has successfully 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)
|
2008-07-02 12:34:05 +08:00
|
|
|
if user and user.is_active \
|
|
|
|
and 'django.contrib.sessions' in settings.INSTALLED_APPS:
|
2009-03-19 00:55:59 +08:00
|
|
|
engine = import_module(settings.SESSION_ENGINE)
|
2007-05-05 23:16:15 +08:00
|
|
|
|
2008-07-02 12:34:05 +08:00
|
|
|
# Create a fake request to store login details.
|
2007-05-05 23:16:15 +08:00
|
|
|
request = HttpRequest()
|
2008-08-15 20:08:29 +08:00
|
|
|
if self.session:
|
|
|
|
request.session = self.session
|
|
|
|
else:
|
|
|
|
request.session = engine.SessionStore()
|
2007-05-05 23:16:15 +08:00
|
|
|
login(request, user)
|
|
|
|
|
2008-07-02 12:34:05 +08:00
|
|
|
# Set the cookie to represent the session.
|
2008-07-02 12:48:58 +08:00
|
|
|
session_cookie = settings.SESSION_COOKIE_NAME
|
|
|
|
self.cookies[session_cookie] = request.session.session_key
|
|
|
|
cookie_data = {
|
|
|
|
'max-age': None,
|
|
|
|
'path': '/',
|
|
|
|
'domain': settings.SESSION_COOKIE_DOMAIN,
|
|
|
|
'secure': settings.SESSION_COOKIE_SECURE or None,
|
|
|
|
'expires': None,
|
|
|
|
}
|
|
|
|
self.cookies[session_cookie].update(cookie_data)
|
2007-05-05 23:16:15 +08:00
|
|
|
|
2008-07-02 12:34:05 +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):
|
2008-07-02 12:34:05 +08:00
|
|
|
"""
|
2009-03-31 06:52:48 +08:00
|
|
|
Removes the authenticated user's cookies and session object.
|
2007-08-17 22:20:25 +08:00
|
|
|
|
|
|
|
Causes the authenticated user to be logged out.
|
|
|
|
"""
|
2009-03-19 00:55:59 +08:00
|
|
|
session = import_module(settings.SESSION_ENGINE).SessionStore()
|
2009-03-31 06:52:48 +08:00
|
|
|
session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
|
|
|
|
if session_cookie:
|
|
|
|
session.delete(session_key=session_cookie.value)
|
2007-08-17 22:20:25 +08:00
|
|
|
self.cookies = SimpleCookie()
|
2009-02-27 21:14:59 +08:00
|
|
|
|
|
|
|
def _handle_redirects(self, response):
|
|
|
|
"Follows any redirects by requesting responses from the server using GET."
|
|
|
|
|
|
|
|
response.redirect_chain = []
|
|
|
|
while response.status_code in (301, 302, 303, 307):
|
|
|
|
url = response['Location']
|
|
|
|
scheme, netloc, path, query, fragment = urlsplit(url)
|
|
|
|
|
|
|
|
redirect_chain = response.redirect_chain
|
|
|
|
redirect_chain.append((url, response.status_code))
|
|
|
|
|
2010-02-13 20:01:14 +08:00
|
|
|
extra = {}
|
|
|
|
if scheme:
|
|
|
|
extra['wsgi.url_scheme'] = scheme
|
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
# The test client doesn't handle external links,
|
|
|
|
# but since the situation is simulated in test_client,
|
|
|
|
# we fake things here by ignoring the netloc portion of the
|
|
|
|
# redirected URL.
|
2010-02-13 20:01:14 +08:00
|
|
|
response = self.get(path, QueryDict(query), follow=False, **extra)
|
2009-02-27 21:14:59 +08:00
|
|
|
response.redirect_chain = redirect_chain
|
|
|
|
|
|
|
|
# Prevent loops
|
|
|
|
if response.redirect_chain[-1] in response.redirect_chain[0:-1]:
|
|
|
|
break
|
|
|
|
return response
|
|
|
|
|