Esempio n. 1
0
    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_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)
Esempio n. 4
0
    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)
Esempio n. 5
0
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_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 cc(self):
     # Cache controller fixture
     return CacheController(Mock(), serializer=Mock())
 def setup(self):
     self.c = CacheController(DictCache(), serializer=NullSerializer())