1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

fix: use urllib3.connection where needed.

Since urllib3 v2 the re-export of connection.HTTPConnection in
urllib3.connectionpool was removed.

In this commit we use urllib3.connection where needed. Some references
to connectionpool.HTTPConnection are still there for backward
compatibility.

Closes #688
This commit is contained in:
Sonny V
2023-05-12 17:58:30 +02:00
committed by Kevin McCarthy
parent 43484e7cff
commit 51c99bb9df
4 changed files with 33 additions and 29 deletions

View File

@@ -145,18 +145,18 @@ def test_https_with_cert_validation_disabled(tmpdir, httpbin_secure, pool_mgr):
def test_urllib3_force_reset(): def test_urllib3_force_reset():
cpool = urllib3.connectionpool conn = urllib3.connection
http_original = cpool.HTTPConnection http_original = conn.HTTPConnection
https_original = cpool.HTTPSConnection https_original = conn.HTTPSConnection
verified_https_original = cpool.VerifiedHTTPSConnection verified_https_original = conn.VerifiedHTTPSConnection
with vcr.use_cassette(path="test"): with vcr.use_cassette(path="test"):
first_cassette_HTTPConnection = cpool.HTTPConnection first_cassette_HTTPConnection = conn.HTTPConnection
first_cassette_HTTPSConnection = cpool.HTTPSConnection first_cassette_HTTPSConnection = conn.HTTPSConnection
first_cassette_VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection first_cassette_VerifiedHTTPSConnection = conn.VerifiedHTTPSConnection
with force_reset(): with force_reset():
assert cpool.HTTPConnection is http_original assert conn.HTTPConnection is http_original
assert cpool.HTTPSConnection is https_original assert conn.HTTPSConnection is https_original
assert cpool.VerifiedHTTPSConnection is verified_https_original assert conn.VerifiedHTTPSConnection is verified_https_original
assert cpool.HTTPConnection is first_cassette_HTTPConnection assert conn.HTTPConnection is first_cassette_HTTPConnection
assert cpool.HTTPSConnection is first_cassette_HTTPSConnection assert conn.HTTPSConnection is first_cassette_HTTPSConnection
assert cpool.VerifiedHTTPSConnection is first_cassette_VerifiedHTTPSConnection assert conn.VerifiedHTTPSConnection is first_cassette_VerifiedHTTPSConnection

View File

@@ -32,15 +32,17 @@ else:
_cpoolBoto3HTTPSConnection = AWSHTTPSConnection _cpoolBoto3HTTPSConnection = AWSHTTPSConnection
cpool = None cpool = None
conn = None
# Try to save the original types for urllib3 # Try to save the original types for urllib3
try: try:
import urllib3.connection as conn
import urllib3.connectionpool as cpool import urllib3.connectionpool as cpool
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
pass pass
else: else:
_VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection _VerifiedHTTPSConnection = conn.VerifiedHTTPSConnection
_cpoolHTTPConnection = cpool.HTTPConnection _connHTTPConnection = conn.HTTPConnection
_cpoolHTTPSConnection = cpool.HTTPSConnection _connHTTPSConnection = conn.HTTPSConnection
# Try to save the original types for requests # Try to save the original types for requests
try: try:
@@ -198,7 +200,7 @@ class CassettePatcherBuilder:
from .stubs import requests_stubs from .stubs import requests_stubs
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
return () return ()
return self._urllib3_patchers(cpool, requests_stubs) return self._urllib3_patchers(cpool, conn, requests_stubs)
@_build_patchers_from_mock_triples_decorator @_build_patchers_from_mock_triples_decorator
def _boto3(self): def _boto3(self):
@@ -248,12 +250,13 @@ class CassettePatcherBuilder:
def _urllib3(self): def _urllib3(self):
try: try:
import urllib3.connection as conn
import urllib3.connectionpool as cpool import urllib3.connectionpool as cpool
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
return () return ()
from .stubs import urllib3_stubs from .stubs import urllib3_stubs
return self._urllib3_patchers(cpool, urllib3_stubs) return self._urllib3_patchers(cpool, conn, urllib3_stubs)
@_build_patchers_from_mock_triples_decorator @_build_patchers_from_mock_triples_decorator
def _httplib2(self): def _httplib2(self):
@@ -330,7 +333,7 @@ class CassettePatcherBuilder:
new_sync_client_send = sync_vcr_send(self._cassette, _HttpxSyncClient_send) new_sync_client_send = sync_vcr_send(self._cassette, _HttpxSyncClient_send)
yield httpx.Client, "send", new_sync_client_send yield httpx.Client, "send", new_sync_client_send
def _urllib3_patchers(self, cpool, stubs): def _urllib3_patchers(self, cpool, conn, stubs):
http_connection_remover = ConnectionRemover( http_connection_remover = ConnectionRemover(
self._get_cassette_subclass(stubs.VCRRequestsHTTPConnection) self._get_cassette_subclass(stubs.VCRRequestsHTTPConnection)
) )
@@ -338,9 +341,9 @@ class CassettePatcherBuilder:
self._get_cassette_subclass(stubs.VCRRequestsHTTPSConnection) self._get_cassette_subclass(stubs.VCRRequestsHTTPSConnection)
) )
mock_triples = ( mock_triples = (
(cpool, "VerifiedHTTPSConnection", stubs.VCRRequestsHTTPSConnection), (conn, "VerifiedHTTPSConnection", stubs.VCRRequestsHTTPSConnection),
(cpool, "HTTPConnection", stubs.VCRRequestsHTTPConnection), (conn, "HTTPConnection", stubs.VCRRequestsHTTPConnection),
(cpool, "HTTPSConnection", stubs.VCRRequestsHTTPSConnection), (conn, "HTTPSConnection", stubs.VCRRequestsHTTPSConnection),
(cpool, "is_connection_dropped", mock.Mock(return_value=False)), # Needed on Windows only (cpool, "is_connection_dropped", mock.Mock(return_value=False)), # Needed on Windows only
(cpool.HTTPConnectionPool, "ConnectionCls", stubs.VCRRequestsHTTPConnection), (cpool.HTTPConnectionPool, "ConnectionCls", stubs.VCRRequestsHTTPConnection),
(cpool.HTTPSConnectionPool, "ConnectionCls", stubs.VCRRequestsHTTPSConnection), (cpool.HTTPSConnectionPool, "ConnectionCls", stubs.VCRRequestsHTTPSConnection),
@@ -410,16 +413,17 @@ def reset_patchers():
yield mock.patch.object(httplib, "HTTPSConnection", _HTTPSConnection) yield mock.patch.object(httplib, "HTTPSConnection", _HTTPSConnection)
try: try:
import urllib3.connection as conn
import urllib3.connectionpool as cpool import urllib3.connectionpool as cpool
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
pass pass
else: else:
yield mock.patch.object(cpool, "VerifiedHTTPSConnection", _VerifiedHTTPSConnection) yield mock.patch.object(conn, "VerifiedHTTPSConnection", _VerifiedHTTPSConnection)
yield mock.patch.object(cpool, "HTTPConnection", _cpoolHTTPConnection) yield mock.patch.object(conn, "HTTPConnection", _connHTTPConnection)
yield mock.patch.object(cpool, "HTTPSConnection", _cpoolHTTPSConnection) yield mock.patch.object(conn, "HTTPSConnection", _connHTTPSConnection)
if hasattr(cpool.HTTPConnectionPool, "ConnectionCls"): if hasattr(cpool.HTTPConnectionPool, "ConnectionCls"):
yield mock.patch.object(cpool.HTTPConnectionPool, "ConnectionCls", _cpoolHTTPConnection) yield mock.patch.object(cpool.HTTPConnectionPool, "ConnectionCls", _connHTTPConnection)
yield mock.patch.object(cpool.HTTPSConnectionPool, "ConnectionCls", _cpoolHTTPSConnection) yield mock.patch.object(cpool.HTTPSConnectionPool, "ConnectionCls", _connHTTPSConnection)
try: try:
# unpatch botocore with awsrequest # unpatch botocore with awsrequest

View File

@@ -1,6 +1,6 @@
"""Stubs for requests""" """Stubs for requests"""
from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection from urllib3.connection import HTTPConnection, VerifiedHTTPSConnection
from ..stubs import VCRHTTPConnection, VCRHTTPSConnection from ..stubs import VCRHTTPConnection, VCRHTTPSConnection

View File

@@ -1,6 +1,6 @@
"""Stubs for urllib3""" """Stubs for urllib3"""
from urllib3.connectionpool import HTTPConnection, VerifiedHTTPSConnection from urllib3.connection import HTTPConnection, VerifiedHTTPSConnection
from ..stubs import VCRHTTPConnection, VCRHTTPSConnection from ..stubs import VCRHTTPConnection, VCRHTTPSConnection