Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10016: the cache middleware no longer vomits when handed long …

…URLs. Thanks, Matt Croydon.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10335 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 16c9df0a761732b8e3acbfa39b940ffde0b1dbf3 1 parent 644e981
@jacobian jacobian authored
Showing with 47 additions and 5 deletions.
  1. +5 −3 django/utils/cache.py
  2. +42 −2 tests/regressiontests/cache/tests.py
View
8 django/utils/cache.py
@@ -29,6 +29,7 @@
from django.utils.encoding import smart_str, iri_to_uri
from django.utils.http import http_date
from django.utils.hashcompat import md5_constructor
+from django.http import HttpRequest
cc_delim_re = re.compile(r'\s*,\s*')
@@ -143,13 +144,14 @@ def _generate_cache_key(request, headerlist, key_prefix):
value = request.META.get(header, None)
if value is not None:
ctx.update(value)
+ path = md5_constructor(iri_to_uri(request.path))
return 'views.decorators.cache.cache_page.%s.%s.%s' % (
- key_prefix, iri_to_uri(request.path), ctx.hexdigest())
+ key_prefix, path.hexdigest(), ctx.hexdigest())
def _generate_cache_header_key(key_prefix, request):
"""Returns a cache key for the header cache."""
- return 'views.decorators.cache.cache_header.%s.%s' % (
- key_prefix, iri_to_uri(request.path))
+ path = md5_constructor(iri_to_uri(request.path))
+ return 'views.decorators.cache.cache_header.%s.%s' % (key_prefix, path.hexdigest())
def get_cache_key(request, key_prefix=None):
"""
View
44 tests/regressiontests/cache/tests.py
@@ -13,8 +13,8 @@
from django.core import management
from django.core.cache import get_cache
from django.core.cache.backends.base import InvalidCacheBackendError
-from django.http import HttpResponse
-from django.utils.cache import patch_vary_headers
+from django.http import HttpResponse, HttpRequest
+from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
from django.utils.hashcompat import md5_constructor
# functions/classes for complex data type tests
@@ -294,6 +294,26 @@ def test_subdirectory_removal(self):
class CacheUtils(unittest.TestCase):
"""TestCase for django.utils.cache functions."""
+ def setUp(self):
+ self.path = '/cache/test/'
+ self.old_settings_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
+ self.old_middleware_seconds = settings.CACHE_MIDDLEWARE_SECONDS
+ settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'settingsprefix'
+ settings.CACHE_MIDDLEWARE_SECONDS = 1
+
+ def tearDown(self):
+ settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.old_settings_key_prefix
+ settings.CACHE_MIDDLEWARE_SECONDS = self.old_middleware_seconds
+
+ def _get_request(self, path):
+ request = HttpRequest()
+ request.META = {
+ 'SERVER_NAME': 'testserver',
+ 'SERVER_PORT': 80,
+ }
+ request.path = request.path_info = "/cache/%s" % path
+ return request
+
def test_patch_vary_headers(self):
headers = (
# Initial vary, new headers, resulting vary.
@@ -314,6 +334,26 @@ def test_patch_vary_headers(self):
patch_vary_headers(response, newheaders)
self.assertEqual(response['Vary'], resulting_vary)
+ def test_get_cache_key(self):
+ request = self._get_request(self.path)
+ response = HttpResponse()
+ key_prefix = 'localprefix'
+ # Expect None if no headers have been set yet.
+ self.assertEqual(get_cache_key(request), None)
+ # Set headers to an empty list.
+ learn_cache_key(request, response)
+ self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e')
+ # Verify that a specified key_prefix is taken in to account.
+ learn_cache_key(request, response, key_prefix=key_prefix)
+ self.assertEqual(get_cache_key(request, key_prefix=key_prefix), 'views.decorators.cache.cache_page.localprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e')
+
+ def test_learn_cache_key(self):
+ request = self._get_request(self.path)
+ response = HttpResponse()
+ response['Vary'] = 'Pony'
+ # Make sure that the Vary header is added to the key hash
+ learn_cache_key(request, response)
+ self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e')
if __name__ == '__main__':
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.