def test_middleware_limit_cache_invalid_settings(self): """ Ensure that the middleware does not rewrite the response if MAX_BROWSER_CACHE_TTL is not a positive integer. """ def response_builder(request): response = HttpResponse("OK") patch_response_headers(response, cache_timeout=3600) return response with self.settings(MAX_BROWSER_CACHE_TTL=-10): middleware = LimitBrowserCacheTTLHeaders(response_builder) response = middleware(HttpRequest()) self.assertEqual(http_date(time.time() + 3600), response["Expires"]) self.assertIn("max-age=3600", response["Cache-Control"]) with self.settings(MAX_BROWSER_CACHE_TTL="pouet"): middleware = LimitBrowserCacheTTLHeaders(response_builder) response = middleware(HttpRequest()) self.assertEqual(http_date(time.time() + 3600), response["Expires"]) self.assertIn("max-age=3600", response["Cache-Control"]) with self.settings(MAX_BROWSER_CACHE_TTL=None): middleware = LimitBrowserCacheTTLHeaders(response_builder) response = middleware(HttpRequest()) self.assertEqual(http_date(time.time() + 3600), response["Expires"]) self.assertIn("max-age=3600", response["Cache-Control"])
def test_middleware_with_no_cache_headers(self): """ Ensure that the middleware does not rewrite the response if it does not contains Cache headers. """ response = HttpResponse("OK") expected_headers = response.serialize_headers() with self.settings(MAX_BROWSER_CACHE_TTL=1): middleware = LimitBrowserCacheTTLHeaders(lambda request: response) response_to_test = middleware(HttpRequest()) self.assertEqual(expected_headers, response_to_test.serialize_headers())
def test_middleware_limit_not_reached(self): """ Ensure that the middleware does not rewrite the response if the cache headers timeout are lower than MAX_BROWSER_CACHE_TTL """ response = HttpResponse("OK") patch_response_headers(response, cache_timeout=30) expected_headers = response.serialize_headers() self.assertIn("max-age=30", response["Cache-Control"]) with self.settings(MAX_BROWSER_CACHE_TTL=60): middleware = LimitBrowserCacheTTLHeaders(lambda request: response) response_to_test = middleware(HttpRequest()) self.assertEqual(expected_headers, response_to_test.serialize_headers())
def test_middleware_limit_cache_not_set(self): """ The MAX_BROWSER_CACHE_TTL setting should default to 600 if not set in the project """ response = HttpResponse("OK") patch_response_headers(response, cache_timeout=3600) self.assertIn("max-age=3600", response["Cache-Control"]) middleware = LimitBrowserCacheTTLHeaders(lambda request: response) response_to_test = middleware(HttpRequest()) expected_expires = http_date(time.time() + 600) self.assertEqual(expected_expires, response_to_test["Expires"]) self.assertIn("max-age=600", response_to_test["Cache-Control"])
def test_middleware_limit_reached(self): """ Ensure that the middleware rewrite the response if the cache headers timeout are greater than MAX_BROWSER_CACHE_TTL """ response = HttpResponse("OK") patch_response_headers(response, cache_timeout=3600) self.assertIn("max-age=3600", response["Cache-Control"]) with self.settings(MAX_BROWSER_CACHE_TTL=5): middleware = LimitBrowserCacheTTLHeaders(lambda request: response) response_to_test = middleware(HttpRequest()) expected_expires = http_date(time.time() + 5) self.assertEqual(expected_expires, response_to_test["Expires"]) self.assertIn("max-age=5", response_to_test["Cache-Control"])