Ticket #10016: 10016-handle-long-urls-with-tests.diff

File 10016-handle-long-urls-with-tests.diff, 5.2 KB (added by mcroydon, 6 years ago)

Updated patch (apply with -p1) to include tests for the new behavior.

  • 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 
    2929from django.utils.encoding import smart_str, iri_to_uri
    3030from django.utils.http import http_date
    3131from django.utils.hashcompat import md5_constructor
     32from django.http import HttpRequest
    3233
    3334cc_delim_re = re.compile(r'\s*,\s*')
    3435
    def _generate_cache_key(request, headerlist, key_prefix): 
    143144        value = request.META.get(header, None)
    144145        if value is not None:
    145146            ctx.update(value)
     147    path = md5_constructor()
     148    path.update(iri_to_uri(request.path))
    146149    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())
    148151
    149152def get_cache_key(request, key_prefix=None):
    150153    """
    def get_cache_key(request, key_prefix=None): 
    158161    """
    159162    if key_prefix is None:
    160163        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
     164    path = md5_constructor()
     165    path.update(iri_to_uri(request.path))
    161166    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
    162                     key_prefix, iri_to_uri(request.path))
     167                    key_prefix, path.hexdigest())
    163168    headerlist = cache.get(cache_key, None)
    164169    if headerlist is not None:
    165170        return _generate_cache_key(request, headerlist, key_prefix)
    def learn_cache_key(request, response, cache_timeout=None, key_prefix=None): 
    183188        key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    184189    if cache_timeout is None:
    185190        cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
     191    path = md5_constructor()
     192    path.update(iri_to_uri(request.path))
    186193    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (
    187                     key_prefix, iri_to_uri(request.path))
     194                    key_prefix, path.hexdigest())
    188195    if response.has_header('Vary'):
    189196        headerlist = ['HTTP_'+header.upper().replace('-', '_')
    190197                      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 
    1313from django.core import management
    1414from django.core.cache import get_cache
    1515from django.core.cache.backends.base import InvalidCacheBackendError
    16 from django.http import HttpResponse
    17 from django.utils.cache import patch_vary_headers
     16from django.http import HttpResponse, HttpRequest
     17from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
    1818from django.utils.hashcompat import md5_constructor
    1919
    2020# functions/classes for complex data type tests
    class FileBasedCacheTests(unittest.TestCase, BaseCacheTests): 
    294294class CacheUtils(unittest.TestCase):
    295295    """TestCase for django.utils.cache functions."""
    296296
     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
    297314    def test_patch_vary_headers(self):
    298315        headers = (
    299316            # Initial vary, new headers, resulting vary.
    class CacheUtils(unittest.TestCase): 
    314331            patch_vary_headers(response, newheaders)
    315332            self.assertEqual(response['Vary'], resulting_vary)
    316333
     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')
    317354
    318355if __name__ == '__main__':
    319356    unittest.main()
Back to Top