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
This commit is contained in:
Jacob Kaplan-Moss 2009-04-01 18:19:32 +00:00
parent 644e98136a
commit 16c9df0a76
2 changed files with 47 additions and 5 deletions

View File

@ -29,6 +29,7 @@ from django.core.cache import cache
from django.utils.encoding import smart_str, iri_to_uri from django.utils.encoding import smart_str, iri_to_uri
from django.utils.http import http_date from django.utils.http import http_date
from django.utils.hashcompat import md5_constructor from django.utils.hashcompat import md5_constructor
from django.http import HttpRequest
cc_delim_re = re.compile(r'\s*,\s*') 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) value = request.META.get(header, None)
if value is not None: if value is not None:
ctx.update(value) ctx.update(value)
path = md5_constructor(iri_to_uri(request.path))
return 'views.decorators.cache.cache_page.%s.%s.%s' % ( 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): def _generate_cache_header_key(key_prefix, request):
"""Returns a cache key for the header cache.""" """Returns a cache key for the header cache."""
return 'views.decorators.cache.cache_header.%s.%s' % ( path = md5_constructor(iri_to_uri(request.path))
key_prefix, 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): def get_cache_key(request, key_prefix=None):
""" """

View File

@ -13,8 +13,8 @@ from django.conf import settings
from django.core import management from django.core import management
from django.core.cache import get_cache from django.core.cache import get_cache
from django.core.cache.backends.base import InvalidCacheBackendError from django.core.cache.backends.base import InvalidCacheBackendError
from django.http import HttpResponse from django.http import HttpResponse, HttpRequest
from django.utils.cache import patch_vary_headers from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
from django.utils.hashcompat import md5_constructor from django.utils.hashcompat import md5_constructor
# functions/classes for complex data type tests # functions/classes for complex data type tests
@ -294,6 +294,26 @@ class FileBasedCacheTests(unittest.TestCase, BaseCacheTests):
class CacheUtils(unittest.TestCase): class CacheUtils(unittest.TestCase):
"""TestCase for django.utils.cache functions.""" """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): def test_patch_vary_headers(self):
headers = ( headers = (
# Initial vary, new headers, resulting vary. # Initial vary, new headers, resulting vary.
@ -314,6 +334,26 @@ class CacheUtils(unittest.TestCase):
patch_vary_headers(response, newheaders) patch_vary_headers(response, newheaders)
self.assertEqual(response['Vary'], resulting_vary) 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__': if __name__ == '__main__':
unittest.main() unittest.main()