Fixed #27211 -- Made UpdateCacheMiddleware include caching headers for "304 Not Modified" responses.

This commit is contained in:
Rinat Khabibiev 2016-09-12 17:19:36 +03:00 committed by Tim Graham
parent 9459ec82aa
commit f9702977b7
2 changed files with 18 additions and 3 deletions

View File

@ -77,7 +77,7 @@ class UpdateCacheMiddleware(MiddlewareMixin):
# We don't need to update the cache, just return. # We don't need to update the cache, just return.
return response return response
if response.streaming or response.status_code != 200: if response.streaming or response.status_code not in (200, 304):
return response return response
# Don't cache responses that set a user-specific (and maybe security # 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. # max-age was set to 0, don't bother caching.
return response return response
patch_response_headers(response, timeout) 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) cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)
if hasattr(response, 'render') and callable(response.render): if hasattr(response, 'render') and callable(response.render):
response.add_post_render_callback( response.add_post_render_callback(

17
tests/cache/tests.py vendored
View File

@ -22,7 +22,9 @@ from django.core.cache import (
) )
from django.core.cache.utils import make_template_fragment_key from django.core.cache.utils import make_template_fragment_key
from django.db import connection, connections 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 ( from django.middleware.cache import (
CacheMiddleware, FetchFromCacheMiddleware, UpdateCacheMiddleware, CacheMiddleware, FetchFromCacheMiddleware, UpdateCacheMiddleware,
) )
@ -2147,6 +2149,19 @@ class CacheMiddlewareTest(SimpleTestCase):
# Inserting a CSRF cookie in a cookie-less request prevented caching. # Inserting a CSRF cookie in a cookie-less request prevented caching.
self.assertIsNone(cache_middleware.process_request(request)) 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( @override_settings(
CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix', CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',