Skip to content

Commit

Permalink
fix(#292): disuse HTTPResponse.strict (#301)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dairiki authored May 25, 2023
1 parent c05ef9e commit 8f037ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ jobs:
runs-on: "${{ matrix.os }}"

strategy:
fail-fast: false
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
os: ["macos-10.15", "windows-latest", "ubuntu-latest"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
os: ["macos-latest", "windows-latest", "ubuntu-latest"]
include:
- python-version: "3.6"
os: "ubuntu-20.04"
- python-version: "3.6"
os: "windows-latest"

steps:
- uses: "actions/checkout@v2"
Expand Down
4 changes: 3 additions & 1 deletion cachecontrol/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def dumps(self, request, response, body=None):
u"status": response.status,
u"version": response.version,
u"reason": text_type(response.reason),
u"strict": response.strict,
u"decode_content": response.decode_content,
}
}
Expand Down Expand Up @@ -138,6 +137,9 @@ def prepare_response(self, request, cached, body_file=None):
# TypeError: 'str' does not support the buffer interface
body = io.BytesIO(body_raw.encode("utf8"))

# Discard any `strict` parameter serialized by older version of cachecontrol.
cached["response"].pop("strict", None)

return HTTPResponse(body=body, preload_content=False, **cached["response"])

def _loads_v0(self, request, data, body_file=None):
Expand Down
19 changes: 15 additions & 4 deletions tests/test_etag.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: 2015 Eric Larson
#
# SPDX-License-Identifier: Apache-2.0
from contextlib import ExitStack
from contextlib import suppress

import pytest

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

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

# This is how the urllib3 response is created in
# requests.adapters
response_mod = "requests.adapters.HTTPResponse.from_httplib"
# These are various ways the the urllib3 response can created
# in requests.adapters. Which one is actually used depends
# on which version if `requests` is in use, as well as perhaps
# other parameters.
response_mods = [
"requests.adapters.HTTPResponse.from_httplib",
"urllib3.HTTPConnectionPool.urlopen",
]

with ExitStack() as stack:
for mod in response_mods:
with suppress(ImportError):
stack.enter_context(patch(mod, Mock(return_value=resp)))

with patch(response_mod, Mock(return_value=resp)):
sess.get(etag_url)
assert resp.read.called
assert resp.release_conn.called
1 change: 0 additions & 1 deletion tests/test_vary.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def cached_equal(self, cached, resp):
cached.status == resp.raw.status,
cached.version == resp.raw.version,
cached.reason == resp.raw.reason,
cached.strict == resp.raw.strict,
cached.decode_content == resp.raw.decode_content,
]

Expand Down

1 comment on commit 8f037ba

@t0b3
Copy link

@t0b3 t0b3 commented on 8f037ba May 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ionrock now this commit is in - can you release a new version i.e. 0.12.13 for https://pypi.org/project/cachecontrol/#history ?
motivation: this would solve many downstream issues for others eg. depending on latest cachecontrol for CI jobs etc. i.e. those using poetry ...
see example at https://gitlab.com/inkscape/extensions/-/commit/22095f0e72e4b0b4064292d75fa72263d68ae068#587d266bb27a4dc3022bbed44dfa19849df3044c_97_94

Please sign in to comment.