From f9702977b72003c17db87d6e4fdaa36415f13a38 Mon Sep 17 00:00:00 2001 From: Rinat Khabibiev Date: Mon, 12 Sep 2016 17:19:36 +0300 Subject: [PATCH] Fixed #27211 -- Made UpdateCacheMiddleware include caching headers for "304 Not Modified" responses. --- django/middleware/cache.py | 4 ++-- tests/cache/tests.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/django/middleware/cache.py b/django/middleware/cache.py index 8ee3f514e85..a4dde10dc4f 100644 --- a/django/middleware/cache.py +++ b/django/middleware/cache.py @@ -77,7 +77,7 @@ class UpdateCacheMiddleware(MiddlewareMixin): # We don't need to update the cache, just return. return response - if response.streaming or response.status_code != 200: + if response.streaming or response.status_code not in (200, 304): return response # Don't cache responses that set a user-specific (and maybe security @@ -95,7 +95,7 @@ class UpdateCacheMiddleware(MiddlewareMixin): # max-age was set to 0, don't bother caching. return response patch_response_headers(response, timeout) - if timeout: + if timeout and response.status_code == 200: cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache) if hasattr(response, 'render') and callable(response.render): response.add_post_render_callback( diff --git a/tests/cache/tests.py b/tests/cache/tests.py index b6d37148ad3..4bd882e7917 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -22,7 +22,9 @@ from django.core.cache import ( ) from django.core.cache.utils import make_template_fragment_key from django.db import connection, connections -from django.http import HttpRequest, HttpResponse, StreamingHttpResponse +from django.http import ( + HttpRequest, HttpResponse, HttpResponseNotModified, StreamingHttpResponse, +) from django.middleware.cache import ( CacheMiddleware, FetchFromCacheMiddleware, UpdateCacheMiddleware, ) @@ -2147,6 +2149,19 @@ class CacheMiddlewareTest(SimpleTestCase): # Inserting a CSRF cookie in a cookie-less request prevented caching. self.assertIsNone(cache_middleware.process_request(request)) + def test_304_response_has_http_caching_headers_but_not_cached(self): + original_view = mock.Mock(return_value=HttpResponseNotModified()) + view = cache_page(2)(original_view) + request = self.factory.get('/view/') + # The view shouldn't be cached on the second call. + view(request).close() + response = view(request) + response.close() + self.assertEqual(original_view.call_count, 2) + self.assertIsInstance(response, HttpResponseNotModified) + self.assertIn('Cache-Control', response) + self.assertIn('Expires', response) + @override_settings( CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',