|
4 | 4 |
|
5 | 5 | from tna_utilities.flask import ( |
6 | 6 | cacheable_duration, |
| 7 | + cacheable_duration_cloudfront, |
7 | 8 | do_not_cache, |
8 | 9 | set_cache_control, |
9 | 10 | vary_by_cookies, |
@@ -115,3 +116,59 @@ def index(): |
115 | 116 | rv.headers["Vary"], |
116 | 117 | "Accept-Encoding, User-Agent", |
117 | 118 | ) |
| 119 | + |
| 120 | + def test_cache_control_and_vary_by_headers_route(self): |
| 121 | + @self.app.route("/") |
| 122 | + @set_cache_control("private, max-age=120") |
| 123 | + @vary_by_headers("Accept-Encoding, User-Agent") |
| 124 | + def index(): |
| 125 | + return "OK" |
| 126 | + |
| 127 | + rv = self.test_client.get("/") |
| 128 | + |
| 129 | + self.assertEqual(rv.status_code, 200) |
| 130 | + self.assertIn("Vary", rv.headers) |
| 131 | + self.assertEqual( |
| 132 | + rv.headers["Vary"], |
| 133 | + "Accept-Encoding, User-Agent", |
| 134 | + ) |
| 135 | + self.assertIn("Cache-Control", rv.headers) |
| 136 | + self.assertEqual( |
| 137 | + rv.headers["Cache-Control"], |
| 138 | + "private, max-age=120", |
| 139 | + ) |
| 140 | + |
| 141 | + def test_cacheable_duration_cloudfront_route(self): |
| 142 | + @self.app.route("/") |
| 143 | + @cacheable_duration_cloudfront(client_seconds=60, cloudfront_seconds=120) |
| 144 | + def index(): |
| 145 | + return "OK" |
| 146 | + |
| 147 | + rv = self.test_client.get("/") |
| 148 | + |
| 149 | + self.assertEqual(rv.status_code, 200) |
| 150 | + self.assertIn("Cache-Control", rv.headers) |
| 151 | + self.assertEqual( |
| 152 | + rv.headers["Cache-Control"], |
| 153 | + "public, max-age=60, s-maxage=120", |
| 154 | + ) |
| 155 | + |
| 156 | + def test_cacheable_duration_cloudfront_route_extras(self): |
| 157 | + @self.app.route("/") |
| 158 | + @cacheable_duration_cloudfront( |
| 159 | + client_seconds=60, |
| 160 | + cloudfront_seconds=120, |
| 161 | + stale_while_revalidate_seconds=30, |
| 162 | + stale_if_error_seconds=15, |
| 163 | + ) |
| 164 | + def index(): |
| 165 | + return "OK" |
| 166 | + |
| 167 | + rv = self.test_client.get("/") |
| 168 | + |
| 169 | + self.assertEqual(rv.status_code, 200) |
| 170 | + self.assertIn("Cache-Control", rv.headers) |
| 171 | + self.assertEqual( |
| 172 | + rv.headers["Cache-Control"], |
| 173 | + "public, max-age=60, s-maxage=120, stale-while-revalidate=30, stale-if-error=15", |
| 174 | + ) |
0 commit comments