def test_cacheable(self): req = HttpRequest() req.path = "/some/path" req.method = "GET" self.assertTrue(request_is_cacheable(req)) req.user = AnonymousUser() self.assertTrue(request_is_cacheable(req)) req.method = "POST" self.assertFalse(request_is_cacheable(req)) req.method = "GET" self.assertTrue(request_is_cacheable(req)) # TODO: ensure that messages works res = HttpResponse("fun times") self.assertTrue(response_is_cacheable(res)) redirect = HttpResponseRedirect("someurl") self.assertFalse(response_is_cacheable(redirect)) res['Pragma'] = "no-cache" self.assertFalse(response_is_cacheable(res))
def decorated(self, request, *args, **kwargs): debug("starting") try: if not settings.CACHES['default']['JOHNNY_CACHE']: raise KeyError except KeyError: raise ImproperlyConfigured('Not using johnny cache backend') if request_is_cacheable(request): key = get_cache_key(request) debug("Retrievable.") cached = cache.get(key) if cached is not None: debug("serving from cache") res = HttpResponse(cached) res["ETag"] = key return res debug("generating!") response = self.f(request, *args, **kwargs) if response_is_cacheable(request, response): debug("storing!") cache.set(key, response.content, self.time) response["ETag"] = key else: debug("Not storable.") return response debug("Not retrievable.") debug("generating!") return self.f(request, *args, **kwargs)
def test_dont_cache_if_vary_is_cookie(self): request = self.factory.get("/") response = HttpResponse() response['Vary'] = "Cookie" self.assertFalse(response_is_cacheable(request, response))
def test_dont_cache_if_pragma_says_so(self): request = self.factory.get("/") response = HttpResponse() response['Pragma'] = "no-cache" self.assertFalse(response_is_cacheable(request, response))
def test_dont_cache_redirects(self): request = self.factory.get("/") response = HttpResponseRedirect("/other/") self.assertFalse(response_is_cacheable(request, response))
def test_dont_cache_responses_that_include_messages(self): request = self.factory.get("/") response = self.client.get("/test_messages/") self.assertFalse(response_is_cacheable(request, response))
def test_cache_simple_responses(self): request = self.factory.get("/") response = HttpResponse("foo") self.assertTrue(response_is_cacheable(request, response))