-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtest_cache_control.py
More file actions
256 lines (200 loc) · 8.64 KB
/
test_cache_control.py
File metadata and controls
256 lines (200 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
Unit tests that verify our caching methods work correctly.
"""
import pytest
from mock import ANY, Mock
import time
from random import shuffle
import string
from cachecontrol import CacheController
from cachecontrol.cache import DictCache
TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
class NullSerializer(object):
def dumps(self, request, response):
return response
def loads(self, request, data):
return data
class TestCacheControllerResponse(object):
url = 'http://url.com/'
def req(self, headers=None):
headers = headers or {}
return Mock(full_url=self.url, # < 1.x support
url=self.url,
headers=headers)
def resp(self, headers=None):
headers = headers or {}
return Mock(status=200,
headers=headers,
request=self.req(),
read=lambda **k: b"testing")
@pytest.fixture()
def cc(self):
# Cache controller fixture
return CacheController(Mock(), serializer=Mock())
def test_no_cache_non_20x_response(self, cc):
# No caching without some extra headers, so we add them
now = time.strftime(TIME_FMT, time.gmtime())
resp = self.resp({'cache-control': 'max-age=3600',
'date': now})
no_cache_codes = [201, 400, 500]
for code in no_cache_codes:
resp.status = code
cc.cache_response(Mock(), resp)
assert not cc.cache.set.called
# this should work b/c the resp is 20x
resp.status = 203
cc.cache_response(self.req(), resp)
assert cc.serializer.dumps.called
assert cc.cache.set.called
def test_no_cache_with_no_date(self, cc):
# No date header which makes our max-age pointless
resp = self.resp({'cache-control': 'max-age=3600'})
cc.cache_response(self.req(), resp)
assert not cc.cache.set.called
def test_no_cache_with_wrong_sized_body(self, cc):
# When the body is the wrong size, then we don't want to cache it
# because it is obviously broken.
resp = self.resp({
"cache-control": "max-age=3600",
"Content-Length": "5",
})
cc.cache_response(self.req(), resp, body=b"0" * 10)
assert not cc.cache.set.called
def test_cache_response_no_cache_control(self, cc):
resp = self.resp()
cc.cache_response(self.req(), resp)
assert not cc.cache.set.called
def test_cache_response_cache_max_age(self, cc):
now = time.strftime(TIME_FMT, time.gmtime())
resp = self.resp({'cache-control': 'max-age=3600',
'date': now})
req = self.req()
cc.cache_response(req, resp)
cc.serializer.dumps.assert_called_with(req, resp, body=None)
cc.cache.set.assert_called_with(self.url, ANY)
def test_cache_response_cache_max_age_with_invalid_value_not_cached(self, cc):
now = time.strftime(TIME_FMT, time.gmtime())
# Not a valid header; this would be from a misconfigured server
resp = self.resp({'cache-control': 'max-age=3600; public',
'date': now})
cc.cache_response(self.req(), resp)
assert not cc.cache.set.called
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)
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
class TestCacheControlRequest(object):
url = 'http://foo.com/bar'
def setup(self):
self.c = CacheController(
DictCache(),
serializer=NullSerializer(),
)
def req(self, headers):
mock_request = Mock(url=self.url, headers=headers)
return self.c.cached_request(mock_request)
def test_cache_request_no_headers(self):
cached_resp = Mock(headers={'ETag': 'jfd9094r808', 'Content-Length': 100})
self.c.cache = DictCache({self.url: cached_resp})
resp = self.req({})
assert not resp
def test_cache_request_no_cache(self):
resp = self.req({'cache-control': 'no-cache'})
assert not resp
def test_cache_request_pragma_no_cache(self):
resp = self.req({'pragma': 'no-cache'})
assert not resp
def test_cache_request_no_store(self):
resp = self.req({'cache-control': 'no-store'})
assert not resp
def test_cache_request_max_age_0(self):
resp = self.req({'cache-control': 'max-age=0'})
assert not resp
def test_cache_request_not_in_cache(self):
resp = self.req({})
assert not resp
def test_cache_request_fresh_max_age(self):
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'cache-control': 'max-age=3600',
'date': now})
cache = DictCache({self.url: resp})
self.c.cache = cache
r = self.req({})
assert r == resp
def test_cache_request_unfresh_max_age(self):
earlier = time.time() - 3700 # epoch - 1h01m40s
now = time.strftime(TIME_FMT, time.gmtime(earlier))
resp = Mock(headers={'cache-control': 'max-age=3600',
'date': now})
self.c.cache = DictCache({self.url: resp})
r = self.req({})
assert not r
def test_cache_request_fresh_expires(self):
later = time.time() + 86400 # GMT + 1 day
expires = time.strftime(TIME_FMT, time.gmtime(later))
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'expires': expires,
'date': now})
cache = DictCache({self.url: resp})
self.c.cache = cache
r = self.req({})
assert r == resp
def test_cache_request_unfresh_expires(self):
sooner = time.time() - 86400 # GMT - 1 day
expires = time.strftime(TIME_FMT, time.gmtime(sooner))
now = time.strftime(TIME_FMT, time.gmtime())
resp = Mock(headers={'expires': expires,
'date': now})
cache = DictCache({self.url: resp})
self.c.cache = cache
r = self.req({})
assert not r
def test_cached_request_with_bad_max_age_headers_not_returned(self):
now = time.strftime(TIME_FMT, time.gmtime())
# Not a valid header; this would be from a misconfigured server
resp = Mock(headers={'cache-control': 'max-age=xxx',
'date': now})
self.c.cache = DictCache({self.url: resp})
assert not self.req({})
def test_cache_url_sorting():
letter_n_numbers = list(enumerate(string.ascii_lowercase[3:], start=4))
suff = '&' + '&'.join('%s=%s' % (k, v) for v, k in letter_n_numbers)
def get_param(url):
"""Mock losing order when processing params"""
shuffle(letter_n_numbers)
params = {k: v for v, k in letter_n_numbers}
url = url.replace(suff, '')
query = '&' + '&'.join('%s=%s' % item for item in params.items())
return url + query
no_query = 'http://example.com'
unsorted_query = 'http://example.com?b=2&c=3&a=1' + suff
sorted_query = 'http://example.com?a=1&b=2&c=3' + suff
cache_url = CacheController.cache_url
assert cache_url(no_query, sort_query=True) == cache_url(no_query)
assert cache_url(unsorted_query) != cache_url(sorted_query)
assert cache_url(unsorted_query, True) == cache_url(sorted_query)
randomized = get_param(unsorted_query)
assert randomized != unsorted_query
assert cache_url(randomized) != cache_url(sorted_query)
assert cache_url(randomized, True) == cache_url(sorted_query)
randomized_again = get_param(unsorted_query)
assert cache_url(randomized, True) == cache_url(randomized_again, True)