5
5
"""
6
6
Unit tests that verify our caching methods work correctly.
7
7
"""
8
- import pytest
9
- from unittest .mock import ANY , Mock
10
8
import time
11
9
from tempfile import mkdtemp
10
+ from unittest .mock import ANY , Mock
11
+
12
+ import pytest
12
13
13
14
from cachecontrol import CacheController
14
15
from cachecontrol .cache import DictCache
15
16
from cachecontrol .caches import SeparateBodyFileCache
16
- from .utils import NullSerializer , DummyResponse , DummyRequest
17
17
18
- TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
18
+ from . utils import DummyRequest , DummyResponse , NullSerializer
19
19
20
+ TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
20
21
21
22
22
23
class TestCacheControllerResponse (object ):
@@ -126,13 +127,16 @@ def test_update_cached_response_no_local_cache(self):
126
127
cache = DictCache ({})
127
128
cc = CacheController (cache )
128
129
req = DummyRequest (url = "http://localhost/" , headers = {"if-match" : "xyz" })
129
- resp = DummyResponse (status = 304 , headers = {
130
- "ETag" : "xyz" ,
131
- "x-value" : "b" ,
132
- "Date" : time .strftime (TIME_FMT , time .gmtime ()),
133
- "Cache-Control" : "max-age=60" ,
134
- "Content-Length" : "200"
135
- })
130
+ resp = DummyResponse (
131
+ status = 304 ,
132
+ headers = {
133
+ "ETag" : "xyz" ,
134
+ "x-value" : "b" ,
135
+ "Date" : time .strftime (TIME_FMT , time .gmtime ()),
136
+ "Cache-Control" : "max-age=60" ,
137
+ "Content-Length" : "200" ,
138
+ },
139
+ )
136
140
# First, ensure the response from update_cached_response() matches the
137
141
# cached one:
138
142
result = cc .update_cached_response (req , resp )
@@ -176,13 +180,16 @@ def update_cached_response_with_valid_headers_test(self, cache):
176
180
cc = CacheController (cache )
177
181
url = "http://localhost:123/x"
178
182
req = DummyRequest (url = url , headers = {})
179
- cached_resp = DummyResponse (status = 200 , headers = {
180
- "ETag" : etag ,
181
- "x-value:" : "a" ,
182
- "Content-Length" : "100" ,
183
- "Cache-Control" : "max-age=60" ,
184
- "Date" : time .strftime (TIME_FMT , time .gmtime ()),
185
- })
183
+ cached_resp = DummyResponse (
184
+ status = 200 ,
185
+ headers = {
186
+ "ETag" : etag ,
187
+ "x-value:" : "a" ,
188
+ "Content-Length" : "100" ,
189
+ "Cache-Control" : "max-age=60" ,
190
+ "Date" : time .strftime (TIME_FMT , time .gmtime ()),
191
+ },
192
+ )
186
193
cc ._cache_set (url , req , cached_resp , b"my body" )
187
194
188
195
# Now we get another request, and it's a 304, with new value for
@@ -191,13 +198,16 @@ def update_cached_response_with_valid_headers_test(self, cache):
191
198
# Set our content length to 200. That would be a mistake in
192
199
# the server, but we'll handle it gracefully... for now.
193
200
req = DummyRequest (url = url , headers = {"if-match" : etag })
194
- resp = DummyResponse (status = 304 , headers = {
195
- "ETag" : etag ,
196
- "x-value" : "b" ,
197
- "Date" : time .strftime (TIME_FMT , time .gmtime ()),
198
- "Cache-Control" : "max-age=60" ,
199
- "Content-Length" : "200"
200
- })
201
+ resp = DummyResponse (
202
+ status = 304 ,
203
+ headers = {
204
+ "ETag" : etag ,
205
+ "x-value" : "b" ,
206
+ "Date" : time .strftime (TIME_FMT , time .gmtime ()),
207
+ "Cache-Control" : "max-age=60" ,
208
+ "Content-Length" : "200" ,
209
+ },
210
+ )
201
211
# First, ensure the response from update_cached_response() matches the
202
212
# cached one:
203
213
result = cc .update_cached_response (req , resp )
@@ -214,15 +224,17 @@ def update_cached_response_with_valid_headers_test(self, cache):
214
224
class TestCacheControlRequest (object ):
215
225
url = "http://foo.com/bar"
216
226
217
- def setup (self ):
227
+ def setup_method (self ):
218
228
self .c = CacheController (DictCache (), serializer = NullSerializer ())
219
229
220
230
def req (self , headers ):
221
231
mock_request = Mock (url = self .url , headers = headers )
222
232
return self .c .cached_request (mock_request )
223
233
224
234
def test_cache_request_no_headers (self ):
225
- cached_resp = Mock (headers = {"ETag" : "jfd9094r808" , "Content-Length" : 100 }, status = 200 )
235
+ cached_resp = Mock (
236
+ headers = {"ETag" : "jfd9094r808" , "Content-Length" : 100 }, status = 200
237
+ )
226
238
self .c .cache = DictCache ({self .url : cached_resp })
227
239
resp = self .req ({})
228
240
assert not resp
0 commit comments