From 1880ae30f4a7a6a24a9ebd2ba21bf2070f43732b Mon Sep 17 00:00:00 2001 From: Jeff Dairiki Date: Tue, 23 May 2023 20:18:40 -0700 Subject: [PATCH] fix(test_etag): fix for requests>=2.29 Patch all the possible methods that might be used to generate a response. --- tests/test_etag.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/test_etag.py b/tests/test_etag.py index 7523bf82..697fe7ea 100644 --- a/tests/test_etag.py +++ b/tests/test_etag.py @@ -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 @@ -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" + # This 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