def test_init_from_request(
    get_url_expiration,
    request_expire_after,
    url_expire_after,
    header_expire_after,
    expected_expiration,
):
    """Test precedence with various combinations or per-request, per-session, per-URL, and
    Cache-Control expiration
    """
    url = 'https://img.site.com/base/img.jpg'
    get_url_expiration.return_value = url_expire_after
    headers = {
        'Cache-Control': f'max-age={header_expire_after}'
    } if header_expire_after else {}

    actions = CacheActions.from_request(
        key='key',
        url=URL(url),
        request_expire_after=request_expire_after,
        session_expire_after=1,
        cache_control=True,
        headers=headers,
    )
    assert actions.expire_after == expected_expiration
Example #2
0
    async def request(
        self,
        method: str,
        url: StrOrURL,
        expire_after: ExpirationTime = None,
        **kwargs,
    ) -> Tuple[Optional[CachedResponse], CacheActions]:
        """Fetch a cached response based on request info

        Args:
            method: HTTP method
            url: Request URL
            expire_after: Expiration time to set only for this request; overrides
                ``CachedSession.expire_after``, and accepts all the same values.
            kwargs: All other request arguments
        """
        key = self.create_key(method, url, **kwargs)
        actions = CacheActions.from_request(
            key,
            url=url,
            request_expire_after=expire_after,
            session_expire_after=self.expire_after,
            urls_expire_after=self.urls_expire_after,
            cache_control=self.cache_control,
            **kwargs,
        )

        # Skip reading from the cache, if specified by request headers
        response = None if actions.skip_read else await self.get_response(
            actions.key)
        return response, actions
def test_ignored_headers(directive):
    """Ensure that currently unimplemented Cache-Control headers do not affect behavior"""
    url = 'https://img.site.com/base/img.jpg'
    headers = {'Cache-Control': directive}
    # expiration = get_expiration(response, session_expire_after=1, cache_control=True)
    actions = CacheActions.from_request(
        key='key',
        url=URL(url),
        session_expire_after=1,
        cache_control=True,
        headers=headers,
    )
    assert actions.expire_after == 1