1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
# -*- coding: utf-8 -*-
import unittest
import time
from unittest.mock import Mock
from sima.lib.cache import DictCache
from sima.lib.http import CacheController
TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
class TestCacheControlRequest(unittest.TestCase):
url = 'http://foo.com/bar'
def setUp(self):
self.c = CacheController(
DictCache(),
)
def req(self, headers):
return self.c.cached_request(Mock(url=self.url, headers=headers))
def test_cache_request_no_cache(self):
resp = self.req({'cache-control': 'no-cache'})
assert not resp
def test_cache_request_pragma_no_cache(self):
resp = self.req({'pragma': 'no-cache'})
assert not resp
def test_cache_request_no_store(self):
resp = self.req({'cache-control': 'no-store'})
assert not resp
def test_cache_request_max_age_0(self):
resp = self.req({'cache-control': 'max-age=0'})
assert not resp
def test_cache_request_not_in_cache(self):
resp = self.req({})
assert not resp
def test_cache_request_fresh_max_age(self):
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'cache-control': 'max-age=3600',
'date': now})
cache = DictCache({self.url: resp})
self.c.cache = cache
r = self.req({})
assert r == resp
def test_cache_request_unfresh_max_age(self):
earlier = time.time() - 3700 # epoch - 1h01m40s
now = time.strftime(TIME_FMT, time.gmtime(earlier))
resp = Mock(headers={'cache-control': 'max-age=3600',
'date': now})
self.c.cache = DictCache({self.url: resp})
r = self.req({})
assert not r
def test_cache_request_fresh_expires(self):
later = time.time() + 86400 # GMT + 1 day
expires = time.strftime(TIME_FMT, time.gmtime(later))
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'expires': expires,
'date': now})
cache = DictCache({self.url: resp})
self.c.cache = cache
r = self.req({})
assert r == resp
def test_cache_request_unfresh_expires(self):
sooner = time.time() - 86400 # GMT - 1 day
expires = time.strftime(TIME_FMT, time.gmtime(sooner))
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'expires': expires,
'date': now})
cache = DictCache({self.url: resp})
self.c.cache = cache
r = self.req({})
assert not r
# VIM MODLINE
# vim: ai ts=4 sw=4 sts=4 expandtab
|