diff --git a/django/middleware/cache.py b/django/middleware/cache.py
a
|
b
|
|
113 | 113 | version if available. |
114 | 114 | """ |
115 | 115 | if self.cache_anonymous_only: |
| 116 | # do all of our request.user access here, so we can reset |
| 117 | # request.session.accessed afterwards (to avoid |
| 118 | # SessionMiddleware setting Vary: Cookie) |
| 119 | assert hasattr(request, 'session'), "The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires both session middleware and authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware' and 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware." |
| 120 | session_accessed = request.session.accessed |
116 | 121 | assert hasattr(request, 'user'), "The Django cache middleware with CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True requires authentication middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware' before the CacheMiddleware." |
| 122 | user_is_authenticated = request.user.is_authenticated() |
| 123 | request.session.accessed = session_accessed |
117 | 124 | |
118 | 125 | if not request.method in ('GET', 'HEAD') or request.GET: |
119 | 126 | request._cache_update_cache = False |
120 | 127 | return None # Don't bother checking the cache. |
121 | 128 | |
122 | | if self.cache_anonymous_only and request.user.is_authenticated(): |
| 129 | if self.cache_anonymous_only and user_is_authenticated: |
123 | 130 | request._cache_update_cache = False |
124 | 131 | return None # Don't cache requests from authenticated users. |
125 | 132 | |
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
a
|
b
|
|
18 | 18 | from django.utils import translation |
19 | 19 | from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key |
20 | 20 | from django.utils.hashcompat import md5_constructor |
| 21 | from django.test import TestCase |
21 | 22 | from regressiontests.cache.models import Poll, expensive_calculation |
22 | 23 | |
23 | 24 | # functions/classes for complex data type tests |
… |
… |
|
568 | 569 | get_cache_data = FetchFromCacheMiddleware().process_request(request) |
569 | 570 | self.assertEqual(get_cache_data.content, es_message) |
570 | 571 | |
| 572 | class CacheMiddlewareTests(TestCase): |
| 573 | urls = 'regressiontests.cache.test_urls' |
| 574 | |
| 575 | def setUp(self): |
| 576 | self._orig_cache_middleware_anonymous_only = getattr(settings, |
| 577 | 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', |
| 578 | False) |
| 579 | self._orig_middleware_classes = settings.MIDDLEWARE_CLASSES |
| 580 | |
| 581 | settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES) |
| 582 | settings.MIDDLEWARE_CLASSES.insert(0, 'django.middleware.cache.UpdateCacheMiddleware') |
| 583 | settings.MIDDLEWARE_CLASSES += ['django.middleware.cache.FetchFromCacheMiddleware'] |
| 584 | |
| 585 | def tearDown(self): |
| 586 | settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = self._orig_cache_middleware_anonymous_only |
| 587 | settings.MIDDLEWARE_CLASSES = self._orig_middleware_classes |
| 588 | |
| 589 | def test_cache_middleware_anonymous_only_does_not_cause_vary_cookie(self): |
| 590 | settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True |
| 591 | |
| 592 | response = self.client.get('/') |
| 593 | |
| 594 | self.failIf('Cookie' in response.get('Vary', '')) |
| 595 | |
| 596 | |
| 597 | |
571 | 598 | if __name__ == '__main__': |
572 | 599 | unittest.main() |