| 223 | |
| 224 | |
| 225 | class TestCacheControlRequest: |
| 226 | url = "http://foo.com/bar" |
| 227 | |
| 228 | def setup_method(self): |
| 229 | self.c = CacheController(DictCache(), serializer=NullSerializer()) |
| 230 | |
| 231 | def req(self, headers): |
| 232 | mock_request = Mock(url=self.url, headers=headers) |
| 233 | return self.c.cached_request(mock_request) |
| 234 | |
| 235 | def test_cache_request_no_headers(self): |
| 236 | cached_resp = Mock( |
| 237 | headers={"ETag": "jfd9094r808", "Content-Length": 100}, status=200 |
| 238 | ) |
| 239 | self.c.cache = DictCache({self.url: cached_resp}) |
| 240 | resp = self.req({}) |
| 241 | assert not resp |
| 242 | |
| 243 | def test_cache_request_no_cache(self): |
| 244 | resp = self.req({"cache-control": "no-cache"}) |
| 245 | assert not resp |
| 246 | |
| 247 | def test_cache_request_pragma_no_cache(self): |
| 248 | resp = self.req({"pragma": "no-cache"}) |
| 249 | assert not resp |
| 250 | |
| 251 | def test_cache_request_no_store(self): |
| 252 | resp = self.req({"cache-control": "no-store"}) |
| 253 | assert not resp |
| 254 | |
| 255 | def test_cache_request_max_age_0(self): |
| 256 | resp = self.req({"cache-control": "max-age=0"}) |
| 257 | assert not resp |
| 258 | |
| 259 | def test_cache_request_not_in_cache(self): |
| 260 | resp = self.req({}) |
| 261 | assert not resp |
| 262 | |
| 263 | def test_cache_request_fresh_max_age(self): |
| 264 | now = time.strftime(TIME_FMT, time.gmtime()) |
| 265 | resp = Mock(headers={"cache-control": "max-age=3600", "date": now}, status=200) |
| 266 | |
| 267 | cache = DictCache({self.url: resp}) |
| 268 | self.c.cache = cache |
| 269 | r = self.req({}) |
| 270 | assert r == resp |
| 271 | |
| 272 | def test_cache_request_unfresh_max_age(self): |
| 273 | earlier = time.time() - 3700 # epoch - 1h01m40s |
| 274 | now = time.strftime(TIME_FMT, time.gmtime(earlier)) |
| 275 | resp = Mock(headers={"cache-control": "max-age=3600", "date": now}, status=200) |
| 276 | self.c.cache = DictCache({self.url: resp}) |
| 277 | r = self.req({}) |
| 278 | assert not r |
| 279 | |
| 280 | def test_cache_request_fresh_expires(self): |
| 281 | later = time.time() + 86400 # GMT + 1 day |
| 282 | expires = time.strftime(TIME_FMT, time.gmtime(later)) |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…