Ticket #10016: 10016-handle-long-urls-with-tests.diff
File 10016-handle-long-urls-with-tests.diff, 5.2 KB (added by , 16 years ago) |
---|
-
django/utils/cache.py
diff --git a/django/utils/cache.py b/django/utils/cache.py index 9c566ae..29a2e2d 100644
a b from django.core.cache import cache 29 29 from django.utils.encoding import smart_str, iri_to_uri 30 30 from django.utils.http import http_date 31 31 from django.utils.hashcompat import md5_constructor 32 from django.http import HttpRequest 32 33 33 34 cc_delim_re = re.compile(r'\s*,\s*') 34 35 … … def _generate_cache_key(request, headerlist, key_prefix): 143 144 value = request.META.get(header, None) 144 145 if value is not None: 145 146 ctx.update(value) 147 path = md5_constructor() 148 path.update(iri_to_uri(request.path)) 146 149 return 'views.decorators.cache.cache_page.%s.%s.%s' % ( 147 key_prefix, iri_to_uri(request.path), ctx.hexdigest())150 key_prefix, path.hexdigest(), ctx.hexdigest()) 148 151 149 152 def get_cache_key(request, key_prefix=None): 150 153 """ … … def get_cache_key(request, key_prefix=None): 158 161 """ 159 162 if key_prefix is None: 160 163 key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX 164 path = md5_constructor() 165 path.update(iri_to_uri(request.path)) 161 166 cache_key = 'views.decorators.cache.cache_header.%s.%s' % ( 162 key_prefix, iri_to_uri(request.path))167 key_prefix, path.hexdigest()) 163 168 headerlist = cache.get(cache_key, None) 164 169 if headerlist is not None: 165 170 return _generate_cache_key(request, headerlist, key_prefix) … … def learn_cache_key(request, response, cache_timeout=None, key_prefix=None): 183 188 key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX 184 189 if cache_timeout is None: 185 190 cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS 191 path = md5_constructor() 192 path.update(iri_to_uri(request.path)) 186 193 cache_key = 'views.decorators.cache.cache_header.%s.%s' % ( 187 key_prefix, iri_to_uri(request.path))194 key_prefix, path.hexdigest()) 188 195 if response.has_header('Vary'): 189 196 headerlist = ['HTTP_'+header.upper().replace('-', '_') 190 197 for header in cc_delim_re.split(response['Vary'])] -
tests/regressiontests/cache/tests.py
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index fe365f9..a82d776 100644
a b from django.conf import settings 13 13 from django.core import management 14 14 from django.core.cache import get_cache 15 15 from django.core.cache.backends.base import InvalidCacheBackendError 16 from django.http import HttpResponse 17 from django.utils.cache import patch_vary_headers 16 from django.http import HttpResponse, HttpRequest 17 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key 18 18 from django.utils.hashcompat import md5_constructor 19 19 20 20 # functions/classes for complex data type tests … … class FileBasedCacheTests(unittest.TestCase, BaseCacheTests): 294 294 class CacheUtils(unittest.TestCase): 295 295 """TestCase for django.utils.cache functions.""" 296 296 297 def setUp(self): 298 self.path = '/cache/test/' 299 self.old_settings_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX 300 settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'settingsprefix' 301 302 def tearDown(self): 303 settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.old_settings_key_prefix 304 305 def _get_request(self, path): 306 request = HttpRequest() 307 request.META = { 308 'SERVER_NAME': 'testserver', 309 'SERVER_PORT': 80, 310 } 311 request.path = request.path_info = "/cache/%s" % path 312 return request 313 297 314 def test_patch_vary_headers(self): 298 315 headers = ( 299 316 # Initial vary, new headers, resulting vary. … … class CacheUtils(unittest.TestCase): 314 331 patch_vary_headers(response, newheaders) 315 332 self.assertEqual(response['Vary'], resulting_vary) 316 333 334 def test_get_cache_key(self): 335 request = self._get_request(self.path) 336 response = HttpResponse() 337 key_prefix = 'localprefix' 338 # Expect None if no headers have been set yet. 339 self.assertEqual(get_cache_key(request), None) 340 # Set headers to an empty list. 341 learn_cache_key(request, response) 342 self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e') 343 # Verify that a specified key_prefix is taken in to account. 344 learn_cache_key(request, response, key_prefix=key_prefix) 345 self.assertEqual(get_cache_key(request, key_prefix=key_prefix), 'views.decorators.cache.cache_page.localprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e') 346 347 def test_learn_cache_key(self): 348 request = self._get_request(self.path) 349 response = HttpResponse() 350 response['Vary'] = 'Pony' 351 # Make sure that the Vary header is added to the key hash 352 learn_cache_key(request, response) 353 self.assertEqual(get_cache_key(request), 'views.decorators.cache.cache_page.settingsprefix.a8c87a3d8c44853d7f79474f7ffe4ad5.d41d8cd98f00b204e9800998ecf8427e') 317 354 318 355 if __name__ == '__main__': 319 356 unittest.main()