示例#1
0
class FetchFromCacheMiddlewareTestCase(TestCase):
    RESPONSE_CACHED_VALUE = 'Response value'

    def setUp(self):
        self.middleware = FetchFromCacheMiddleware()
        self.factory = RequestFactory()
        self.request = RequestFactory().get('/')

    def warmup_cache(self):
        cache = get_cache(settings.CACHE_MIDDLEWARE_ALIAS)
        cache_header_key = _generate_cache_header_key(settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.request)
        cache.set(cache_header_key, 'header dummy cache', 30)
        cache_key = get_cache_key(self.request,
                                  settings.CACHE_MIDDLEWARE_KEY_PREFIX, 'GET',
                                  cache=cache)
        cache.set(cache_key, self.RESPONSE_CACHED_VALUE, 30)

    def test_cold_cache_regular_request(self):
        result = self.middleware.process_request(self.request)
        self.assertTrue(self.request._cache_update_cache)
        self.assertEqual(result, None)

    def test_warmedup_cache_regular_request(self):
        self.warmup_cache()
        result = self.middleware.process_request(self.request)
        self.assertFalse(self.request._cache_update_cache)
        self.assertEqual(result, self.RESPONSE_CACHED_VALUE)

    def test_cold_cache_request_force_cache(self):
        request = self.factory.get('/', WEBPERF_FORCE_CACHE_UPDATE=True)
        result = self.middleware.process_request(request)
        self.assertTrue(request._cache_update_cache)
        self.assertEqual(result, None)

    def test_warmedup_cache_request_force_cache(self):
        self.warmup_cache()
        request = self.factory.get('/', WEBPERF_FORCE_CACHE_UPDATE=True)
        result = self.middleware.process_request(request)
        self.assertTrue(request._cache_update_cache)
        self.assertEqual(result, None)
示例#2
0
 def setUp(self):
     self.middleware = FetchFromCacheMiddleware()
     self.factory = RequestFactory()
     self.request = RequestFactory().get('/')