Skip to content

Commit

Permalink
fix(test_etag): fix for requests>=2.29
Browse files Browse the repository at this point in the history
Patch all the possible methods that might be used to generate a
response.
  • Loading branch information
dairiki committed May 24, 2023
1 parent 240dc16 commit b91b7eb
Showing 1 changed file with 15 additions and 4 deletions.
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

0 comments on commit b91b7eb

Please sign in to comment.