Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Improved CACHE_MIDDLEWARE_ANONYMOUS_ONLY test.

Backport of r15557 from trunk. (Backported in order to simplify backport of upcoming test addition).

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15558 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9ec9d2efd134cd2fe64c33cf44af00780841edeb 1 parent dbe7010
Carl Meyer authored February 17, 2011

Showing 1 changed file with 39 additions and 15 deletions. Show diff stats Hide diff stats

  1. 54  tests/regressiontests/cache/tests.py
54  tests/regressiontests/cache/tests.py
@@ -14,8 +14,7 @@
14 14
 from django.core.cache import get_cache
15 15
 from django.core.cache.backends.base import CacheKeyWarning
16 16
 from django.http import HttpResponse, HttpRequest
17  
-from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware
18  
-from django.test import TestCase
  17
+from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware, CacheMiddleware
19 18
 from django.test.utils import get_warnings_state, restore_warnings_state
20 19
 from django.utils import translation
21 20
 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
@@ -648,27 +647,52 @@ def set_cache(request, lang, msg):
648 647
         get_cache_data = FetchFromCacheMiddleware().process_request(request)
649 648
         self.assertEqual(get_cache_data.content, es_message)
650 649
 
  650
+def hello_world_view(request, value):
  651
+    return HttpResponse('Hello World %s' % value)
651 652
 
652  
-class CacheMiddlewareAnonymousOnlyTests(TestCase):
653  
-    urls = 'regressiontests.cache.urls'
  653
+class CacheMiddlewareTest(unittest.TestCase):
654 654
 
655 655
     def setUp(self):
656  
-        self._orig_cache_middleware_anonymous_only = \
657  
-            getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
658  
-        self._orig_middleware_classes = settings.MIDDLEWARE_CLASSES
  656
+        self.orig_cache_middleware_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
  657
+        self._orig_cache_backend = settings.CACHE_BACKEND
659 658
 
660  
-        settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES)
661  
-        settings.MIDDLEWARE_CLASSES.insert(0, 'django.middleware.cache.UpdateCacheMiddleware')
662  
-        settings.MIDDLEWARE_CLASSES += ['django.middleware.cache.FetchFromCacheMiddleware']
  659
+        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = False
  660
+        settings.CACHE_BACKEND = "locmem://"
663 661
 
664 662
     def tearDown(self):
665  
-        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = self._orig_cache_middleware_anonymous_only
666  
-        settings.MIDDLEWARE_CLASSES = self._orig_middleware_classes
  663
+        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = self.orig_cache_middleware_anonymous_only
  664
+        settings.CACHE_BACKEND = self._orig_cache_backend
667 665
 
668  
-    def test_cache_middleware_anonymous_only_does_not_cause_vary_cookie(self):
  666
+    def test_cache_middleware_anonymous_only_wont_cause_session_access(self):
  667
+        """ The cache middleware shouldn't cause a session access due to
  668
+        CACHE_MIDDLEWARE_ANONYMOUS_ONLY if nothing else has accessed the
  669
+        session. Refs 13283 """
669 670
         settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
670  
-        response = self.client.get('/')
671  
-        self.failIf('Cookie' in response.get('Vary', ''))
  671
+
  672
+        from django.contrib.sessions.middleware import SessionMiddleware
  673
+        from django.contrib.auth.middleware import AuthenticationMiddleware
  674
+
  675
+        middleware = CacheMiddleware()
  676
+        session_middleware = SessionMiddleware()
  677
+        auth_middleware = AuthenticationMiddleware()
  678
+
  679
+        request = HttpRequest()
  680
+        request.path = '/view_anon/'
  681
+        request.method = 'GET'
  682
+
  683
+        # Put the request through the request middleware
  684
+        session_middleware.process_request(request)
  685
+        auth_middleware.process_request(request)
  686
+        result = middleware.process_request(request)
  687
+        self.assertEquals(result, None)
  688
+
  689
+        response = hello_world_view(request, '1')
  690
+
  691
+        # Now put the response through the response middleware
  692
+        session_middleware.process_response(request, response)
  693
+        response = middleware.process_response(request, response)
  694
+
  695
+        self.assertEqual(request.session.accessed, False)
672 696
 
673 697
 
674 698
 if __name__ == '__main__':

0 notes on commit 9ec9d2e

Please sign in to comment.
Something went wrong with that request. Please try again.