1 # -*- coding: utf-8 -*-
6 from unittest.mock import Mock
8 from sima.lib.cache import DictCache
9 from sima.lib.http import CacheController
11 TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
14 class TestCacheControlRequest(unittest.TestCase):
15 url = 'http://foo.com/bar'
18 self.c = CacheController(
22 def req(self, headers):
23 return self.c.cached_request(Mock(url=self.url, headers=headers))
25 def test_cache_request_no_cache(self):
26 resp = self.req({'cache-control': 'no-cache'})
29 def test_cache_request_pragma_no_cache(self):
30 resp = self.req({'pragma': 'no-cache'})
33 def test_cache_request_no_store(self):
34 resp = self.req({'cache-control': 'no-store'})
37 def test_cache_request_max_age_0(self):
38 resp = self.req({'cache-control': 'max-age=0'})
41 def test_cache_request_not_in_cache(self):
45 def test_cache_request_fresh_max_age(self):
46 now = time.strftime(TIME_FMT, time.gmtime())
47 resp = Mock(headers={'cache-control': 'max-age=3600',
50 cache = DictCache({self.url: resp})
55 def test_cache_request_unfresh_max_age(self):
56 earlier = time.time() - 3700 # epoch - 1h01m40s
57 now = time.strftime(TIME_FMT, time.gmtime(earlier))
58 resp = Mock(headers={'cache-control': 'max-age=3600',
60 self.c.cache = DictCache({self.url: resp})
64 def test_cache_request_fresh_expires(self):
65 later = time.time() + 86400 # GMT + 1 day
66 expires = time.strftime(TIME_FMT, time.gmtime(later))
67 now = time.strftime(TIME_FMT, time.gmtime())
68 resp = Mock(headers={'expires': expires,
70 cache = DictCache({self.url: resp})
75 def test_cache_request_unfresh_expires(self):
76 sooner = time.time() - 86400 # GMT - 1 day
77 expires = time.strftime(TIME_FMT, time.gmtime(sooner))
78 now = time.strftime(TIME_FMT, time.gmtime())
79 resp = Mock(headers={'expires': expires,
81 cache = DictCache({self.url: resp})
87 # vim: ai ts=4 sw=4 sts=4 expandtab