Skip to content

Commit 77b3b37

Browse files
committed
fix: compatibility with urllib3 2.0
Signed-off-by: Frost Ming <[email protected]>
1 parent c05ef9e commit 77b3b37

8 files changed

+66
-67
lines changed

cachecontrol/serialize.py

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def dumps(self, request, response, body=None):
5151
u"status": response.status,
5252
u"version": response.version,
5353
u"reason": text_type(response.reason),
54-
u"strict": response.strict,
5554
u"decode_content": response.decode_content,
5655
}
5756
}

tests/test_cache_control.py

+39-27
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
"""
66
Unit tests that verify our caching methods work correctly.
77
"""
8-
import pytest
9-
from mock import ANY, Mock
108
import time
119
from tempfile import mkdtemp
1210

11+
import pytest
12+
from mock import ANY, Mock
13+
1314
from cachecontrol import CacheController
1415
from cachecontrol.cache import DictCache
1516
from 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

2223
class 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):
214224
class 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

tests/test_etag.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import pytest
6-
7-
from mock import Mock, patch
8-
96
import requests
7+
from mock import Mock, patch
108

119
from cachecontrol import CacheControl
1210
from cachecontrol.cache import DictCache
1311
from cachecontrol.compat import urljoin
12+
1413
from .utils import NullSerializer
1514

1615

@@ -136,7 +135,7 @@ def test_not_modified_releases_connection(self, server, url):
136135

137136
# This is how the urllib3 response is created in
138137
# requests.adapters
139-
response_mod = "requests.adapters.HTTPResponse.from_httplib"
138+
response_mod = "urllib3.HTTPConnectionPool.urlopen"
140139

141140
with patch(response_mod, Mock(return_value=resp)):
142141
sess.get(etag_url)

tests/test_expires_heuristics.py

+17-24
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44

55
import calendar
66
import time
7-
8-
from email.utils import formatdate, parsedate
97
from datetime import datetime
8+
from email.utils import formatdate, parsedate
9+
from pprint import pprint
1010

1111
from mock import Mock
1212
from requests import Session, get
1313

1414
from cachecontrol import CacheControl
15-
from cachecontrol.heuristics import LastModified, ExpiresAfter, OneDayCache
16-
from cachecontrol.heuristics import TIME_FMT
17-
from cachecontrol.heuristics import BaseHeuristic
18-
from .utils import DummyResponse
15+
from cachecontrol.heuristics import (
16+
TIME_FMT,
17+
BaseHeuristic,
18+
ExpiresAfter,
19+
LastModified,
20+
OneDayCache,
21+
)
1922

20-
from pprint import pprint
23+
from .utils import DummyResponse
2124

2225

2326
class TestHeuristicWithoutWarning(object):
24-
25-
def setup(self):
26-
27+
def setup_method(self):
2728
class NoopHeuristic(BaseHeuristic):
2829
warning = Mock()
2930

@@ -35,17 +36,14 @@ def update_headers(self, resp):
3536

3637
def test_no_header_change_means_no_warning_header(self, url):
3738
the_url = url + "optional_cacheable_request"
38-
resp = self.sess.get(the_url)
39+
self.sess.get(the_url)
3940

4041
assert not self.heuristic.warning.called
4142

4243

4344
class TestHeuristicWith3xxResponse(object):
44-
45-
def setup(self):
46-
45+
def setup_method(self):
4746
class DummyHeuristic(BaseHeuristic):
48-
4947
def update_headers(self, resp):
5048
return {"x-dummy-header": "foobar"}
5149

@@ -63,16 +61,14 @@ def test_heuristic_applies_to_304(self, url):
6361

6462

6563
class TestUseExpiresHeuristic(object):
66-
6764
def test_expires_heuristic_arg(self):
6865
sess = Session()
6966
cached_sess = CacheControl(sess, heuristic=Mock())
7067
assert cached_sess
7168

7269

7370
class TestOneDayCache(object):
74-
75-
def setup(self):
71+
def setup_method(self):
7672
self.sess = Session()
7773
self.cached_sess = CacheControl(self.sess, heuristic=OneDayCache())
7874

@@ -91,8 +87,7 @@ def test_cache_for_one_day(self, url):
9187

9288

9389
class TestExpiresAfter(object):
94-
95-
def setup(self):
90+
def setup_method(self):
9691
self.sess = Session()
9792
self.cache_sess = CacheControl(self.sess, heuristic=ExpiresAfter(days=1))
9893

@@ -112,8 +107,7 @@ def test_expires_after_one_day(self, url):
112107

113108

114109
class TestLastModified(object):
115-
116-
def setup(self):
110+
def setup_method(self):
117111
self.sess = Session()
118112
self.cached_sess = CacheControl(self.sess, heuristic=LastModified())
119113

@@ -136,11 +130,10 @@ def datetime_to_header(dt):
136130

137131

138132
class TestModifiedUnitTests(object):
139-
140133
def last_modified(self, period):
141134
return time.strftime(TIME_FMT, time.gmtime(self.time_now - period))
142135

143-
def setup(self):
136+
def setup_method(self):
144137
self.heuristic = LastModified()
145138
self.time_now = time.time()
146139
day_in_seconds = 86400

tests/test_redirects.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212

1313
class TestPermanentRedirects(object):
14-
15-
def setup(self):
14+
def setup_method(self):
1615
self.sess = CacheControl(requests.Session())
1716

1817
def test_redirect_response_is_cached(self, url):
@@ -33,8 +32,7 @@ def test_bust_cache_on_redirect(self, url):
3332

3433

3534
class TestMultipleChoicesRedirects(object):
36-
37-
def setup(self):
35+
def setup_method(self):
3836
self.sess = CacheControl(requests.Session())
3937

4038
def test_multiple_choices_is_cacheable(self, url):

tests/test_serialization.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class TestSerializer(object):
15-
def setup(self):
15+
def setup_method(self):
1616
self.serializer = Serializer()
1717
self.response_data = {
1818
u"response": {
@@ -27,7 +27,6 @@ def setup(self):
2727
u"status": 200,
2828
u"version": 11,
2929
u"reason": u"",
30-
u"strict": True,
3130
u"decode_content": True,
3231
}
3332
}
@@ -46,7 +45,7 @@ def test_read_version_v1(self):
4645

4746
def test_read_version_v2(self):
4847
req = Mock()
49-
compressed_base64_json = b"x\x9c%O\xb9\n\x83@\x10\xfd\x97\xa9-\x92%E\x14R\xe4 +\x16\t\xe6\x10\xbb\xb0\xc7\xe0\x81\xb8\xb2\xbb*A\xfc\xf7\x8c\xa6|\xe7\xbc\x99\xc0\xa2\xebL\xeb\x10\xa2\t\xa4\xd1_\x88\xe0\xc93'\xf9\xbe\xc8X\xf8\x95<=@\x00\x1a\x95\xd1\xf8Q\xa6\xf5\xd8z\x88\xbc\xed1\x80\x12\x85F\xeb\x96h\xca\xc2^\xf3\xac\xd7\xe7\xed\x1b\xf3SC5\x04w\xfa\x1c\x8e\x92_;Y\x1c\x96\x9a\x94]k\xc1\xdf~u\xc7\xc9 \x8fDG\xa0\xe2\xac\x92\xbc\xa9\xc9\xf1\xc8\xcbQ\xe4I\xa3\xc6U\xb9_\x14\xbb\xbdh\xc2\x1c\xd0R\xe1LK$\xd9\x9c\x17\xbe\xa7\xc3l\xb3Y\x80\xad\x94\xff\x0b\x03\xed\xa9V\x17[2\x83\xb0\xf4\xd14\xcf?E\x03Im"
48+
compressed_base64_json = b'x\x9c\x1d\x8fK\x0f\x820\x10\x84\xff\xcb\x9e9h\xe3A\x9ax\xf0\x11K8hPi\xb8\x99>6\n!\xd4\xd0\x02!\x84\xff\xee\xc2qg\xbe\x9d\x9d\x9d\xa0E\xffs\x8dG\xe0\x13hgG\xe0\xf0\x14\xd2k\xb1\xffH\x16\x8fZd\x07\x88\xc0\xa2q\x16\xdf\xc65\x01\x9b\x00<\xb4\x1dF\xf0Ee\xb1\xf5\xcbj\xc6\xe2\xce\n\xd9\xd9\xf36\xc7\xe2TS\x0c\x8d;{\x8e\x07-\xae?\xfd9,1\x19\xbbVJ\xe4a\xa5\x93\xb4\xd7G\x929\x98D\x96Z\xd4\x15\x11\x8f\xe2;\xa8"\xad\xcd\xb0:\xf7\x8ba\xb7\x17U\x98#j\xaa\xbckH$\xcc\x07\x15::\xcc6\x9b\x08z\xeaP\xae\x0e[\xb8^\xb5\xf4\xc54\xcf\x7f\xef\xc0E\xe6'
5049
resp = self.serializer._loads_v2(req, compressed_base64_json)
5150
# We have to decode our urllib3 data back into a unicode string.
5251
assert resp.data == "Hello World".encode("utf-8")
@@ -72,7 +71,7 @@ def test_read_v1_serialized_with_py2_TypeError(self):
7271
b"(dp1\nS'response'\np2\n(dp3\nS'body'\np4\nS'Hello World'\n",
7372
b"p5\nsS'version'\np6\nS'2'\nsS'status'\np7\nI200\n",
7473
b"sS'reason'\np8\nS''\nsS'decode_content'\np9\nI01\n",
75-
b"sS'strict'\np10\nS''\nsS'headers'\np11\n(dp12\n",
74+
b"sS'headers'\np11\n(dp12\n",
7675
b"S'Content-Type'\np13\nS'text/plain'\np14\n",
7776
b"sS'Cache-Control'\np15\nS'public'\np16\n",
7877
b"sS'Expires'\np17\nS'87654'\np18\nsss.",

tests/test_storage_redis.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from datetime import datetime
66

77
from mock import Mock
8+
89
from cachecontrol.caches import RedisCache
910

1011

1112
class TestRedisCache(object):
12-
13-
def setup(self):
13+
def setup_method(self):
1414
self.conn = Mock()
1515
self.cache = RedisCache(self.conn)
1616

tests/test_vary.py

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def cached_equal(self, cached, resp):
3333
cached.status == resp.raw.status,
3434
cached.version == resp.raw.version,
3535
cached.reason == resp.raw.reason,
36-
cached.strict == resp.raw.strict,
3736
cached.decode_content == resp.raw.decode_content,
3837
]
3938

0 commit comments

Comments
 (0)