def test_update_cached_response_with_valid_headers(self): cached_resp = Mock(headers={ 'ETag': 'jfd9094r808', 'Content-Length': 100 }) # Set our content length to 200. That would be a mistake in # the server, but we'll handle it gracefully... for now. resp = Mock(headers={'ETag': '28371947465', 'Content-Length': 200}) cache = DictCache({self.url: cached_resp}) cc = CacheController(cache) # skip our in/out processing cc.serializer = Mock() cc.serializer.loads.return_value = cached_resp cc.cache_url = Mock(return_value='http://foo.com') req_headers = {} request = type('Request', (object, ), { 'headers': req_headers, 'url': 'http://example.com' })() result = cc.update_cached_response(request, resp) assert result.headers['ETag'] == resp.headers['ETag'] assert result.headers['Content-Length'] == 100
def get_public_keys(self) -> str: if (self._jwks_token is None or self._jwks_expires_at is None or self._jwks_expires_at <= datetime.now()): resp = requests.get(self.public_key_url()) cache_control = CacheController().parse_cache_control(resp.headers) max_age = cache_control.get("max-age", 0) self._jwks_expires_at = datetime.now() + timedelta( seconds=float(max_age)) # type: ignore self._jwks_token = resp.json() return self._jwks_token # type: ignore
def test_cache_repsonse_no_store(self): resp = Mock() cache = DictCache({self.url: resp}) cc = CacheController(cache) cache_url = cc.cache_url(self.url) resp = self.resp({'cache-control': 'no-store'}) assert cc.cache.get(cache_url) cc.cache_response(self.req(), resp) assert not cc.cache.get(cache_url)
def test_cache_response_no_store(self): resp = Mock() cache = DictCache({self.url: resp}) cc = CacheController(cache) cache_url = cc.cache_url(self.url) resp = self.resp({"cache-control": "no-store"}) assert cc.cache.get(cache_url) cc.cache_response(self.req(), resp) assert not cc.cache.get(cache_url)
class TestCacheVaryByAuthorization(object): url = 'http://foo.com/bar' def setup(self): self.c = CacheController( DictCache(), serializer=NullSerializer(), ) def req(self, headers): mock_request = Mock(url=self.url, headers=headers) return self.c.cached_request(mock_request) def test_uses_auth_token_to_lookup_response(self): req_headers = {'Authorization': 'Bearer some-token'} resp_headers = {'Vary': 'Authorization'} request = type('Request', (object, ), { 'headers': req_headers, 'url': self.url })() expected_key = CacheController.cache_key(request) response = Mock(headers=resp_headers, status=301) # we set the status to 301 so that it is immediately returned without performing any etag/date checks to see if # cache eviction is required self.c.cache = DictCache({expected_key: response}) assert self.req(headers=req_headers) == response
def test_cache_response_no_store(self): resp = Mock() cache = DictCache({self.url: resp}) cc = CacheController(cache) request = type('Request', (object, ), { 'headers': {}, 'url': self.url })() cache_key = cc.cache_key(request) resp = self.resp({'cache-control': 'no-store'}) assert cc.cache.get(cache_key) cc.cache_response(self.req(), resp) assert not cc.cache.get(cache_key)
def test_update_cached_response_with_valid_headers(self): cached_resp = Mock(headers={'ETag': 'jfd9094r808', 'Content-Length': 100}) # Set our content length to 200. That would be a mistake in # the server, but we'll handle it gracefully... for now. resp = Mock(headers={'ETag': '28371947465', 'Content-Length': 200}) cache = DictCache({self.url: cached_resp}) cc = CacheController(cache) # skip our in/out processing cc.serializer = Mock() cc.serializer.loads.return_value = cached_resp cc.cache_url = Mock(return_value='http://foo.com') result = cc.update_cached_response(Mock(), resp) assert result.headers['ETag'] == resp.headers['ETag'] assert result.headers['Content-Length'] == 100
def test_update_cached_response_with_valid_headers(self): cached_resp = Mock(headers={"ETag": "jfd9094r808", "Content-Length": 100}) # Set our content length to 200. That would be a mistake in # the server, but we'll handle it gracefully... for now. resp = Mock(headers={"ETag": "28371947465", "Content-Length": 200}) cache = DictCache({self.url: cached_resp}) cc = CacheController(cache) # skip our in/out processing cc.serializer = Mock() cc.serializer.loads.return_value = cached_resp cc.cache_url = Mock(return_value="http://foo.com") result = cc.update_cached_response(Mock(), resp) assert result.headers["ETag"] == resp.headers["ETag"] assert result.headers["Content-Length"] == 100
def _build_session(): """Builds a requests session that caches responses where possible, making redirects faster. Returns: requests.Session -- A shared session to use for the notebook """ result = requests.session() # Set up caching. Particularly obey and cache 307 redirects to avoid duplicate expensive calls when we already # have a result cache_adapter = CacheControlAdapter() cache_adapter.controller = CacheController(cache=cache_adapter.cache, status_codes=(200, 203, 300, 301, 307)) result.mount('http://', cache_adapter) result.mount('https://', cache_adapter) return result
def test_uses_auth_token_to_lookup_response(self): req_headers = {'Authorization': 'Bearer some-token'} resp_headers = {'Vary': 'Authorization'} request = type('Request', (object, ), { 'headers': req_headers, 'url': self.url })() expected_key = CacheController.cache_key(request) response = Mock(headers=resp_headers, status=301) # we set the status to 301 so that it is immediately returned without performing any etag/date checks to see if # cache eviction is required self.c.cache = DictCache({expected_key: response}) assert self.req(headers=req_headers) == response
class TestCacheControlRequest(object): url = "http://foo.com/bar" def setup(self): self.c = CacheController(DictCache(), serializer=NullSerializer()) def req(self, headers): mock_request = Mock(url=self.url, headers=headers) return self.c.cached_request(mock_request) 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_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 def test_cached_request_with_bad_max_age_headers_not_returned(self): now = time.strftime(TIME_FMT, time.gmtime()) # Not a valid header; this would be from a misconfigured server resp = Mock(headers={"cache-control": "max-age=xxx", "date": now}) self.c.cache = DictCache({self.url: resp}) assert not self.req({})
class TestCacheControlRequest(object): url = 'http://foo.com/bar' def setup(self): self.c = CacheController( DictCache(), serializer=NullSerializer(), ) def req(self, headers): mock_request = Mock(url=self.url, headers=headers) return self.c.cached_request(mock_request) 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_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 def test_cached_request_with_bad_max_age_headers_not_returned(self): now = time.strftime(TIME_FMT, time.gmtime()) # Not a valid header; this would be from a misconfigured server resp = Mock(headers={'cache-control': 'max-age=xxx', 'date': now}) self.c.cache = DictCache({self.url: resp}) assert not self.req({})
class TestCacheControlRequest(object): url = 'http://foo.com/bar' def setup(self): self.c = CacheController( DictCache(), serializer=NullSerializer(), ) def req(self, headers): mock_request = Mock(url=self.url, headers=headers) return self.c.cached_request(mock_request) 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_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
class TestCacheControlRequest(object): url = 'http://foo.com/bar' def setup(self): self.c = CacheController( DictCache(), serializer=NullSerializer(), ) def req(self, headers): mock_request = Mock(url=self.url, headers=headers) return self.c.cached_request(mock_request) 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_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 def test_cached_request_with_bad_max_age_headers_not_returned(self): now = time.strftime(TIME_FMT, time.gmtime()) # Not a valid header; this would be from a misconfigured server resp = Mock(headers={'cache-control': 'max-age=xxx', 'date': now}) self.c.cache = DictCache({self.url: resp}) assert not self.req({}) def test_cached_request_with_0_max_age_not_returned(self): self.c.cache = DictCache({self.url: 1000000}) assert self.req({'cache-control': 'max-age=0'}) is False def test_cached_request_without_date_and_etags_is_evicted(self): resp = Mock(headers={'xx': 'this-value-is-unimportant'}) self.c.cache = DictCache({self.url: resp}) assert self.c.cache.get(self.url) is not None assert self.req({}) is False assert self.c.cache.get(self.url) is None
def setup(self): self.c = CacheController( DictCache(), serializer=NullSerializer(), )
class TestCacheControlRequest(object): url = 'http://foo.com/bar' def setup(self): self.c = CacheController( DictCache(), serializer=NullSerializer(), ) 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
def setup(self): self.c = CacheController(DictCache())
def setup(self): self.c = CacheController(DictCache(), serializer=NullSerializer())
def cc(self): # Cache controller fixture return CacheController(Mock(), serializer=Mock())
class TestCacheControlRequest(object): url = 'http://foo.com/bar' def setup(self): self.c = CacheController(DictCache()) def req(self, headers): return self.c.cached_request(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 = datetime.datetime.utcnow().strftime(TIME_FMT) 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 now = datetime.datetime.fromtimestamp(earlier).strftime(TIME_FMT) 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 = datetime.timedelta(days=1) expires = (datetime.datetime.utcnow() + later).strftime(TIME_FMT) now = datetime.datetime.utcnow().strftime(TIME_FMT) 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): later = datetime.timedelta(days=-1) expires = (datetime.datetime.utcnow() + later).strftime(TIME_FMT) now = datetime.datetime.utcnow().strftime(TIME_FMT) resp = Mock(headers={'expires': expires, 'date': now}) cache = DictCache({self.url: resp}) self.c.cache = cache r = self.req({}) assert not r