Ticket #15863: test-cache-middleware-unpickable-objects.diff

File test-cache-middleware-unpickable-objects.diff, 2.3 KB (added by rakuco, 4 years ago)

Proposed addition to Django's test suite

  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    index c5349d5..d33b9ac 100644
    a b class DummyCacheTests(unittest.TestCase): 
    165165class BaseCacheTests(object):
    166166    # A common set of tests to apply to all cache backends
    167167
     168    def _get_request_cache(self, path):
     169        request = HttpRequest()
     170        request.META = {
     171            'SERVER_NAME': 'testserver',
     172            'SERVER_PORT': 80,
     173        }
     174        request.path = request.path_info = path
     175        request._cache_update_cache = True
     176        request.method = 'GET'
     177        return request
     178
    168179    def test_simple(self):
    169180        # Simple cache set/get works
    170181        self.cache.set("key", "value")
    class BaseCacheTests(object): 
    724735        self.assertEqual(self.custom_key_cache.get('answer2'), 42)
    725736        self.assertEqual(self.custom_key_cache2.get('answer2'), 42)
    726737
     738    def test_cache_write_unpickable_object(self):
     739        update_middleware = UpdateCacheMiddleware()
     740        update_middleware.cache = self.cache
     741
     742        fetch_middleware = FetchFromCacheMiddleware()
     743        fetch_middleware.cache = self.cache
     744
     745        request = self._get_request_cache('/cache/test')
     746        get_cache_data = FetchFromCacheMiddleware().process_request(request)
     747        self.assertEqual(get_cache_data, None)
     748
     749        response = HttpResponse()
     750        content = 'Testing cookie serialization.'
     751        response.content = content
     752        response.set_cookie('foo', 'bar')
     753
     754        update_middleware.process_response(request, response)
     755
     756        get_cache_data = fetch_middleware.process_request(request)
     757        self.assertNotEqual(get_cache_data, None)
     758        self.assertEqual(get_cache_data.content, content)
     759        self.assertEqual(get_cache_data.cookies, response.cookies)
     760
     761        update_middleware.process_response(request, get_cache_data)
     762        get_cache_data = fetch_middleware.process_request(request)
     763        self.assertNotEqual(get_cache_data, None)
     764        self.assertEqual(get_cache_data.content, content)
     765        self.assertEqual(get_cache_data.cookies, response.cookies)
     766
    727767def custom_key_func(key, key_prefix, version):
    728768    "A customized cache key function"
    729769    return 'CUSTOM-' + '-'.join([key_prefix, str(version), key])
Back to Top