diff options
| author | Unai Zalakain <unai@gisa-elkartea.org> | 2013-10-29 18:31:54 +0100 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-11-19 09:04:20 +0200 |
| commit | 4fdd51b73240bf9c8d9472fcc45df699f0714755 (patch) | |
| tree | 50e7cb8ea392bce529da22eb84a22b95a4220382 /django | |
| parent | 9348fc5628ccbbe156e9523b5eb8ba3ce783166e (diff) | |
Fixed #15179 -- middlewares not applied for test client login()
Requests made with django.test.Client.login() and logout() respect
defaults defined in django.test.Client instantiation and are processed
through middleware.
Thanks to Loic for the reviews.
Diffstat (limited to 'django')
| -rw-r--r-- | django/test/client.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/django/test/client.py b/django/test/client.py index c916b7dbe0..bf099e5b37 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -15,7 +15,7 @@ from django.core.handlers.wsgi import WSGIRequest from django.core.signals import (request_started, request_finished, got_request_exception) from django.db import close_old_connections -from django.http import SimpleCookie, HttpRequest, QueryDict +from django.http import SimpleCookie, QueryDict from django.template import TemplateDoesNotExist from django.test import signals from django.utils.functional import curry @@ -83,8 +83,9 @@ def closing_iterator_wrapper(iterable, close): class ClientHandler(BaseHandler): """ A HTTP Handler that can be used for testing purposes. - Uses the WSGI interface to compose requests, but returns - the raw HttpResponse object + Uses the WSGI interface to compose requests, but returns the raw + HttpResponse object with the originating WSGIRequest attached to its + ``request_instance`` attribute. """ def __init__(self, enforce_csrf_checks=True, *args, **kwargs): self.enforce_csrf_checks = enforce_csrf_checks @@ -105,7 +106,13 @@ class ClientHandler(BaseHandler): # required for backwards compatibility with external tests against # admin views. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks + + # Request goes through middleware. response = self.get_response(request) + # Attach the originating request to the response so that it could be + # later retrieved. + response.request_instance = request + # We're emulating a WSGI server; we must call the close method # on completion. if response.streaming: @@ -546,8 +553,9 @@ class Client(RequestFactory): 'django.contrib.sessions' in settings.INSTALLED_APPS): engine = import_module(settings.SESSION_ENGINE) - # Create a fake request to store login details. - request = HttpRequest() + # Create a fake request that goes through request middleware + request = self.request().request_instance + if self.session: request.session = self.session else: @@ -579,7 +587,9 @@ class Client(RequestFactory): Causes the authenticated user to be logged out. """ - request = HttpRequest() + # Create a fake request that goes through request middleware + request = self.request().request_instance + engine = import_module(settings.SESSION_ENGINE) UserModel = get_user_model() if self.session: |
