Beispiel #1
0
    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))
Beispiel #2
0
    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))
Beispiel #3
0
    def test_authenticated_and_anonymous_users(self):
        request = self.factory.get("/")
        request.user = AnonymousUser()
        self.assertTrue(request_is_cacheable(request))

        john = User.objects.create_user("john", "*****@*****.**", "secret")
        self.client.login(username="******", password="******")
        response = self.client.get("/")
        self.assertTrue(request_is_cacheable(request))
Beispiel #4
0
    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)
Beispiel #5
0
    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)
Beispiel #6
0
    def test_only_cache_get_requests(self):
        request = self.factory.get("/")
        self.assertTrue(request_is_cacheable(request))

        request = self.factory.post("/")
        self.assertFalse(request_is_cacheable(request))