Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Prevented caching of streaming responses.

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

Refs #17758, #7581.
  • Loading branch information...
commit 1c8be95a864540d416602577d1aa03d58ba33168 1 parent 35d1cd0
Aymeric Augustin authored December 24, 2012
2  django/middleware/cache.py
@@ -93,7 +93,7 @@ def process_response(self, request, response):
93 93
         if not self._should_update_cache(request, response):
94 94
             # We don't need to update the cache, just return.
95 95
             return response
96  
-        if not response.status_code == 200:
  96
+        if response.streaming or response.status_code != 200:
97 97
             return response
98 98
         # Try to get the timeout from the "max-age" section of the "Cache-
99 99
         # Control" header before reverting to using the default cache_timeout
21  tests/regressiontests/cache/tests.py
@@ -1428,24 +1428,21 @@ def set_cache(request, lang, msg):
1428 1428
             CACHE_MIDDLEWARE_SECONDS=60,
1429 1429
             USE_ETAGS=True,
1430 1430
     )
1431  
-    def test_middleware_with_streaming_response(self):
1432  
-        # cache with non empty request.GET
1433  
-        request = self._get_request_cache(query_string='foo=baz&other=true')
1434  
-
1435  
-        # first access, cache must return None
  1431
+    def test_middleware_doesnt_cache_streaming_response(self):
  1432
+        request = self._get_request()
1436 1433
         get_cache_data = FetchFromCacheMiddleware().process_request(request)
1437  
-        self.assertEqual(get_cache_data, None)
  1434
+        self.assertIsNone(get_cache_data)
1438 1435
 
1439  
-        # pass streaming response through UpdateCacheMiddleware.
1440  
-        content = 'Check for cache with QUERY_STRING and streaming content'
  1436
+        # This test passes on Python < 3.3 even without the corresponding code
  1437
+        # in UpdateCacheMiddleware, because pickling a StreamingHttpResponse
  1438
+        # fails (http://bugs.python.org/issue14288). LocMemCache silently
  1439
+        # swallows the exception and doesn't store the response in cache.
  1440
+        content = ['Check for cache with streaming content.']
1441 1441
         response = StreamingHttpResponse(content)
1442 1442
         UpdateCacheMiddleware().process_response(request, response)
1443 1443
 
1444  
-        # second access, cache must still return None, because we can't cache
1445  
-        # streaming response.
1446 1444
         get_cache_data = FetchFromCacheMiddleware().process_request(request)
1447  
-        self.assertEqual(get_cache_data, None)
1448  
-
  1445
+        self.assertIsNone(get_cache_data)
1449 1446
 
1450 1447
 @override_settings(
1451 1448
         CACHES={

0 notes on commit 1c8be95

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