| 
2 | 2 | Unit tests that verify our caching methods work correctly.  | 
3 | 3 | """  | 
4 | 4 | import pytest  | 
5 |  | -from mock import ANY, Mock  | 
 | 5 | +from mock import ANY, Mock, patch  | 
6 | 6 | import time  | 
7 | 7 | 
 
  | 
8 | 8 | from cachecontrol import CacheController  | 
@@ -115,6 +115,44 @@ def test_cache_response_no_store(self):  | 
115 | 115 |         cc.cache_response(self.req(), resp)  | 
116 | 116 |         assert not cc.cache.get(cache_url)  | 
117 | 117 | 
 
  | 
 | 118 | +    def test_cache_response_private_with_shared_cache(self):  | 
 | 119 | +        '''  | 
 | 120 | +        When a cache store is shared, a private directive should turn off caching.  | 
 | 121 | +
  | 
 | 122 | +        In this example, the etag is set, which should trigger a  | 
 | 123 | +        cache, but since the private directive is set and the cache is  | 
 | 124 | +        considered shared, we should not cache.  | 
 | 125 | +        '''  | 
 | 126 | +        resp = Mock()  | 
 | 127 | +        cache = DictCache(shared=True)  | 
 | 128 | +        cc = CacheController(cache)  | 
 | 129 | + | 
 | 130 | +        cache_url = cc.cache_url(self.url)  | 
 | 131 | + | 
 | 132 | +        resp = self.resp({'cache-control': 'max-age=3600, private'})  | 
 | 133 | + | 
 | 134 | +        cc.cache_response(self.req(), resp)  | 
 | 135 | +        assert not cc.cache.get(cache_url)  | 
 | 136 | + | 
 | 137 | +    def test_cache_response_private_with_legacy_cache(self):  | 
 | 138 | +        # Not all cache objects will have the "shared" attribute.  | 
 | 139 | +        resp = Mock()  | 
 | 140 | +        cache = Mock()  | 
 | 141 | +        cache.shared.side_effect = AttributeError  | 
 | 142 | +        cc = CacheController(cache)  | 
 | 143 | +        cc.serializer = Mock()  | 
 | 144 | + | 
 | 145 | +        cache_url = cc.cache_url(self.url)  | 
 | 146 | + | 
 | 147 | +        now = time.strftime(TIME_FMT, time.gmtime())  | 
 | 148 | +        resp = self.resp({  | 
 | 149 | +            'cache-control': 'max-age=3600, private',  | 
 | 150 | +            'date': now,  | 
 | 151 | +        })  | 
 | 152 | + | 
 | 153 | +        cc.cache_response(self.req(), resp)  | 
 | 154 | +        assert cc.cache.set.called  | 
 | 155 | + | 
118 | 156 |     def test_update_cached_response_with_valid_headers(self):  | 
119 | 157 |         cached_resp = Mock(headers={'ETag': 'jfd9094r808', 'Content-Length': 100})  | 
120 | 158 | 
 
  | 
 | 
0 commit comments