Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_no_headers(self):
cached_resp = Mock(headers={"ETag": "jfd9094r808", "Content-Length": 100})
self.c.cache = DictCache({self.url: cached_resp})
resp = self.req({})
assert not 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 make_session(cache_etags=True, serializer=None, heuristic=None):
session = requests.Session()
adapter = CacheControlAdapter(
DictCache(),
cache_etags=cache_etags,
serializer=serializer,
heuristic=heuristic,
)
session.mount('http://', adapter)
session.mount('https://', adapter)
session.cache_controller = adapter.controller
session.headers.update({'User-Agent': USER_AGENT, 'Accept-Encoding': 'gzip,deflate'})
return session
def get_session():
adapter = CacheControlAdapter(
DictCache(), cache_etags=True, serializer=None, heuristic=None
)
sess = requests.Session()
sess.mount("http://", adapter)
sess.mount("https://", adapter)
sess.cache_controller = adapter.controller
return sess
def __init__(self, cache=None, cache_etags=True, controller_class=None,
serializer=None, heuristic=None):
self.cache = cache or DictCache()
self.heuristic = heuristic
controller_factory = controller_class or CacheController
self.controller = controller_factory(
self.cache,
cache_etags=cache_etags,
serializer=serializer,
)
def __init__(
self, cache=None, cache_etags=True, serializer=None, status_codes=None
):
self.cache = DictCache() if cache is None else cache
self.cache_etags = cache_etags
self.serializer = serializer or Serializer()
self.cacheable_status_codes = status_codes or (200, 203, 300, 301)