From 9eb16031ca837624433c49646289b50f9566a25d Mon Sep 17 00:00:00 2001 From: Unai Zalakain Date: Tue, 19 Nov 2013 09:44:30 +0100 Subject: [PATCH] Fixed #12571 -- Attached originating WSGIRequest to test client responses. Originating WSGIRequests are now attached to the ``wsgi_request`` attribute of the ``HttpResponse`` returned by the testing client. Thanks rvdrijst for the suggestion. --- django/test/client.py | 13 ++++++------- docs/releases/1.7.txt | 3 +++ docs/topics/testing/tools.txt | 7 +++++++ tests/test_client/tests.py | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/django/test/client.py b/django/test/client.py index 9197a5ab1b5..5dbc3e35684 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -83,10 +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 with the originating WSGIRequest attached to its - ``request_instance`` attribute. + A HTTP Handler that can be used for testing purposes. Uses the WSGI + interface to compose requests, but returns the raw HttpResponse object with + the originating WSGIRequest attached to its ``wsgi_request`` attribute. """ def __init__(self, enforce_csrf_checks=True, *args, **kwargs): self.enforce_csrf_checks = enforce_csrf_checks @@ -112,7 +111,7 @@ class ClientHandler(BaseHandler): response = self.get_response(request) # Attach the originating request to the response so that it could be # later retrieved. - response.request_instance = request + response.wsgi_request = request # We're emulating a WSGI server; we must call the close method # on completion. @@ -555,7 +554,7 @@ class Client(RequestFactory): engine = import_module(settings.SESSION_ENGINE) # Create a fake request that goes through request middleware - request = self.request().request_instance + request = self.request().wsgi_request if self.session: request.session = self.session @@ -589,7 +588,7 @@ class Client(RequestFactory): Causes the authenticated user to be logged out. """ # Create a fake request that goes through request middleware - request = self.request().request_instance + request = self.request().wsgi_request engine = import_module(settings.SESSION_ENGINE) UserModel = get_user_model() diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index ab813795e98..a8becf1babd 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -579,6 +579,9 @@ Tests * :meth:`~django.test.TransactionTestCase.assertNumQueries` now prints out the list of executed queries if the assertion fails. +* The ``WSGIRequest`` instance generated by the test handler is now attached to + the :attr:`django.test.Response.wsgi_request` attribute. + Validators ^^^^^^^^^^ diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index d97644fb725..680c1dd17c6 100644 --- a/docs/topics/testing/tools.txt +++ b/docs/topics/testing/tools.txt @@ -427,6 +427,13 @@ Specifically, a ``Response`` object has the following attributes: The request data that stimulated the response. + .. attribute:: wsgi_request + + .. versionadded:: 1.7 + + The ``WSGIRequest`` instance generated by the test handler that + generated the response. + .. attribute:: status_code The HTTP status of the response, as an integer. See diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index 3693b337521..5fa6360edac 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -84,6 +84,20 @@ class ClientTest(TestCase): self.assertEqual(response['X-DJANGO-TEST'], 'Slartibartfast') + def test_response_attached_request(self): + """ + Check that the returned response has a ``request`` attribute with the + originating environ dict and a ``wsgi_request`` with the originating + ``WSGIRequest`` instance. + """ + response = self.client.get("/test_client/header_view/") + + self.assertTrue(hasattr(response, 'request')) + self.assertTrue(hasattr(response, 'wsgi_request')) + for key, value in response.request.items(): + self.assertIn(key, response.wsgi_request.environ) + self.assertEqual(response.wsgi_request.environ[key], value) + def test_raw_post(self): "POST raw data (with a content type) to a view" test_doc = """BlinkMalcolm Gladwell"""