Skip to content

Commit 8f037ba

Browse files
authored
fix(#292): disuse HTTPResponse.strict (#301)
* tests(ci): set fail-fast: false so that the whole test matrix is run * fix(ci): update matrix to use runners that support selected python versions * fix: omit `strict` attribute from response serialization The `strict` attribute of `HTTPResponse` is gone in `urllib3>=2.0`. In addition it has no effect, at least when running under Python 3. * fix: omit `strict` attribute during response deserialization This prevents issues when deserializing responses serialized by an earlier version of cachecontrol. * fix(test_etag): fix for requests>=2.29 Patch all the possible methods that might be used to generate a response.
1 parent c05ef9e commit 8f037ba

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

.github/workflows/tests.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ jobs:
1414
runs-on: "${{ matrix.os }}"
1515

1616
strategy:
17+
fail-fast: false
1718
matrix:
18-
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
19-
os: ["macos-10.15", "windows-latest", "ubuntu-latest"]
19+
python-version: ["3.7", "3.8", "3.9", "3.10"]
20+
os: ["macos-latest", "windows-latest", "ubuntu-latest"]
21+
include:
22+
- python-version: "3.6"
23+
os: "ubuntu-20.04"
24+
- python-version: "3.6"
25+
os: "windows-latest"
2026

2127
steps:
2228
- uses: "actions/checkout@v2"

cachecontrol/serialize.py

+3-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
}
@@ -138,6 +137,9 @@ def prepare_response(self, request, cached, body_file=None):
138137
# TypeError: 'str' does not support the buffer interface
139138
body = io.BytesIO(body_raw.encode("utf8"))
140139

140+
# Discard any `strict` parameter serialized by older version of cachecontrol.
141+
cached["response"].pop("strict", None)
142+
141143
return HTTPResponse(body=body, preload_content=False, **cached["response"])
142144

143145
def _loads_v0(self, request, data, body_file=None):

tests/test_etag.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SPDX-FileCopyrightText: 2015 Eric Larson
22
#
33
# SPDX-License-Identifier: Apache-2.0
4+
from contextlib import ExitStack
5+
from contextlib import suppress
46

57
import pytest
68

@@ -134,11 +136,20 @@ def test_not_modified_releases_connection(self, server, url):
134136

135137
resp = Mock(status=304, headers={})
136138

137-
# This is how the urllib3 response is created in
138-
# requests.adapters
139-
response_mod = "requests.adapters.HTTPResponse.from_httplib"
139+
# These are various ways the the urllib3 response can created
140+
# in requests.adapters. Which one is actually used depends
141+
# on which version if `requests` is in use, as well as perhaps
142+
# other parameters.
143+
response_mods = [
144+
"requests.adapters.HTTPResponse.from_httplib",
145+
"urllib3.HTTPConnectionPool.urlopen",
146+
]
147+
148+
with ExitStack() as stack:
149+
for mod in response_mods:
150+
with suppress(ImportError):
151+
stack.enter_context(patch(mod, Mock(return_value=resp)))
140152

141-
with patch(response_mod, Mock(return_value=resp)):
142153
sess.get(etag_url)
143154
assert resp.read.called
144155
assert resp.release_conn.called

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)