55"""
66Unit tests that verify our caching methods work correctly.
77"""
8- import pytest
9- from unittest .mock import ANY , Mock
108import time
119from tempfile import mkdtemp
10+ from unittest .mock import ANY , Mock
11+
12+ import pytest
1213
1314from cachecontrol import CacheController
1415from cachecontrol .cache import DictCache
1516from cachecontrol .caches import SeparateBodyFileCache
16- from .utils import NullSerializer , DummyResponse , DummyRequest
1717
18- TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
18+ from . utils import DummyRequest , DummyResponse , NullSerializer
1919
20+ TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
2021
2122
2223class TestCacheControllerResponse (object ):
@@ -126,13 +127,16 @@ def test_update_cached_response_no_local_cache(self):
126127 cache = DictCache ({})
127128 cc = CacheController (cache )
128129 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+ )
136140 # First, ensure the response from update_cached_response() matches the
137141 # cached one:
138142 result = cc .update_cached_response (req , resp )
@@ -176,13 +180,16 @@ def update_cached_response_with_valid_headers_test(self, cache):
176180 cc = CacheController (cache )
177181 url = "http://localhost:123/x"
178182 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+ )
186193 cc ._cache_set (url , req , cached_resp , b"my body" )
187194
188195 # 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):
191198 # Set our content length to 200. That would be a mistake in
192199 # the server, but we'll handle it gracefully... for now.
193200 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+ )
201211 # First, ensure the response from update_cached_response() matches the
202212 # cached one:
203213 result = cc .update_cached_response (req , resp )
@@ -214,15 +224,17 @@ def update_cached_response_with_valid_headers_test(self, cache):
214224class TestCacheControlRequest (object ):
215225 url = "http://foo.com/bar"
216226
217- def setup (self ):
227+ def setup_method (self ):
218228 self .c = CacheController (DictCache (), serializer = NullSerializer ())
219229
220230 def req (self , headers ):
221231 mock_request = Mock (url = self .url , headers = headers )
222232 return self .c .cached_request (mock_request )
223233
224234 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+ )
226238 self .c .cache = DictCache ({self .url : cached_resp })
227239 resp = self .req ({})
228240 assert not resp
0 commit comments