| 1 |
from unittest import mock |
| 2 |
|
| 3 |
from asgiref.sync import iscoroutinefunction |
| 4 |
|
| 5 |
from django.http import HttpRequest, HttpResponse |
| 6 |
from django.test import SimpleTestCase |
| 7 |
from django.utils.decorators import method_decorator |
| 8 |
from django.views.decorators.cache import cache_control, cache_page, never_cache |
| 9 |
|
| 10 |
|
| 11 |
class HttpRequestProxy: |
| 12 |
def __init__(self, request): |
| 13 |
self._request = request |
| 14 |
|
| 15 |
def __getattr__(self, attr): |
| 16 |
"""Proxy to the underlying HttpRequest object.""" |
| 17 |
return getattr(self._request, attr) |
| 18 |
|
| 19 |
|
| 20 |
class CacheControlDecoratorTest(SimpleTestCase): |
| 21 |
def test_wrapped_sync_function_is_not_coroutine_function(self): |
| 22 |
def sync_view(request): |
| 23 |
return HttpResponse() |
| 24 |
|
| 25 |
wrapped_view = cache_control()(sync_view) |
| 26 |
self.assertIs(iscoroutinefunction(wrapped_view), False) |
| 27 |
|
| 28 |
def test_wrapped_async_function_is_coroutine_function(self): |
| 29 |
async def async_view(request): |
| 30 |
return HttpResponse() |
| 31 |
|
| 32 |
wrapped_view = cache_control()(async_view) |
| 33 |
self.assertIs(iscoroutinefunction(wrapped_view), True) |
| 34 |
|
| 35 |
def test_cache_control_decorator_http_request(self): |
| 36 |
class MyClass: |
| 37 |
@cache_control(a="b") |
| 38 |
def a_view(self, request): |
| 39 |
return HttpResponse() |
| 40 |
|
| 41 |
msg = ( |
| 42 |
"cache_control didn't receive an HttpRequest. If you are " |
| 43 |
"decorating a classmethod, be sure to use @method_decorator." |
| 44 |
) |
| 45 |
request = HttpRequest() |
| 46 |
with self.assertRaisesMessage(TypeError, msg): |
| 47 |
MyClass().a_view(request) |
| 48 |
with self.assertRaisesMessage(TypeError, msg): |
| 49 |
MyClass().a_view(HttpRequestProxy(request)) |
| 50 |
|
| 51 |
async def test_cache_control_decorator_http_request_async_view(self): |
| 52 |
class MyClass: |
| 53 |
@cache_control(a="b") |
| 54 |
async def async_view(self, request): |
| 55 |
return HttpResponse() |
| 56 |
|
| 57 |
msg = ( |
| 58 |
"cache_control didn't receive an HttpRequest. If you are decorating a " |
| 59 |
"classmethod, be sure to use @method_decorator." |
| 60 |
) |
| 61 |
request = HttpRequest() |
| 62 |
with self.assertRaisesMessage(TypeError, msg): |
| 63 |
await MyClass().async_view(request) |
| 64 |
with self.assertRaisesMessage(TypeError, msg): |
| 65 |
await MyClass().async_view(HttpRequestProxy(request)) |
| 66 |
|
| 67 |
def test_cache_control_decorator_http_request_proxy(self): |
| 68 |
class MyClass: |
| 69 |
@method_decorator(cache_control(a="b")) |
| 70 |
def a_view(self, request): |
| 71 |
return HttpResponse() |
| 72 |
|
| 73 |
request = HttpRequest() |
| 74 |
response = MyClass().a_view(HttpRequestProxy(request)) |
| 75 |
self.assertEqual(response.headers["Cache-Control"], "a=b") |
| 76 |
|
| 77 |
def test_cache_control_empty_decorator(self): |
| 78 |
@cache_control() |
| 79 |
def a_view(request): |
| 80 |
return HttpResponse() |
| 81 |
|
| 82 |
response = a_view(HttpRequest()) |
| 83 |
self.assertEqual(response.get("Cache-Control"), "") |
| 84 |
|
| 85 |
async def test_cache_control_empty_decorator_async_view(self): |
| 86 |
@cache_control() |
| 87 |
async def async_view(request): |
| 88 |
return HttpResponse() |
| 89 |
|
| 90 |
response = await async_view(HttpRequest()) |
| 91 |
self.assertEqual(response.get("Cache-Control"), "") |
| 92 |
|
| 93 |
def test_cache_control_full_decorator(self): |
| 94 |
@cache_control(max_age=123, private=True, public=True, custom=456) |
| 95 |
def a_view(request): |
| 96 |
return HttpResponse() |
| 97 |
|
| 98 |
response = a_view(HttpRequest()) |
| 99 |
cache_control_items = response.get("Cache-Control").split(", ") |
| 100 |
self.assertEqual( |
| 101 |
set(cache_control_items), {"max-age=123", "private", "public", "custom=456"} |
| 102 |
) |
| 103 |
|
| 104 |
async def test_cache_control_full_decorator_async_view(self): |
| 105 |
@cache_control(max_age=123, private=True, public=True, custom=456) |
| 106 |
async def async_view(request): |
| 107 |
return HttpResponse() |
| 108 |
|
| 109 |
response = await async_view(HttpRequest()) |
| 110 |
cache_control_items = response.get("Cache-Control").split(", ") |
| 111 |
self.assertEqual( |
| 112 |
set(cache_control_items), {"max-age=123", "private", "public", "custom=456"} |
| 113 |
) |
| 114 |
|
| 115 |
|
| 116 |
class CachePageDecoratorTest(SimpleTestCase): |
| 117 |
def test_cache_page(self): |
| 118 |
def my_view(request): |
| 119 |
return "response" |
| 120 |
|
| 121 |
my_view_cached = cache_page(123)(my_view) |
| 122 |
self.assertEqual(my_view_cached(HttpRequest()), "response") |
| 123 |
my_view_cached2 = cache_page(123, key_prefix="test")(my_view) |
| 124 |
self.assertEqual(my_view_cached2(HttpRequest()), "response") |
| 125 |
|
| 126 |
|
| 127 |
class NeverCacheDecoratorTest(SimpleTestCase): |
| 128 |
def test_wrapped_sync_function_is_not_coroutine_function(self): |
| 129 |
def sync_view(request): |
| 130 |
return HttpResponse() |
| 131 |
|
| 132 |
wrapped_view = never_cache(sync_view) |
| 133 |
self.assertIs(iscoroutinefunction(wrapped_view), False) |
| 134 |
|
| 135 |
def test_wrapped_async_function_is_coroutine_function(self): |
| 136 |
async def async_view(request): |
| 137 |
return HttpResponse() |
| 138 |
|
| 139 |
wrapped_view = never_cache(async_view) |
| 140 |
self.assertIs(iscoroutinefunction(wrapped_view), True) |
| 141 |
|
| 142 |
@mock.patch("time.time") |
| 143 |
def test_never_cache_decorator_headers(self, mocked_time): |
| 144 |
@never_cache |
| 145 |
def a_view(request): |
| 146 |
return HttpResponse() |
| 147 |
|
| 148 |
mocked_time.return_value = 1167616461.0 |
| 149 |
response = a_view(HttpRequest()) |
| 150 |
self.assertEqual( |
| 151 |
response.headers["Expires"], |
| 152 |
"Mon, 01 Jan 2007 01:54:21 GMT", |
| 153 |
) |
| 154 |
self.assertEqual( |
| 155 |
response.headers["Cache-Control"], |
| 156 |
"max-age=0, no-cache, no-store, must-revalidate, private", |
| 157 |
) |
| 158 |
|
| 159 |
@mock.patch("time.time") |
| 160 |
async def test_never_cache_decorator_headers_async_view(self, mocked_time): |
| 161 |
@never_cache |
| 162 |
async def async_view(request): |
| 163 |
return HttpResponse() |
| 164 |
|
| 165 |
mocked_time.return_value = 1167616461.0 |
| 166 |
response = await async_view(HttpRequest()) |
| 167 |
self.assertEqual(response.headers["Expires"], "Mon, 01 Jan 2007 01:54:21 GMT") |
| 168 |
self.assertEqual( |
| 169 |
response.headers["Cache-Control"], |
| 170 |
"max-age=0, no-cache, no-store, must-revalidate, private", |
| 171 |
) |
| 172 |
|
| 173 |
def test_never_cache_decorator_expires_not_overridden(self): |
| 174 |
@never_cache |
| 175 |
def a_view(request): |
| 176 |
return HttpResponse(headers={"Expires": "tomorrow"}) |
| 177 |
|
| 178 |
response = a_view(HttpRequest()) |
| 179 |
self.assertEqual(response.headers["Expires"], "tomorrow") |
| 180 |
|
| 181 |
async def test_never_cache_decorator_expires_not_overridden_async_view(self): |
| 182 |
@never_cache |
| 183 |
async def async_view(request): |
| 184 |
return HttpResponse(headers={"Expires": "tomorrow"}) |
| 185 |
|
| 186 |
response = await async_view(HttpRequest()) |
| 187 |
self.assertEqual(response.headers["Expires"], "tomorrow") |
| 188 |
|
| 189 |
def test_never_cache_decorator_http_request(self): |
| 190 |
class MyClass: |
| 191 |
@never_cache |
| 192 |
def a_view(self, request): |
| 193 |
return HttpResponse() |
| 194 |
|
| 195 |
request = HttpRequest() |
| 196 |
msg = ( |
| 197 |
"never_cache didn't receive an HttpRequest. If you are decorating " |
| 198 |
"a classmethod, be sure to use @method_decorator." |
| 199 |
) |
| 200 |
with self.assertRaisesMessage(TypeError, msg): |
| 201 |
MyClass().a_view(request) |
| 202 |
with self.assertRaisesMessage(TypeError, msg): |
| 203 |
MyClass().a_view(HttpRequestProxy(request)) |
| 204 |
|
| 205 |
async def test_never_cache_decorator_http_request_async_view(self): |
| 206 |
class MyClass: |
| 207 |
@never_cache |
| 208 |
async def async_view(self, request): |
| 209 |
return HttpResponse() |
| 210 |
|
| 211 |
request = HttpRequest() |
| 212 |
msg = ( |
| 213 |
"never_cache didn't receive an HttpRequest. If you are decorating " |
| 214 |
"a classmethod, be sure to use @method_decorator." |
| 215 |
) |
| 216 |
with self.assertRaisesMessage(TypeError, msg): |
| 217 |
await MyClass().async_view(request) |
| 218 |
with self.assertRaisesMessage(TypeError, msg): |
| 219 |
await MyClass().async_view(HttpRequestProxy(request)) |
| 220 |
|
| 221 |
def test_never_cache_decorator_http_request_proxy(self): |
| 222 |
class MyClass: |
| 223 |
@method_decorator(never_cache) |
| 224 |
def a_view(self, request): |
| 225 |
return HttpResponse() |
| 226 |
|
| 227 |
request = HttpRequest() |
| 228 |
response = MyClass().a_view(HttpRequestProxy(request)) |
| 229 |
self.assertIn("Cache-Control", response.headers) |
| 230 |
self.assertIn("Expires", response.headers) |