[1.5.x] Prevented caching of streaming responses.

The test introduced in 4b278131 accidentally passed because of a
limitation of Python < 3.3.

Refs #17758, #7581.

Backport of 1c8be95 from master.
This commit is contained in:
Aymeric Augustin 2012-12-24 20:25:02 +01:00
parent ef98ae2910
commit cd914175c8
2 changed files with 10 additions and 13 deletions

View File

@ -93,7 +93,7 @@ class UpdateCacheMiddleware(object):
if not self._should_update_cache(request, response): if not self._should_update_cache(request, response):
# 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 not response.status_code == 200: if response.streaming or response.status_code != 200:
return response return response
# Try to get the timeout from the "max-age" section of the "Cache- # Try to get the timeout from the "max-age" section of the "Cache-
# Control" header before reverting to using the default cache_timeout # Control" header before reverting to using the default cache_timeout

View File

@ -1424,24 +1424,21 @@ class CacheI18nTest(TestCase):
CACHE_MIDDLEWARE_SECONDS=60, CACHE_MIDDLEWARE_SECONDS=60,
USE_ETAGS=True, USE_ETAGS=True,
) )
def test_middleware_with_streaming_response(self): def test_middleware_doesnt_cache_streaming_response(self):
# cache with non empty request.GET request = self._get_request()
request = self._get_request_cache(query_string='foo=baz&other=true')
# first access, cache must return None
get_cache_data = FetchFromCacheMiddleware().process_request(request) get_cache_data = FetchFromCacheMiddleware().process_request(request)
self.assertEqual(get_cache_data, None) self.assertIsNone(get_cache_data)
# pass streaming response through UpdateCacheMiddleware. # This test passes on Python < 3.3 even without the corresponding code
content = 'Check for cache with QUERY_STRING and streaming content' # in UpdateCacheMiddleware, because pickling a StreamingHttpResponse
# fails (http://bugs.python.org/issue14288). LocMemCache silently
# swallows the exception and doesn't store the response in cache.
content = ['Check for cache with streaming content.']
response = StreamingHttpResponse(content) response = StreamingHttpResponse(content)
UpdateCacheMiddleware().process_response(request, response) UpdateCacheMiddleware().process_response(request, response)
# second access, cache must still return None, because we can't cache
# streaming response.
get_cache_data = FetchFromCacheMiddleware().process_request(request) get_cache_data = FetchFromCacheMiddleware().process_request(request)
self.assertEqual(get_cache_data, None) self.assertIsNone(get_cache_data)
@override_settings( @override_settings(
CACHES={ CACHES={