Exemple #1
0
 def test_CacheControl(self):
     headers = CIMultiDict()
     c = CacheControl()
     self.assertFalse(c.private)
     self.assertFalse(c.maxage)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'no-cache')
     c = CacheControl(maxage=3600)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, public')
     c = CacheControl(maxage=3600, private=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, private')
     c = CacheControl(maxage=3600, must_revalidate=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, public, must-revalidate')
     c = CacheControl(maxage=3600, proxy_revalidate=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, public, proxy-revalidate')
     c = CacheControl(maxage=3600, proxy_revalidate=True,
                      nostore=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'no-store, no-cache, must-revalidate, max-age=0')
Exemple #2
0
 def test_CacheControl(self):
     headers = CIMultiDict()
     c = CacheControl()
     self.assertFalse(c.private)
     self.assertFalse(c.maxage)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'no-cache')
     c = CacheControl(maxage=3600)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, public')
     c = CacheControl(maxage=3600, private=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, private')
     c = CacheControl(maxage=3600, must_revalidate=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, public, must-revalidate')
     c = CacheControl(maxage=3600, proxy_revalidate=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'max-age=3600, public, proxy-revalidate')
     c = CacheControl(maxage=3600, proxy_revalidate=True, nostore=True)
     c(headers)
     self.assertEqual(', '.join(headers.getall('cache-control')),
                      'no-store, no-cache, must-revalidate, max-age=0')
Exemple #3
0
    def _build_response(self,
                        url: 'Union[URL, str]',
                        method: str = hdrs.METH_GET,
                        request_headers: Dict = None,
                        status: int = 200,
                        body: str = '',
                        content_type: str = 'application/json',
                        payload: Dict = None,
                        headers: Dict = None,
                        response_class: 'ClientResponse' = None,
                        reason: Optional[str] = None) -> ClientResponse:
        if response_class is None:
            response_class = ClientResponse
        if payload is not None:
            body = json.dumps(payload)
        if not isinstance(body, bytes):
            body = str.encode(body)
        if request_headers is None:
            request_headers = {}
        kwargs = {}
        if AIOHTTP_VERSION >= StrictVersion('3.1.0'):
            loop = Mock()
            loop.get_debug = Mock()
            loop.get_debug.return_value = True
            kwargs['request_info'] = Mock(
                url=url,
                method=method,
                headers=CIMultiDictProxy(CIMultiDict(**request_headers)),
            )
            kwargs['writer'] = Mock()
            kwargs['continue100'] = None
            kwargs['timer'] = TimerNoop()
            if AIOHTTP_VERSION < StrictVersion('3.3.0'):
                kwargs['auto_decompress'] = True
            kwargs['traces'] = []
            kwargs['loop'] = loop
            kwargs['session'] = None
        else:
            loop = None
        # We need to initialize headers manually
        _headers = CIMultiDict({hdrs.CONTENT_TYPE: content_type})
        if headers:
            _headers.update(headers)
        raw_headers = self._build_raw_headers(_headers)
        resp = response_class(method, url, **kwargs)

        for hdr in _headers.getall(hdrs.SET_COOKIE, ()):
            resp.cookies.load(hdr)

        if AIOHTTP_VERSION >= StrictVersion('3.3.0'):
            # Reified attributes
            resp._headers = _headers
            resp._raw_headers = raw_headers
        else:
            resp.headers = _headers
            resp.raw_headers = raw_headers
        resp.status = status
        resp.reason = reason
        resp.content = stream_reader_factory(loop)
        resp.content.feed_data(body)
        resp.content.feed_eof()
        return resp
Exemple #4
0
def has_cache_headers(headers: Mapping) -> bool:
    """Determine if headers contain cache directives **that we currently support**"""
    ci_headers = CIMultiDict(headers)
    cache_control = ','.join(ci_headers.getall('Cache-Control', []))
    return any([d in cache_control for d in CACHE_DIRECTIVES] + [bool(headers.get('Expires'))])