def test_update_from_response__disabled():
    """Response headers should not be used if cache_control=False"""
    url = 'https://img.site.com/base/img.jpg'
    headers = [('Cache-Control', 'max-age=60')]
    response = MagicMock(url=url,
                         headers=CIMultiDictProxy(CIMultiDict(headers)))

    actions = CacheActions(key='key', cache_control=False, expire_after=30)
    actions.update_from_response(response)
    assert actions.expire_after == 30
def test_update_from_response(headers, expected_expiration):
    """Test with Cache-Control response headers"""
    url = 'https://img.site.com/base/img.jpg'
    response = MagicMock(url=url,
                         headers=CIMultiDictProxy(CIMultiDict(headers)))
    actions = CacheActions(key='key', cache_control=True)
    actions.update_from_response(response)

    if expected_expiration == DO_NOT_CACHE:
        assert actions.skip_write is True
    else:
        assert actions.expire_after == expected_expiration
        assert actions.skip_write is False
Example #3
0
    async def save_response(self, response: ClientResponse,
                            actions: CacheActions):
        """Save a response to the cache

        Args:
            response: Response to save
            actions: Specific cache actions to take
        """
        actions.update_from_response(response)
        if not self.is_cacheable(response, actions):
            logger.debug(f'Not caching response for key: {actions.key}')
            return

        logger.debug(f'Saving response for key: {actions.key}')
        cached_response = await CachedResponse.from_client_response(
            response, actions.expires)
        await self.responses.write(actions.key, cached_response)

        # Alias any redirect requests to the same cache key
        for r in response.history:
            await self.redirects.write(self.create_key(r.method, r.url),
                                       actions.key)