]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Don't unmock requests.packages if not necessary
authorLaurent Mazuel <lmazuel@microsoft.com>
Wed, 16 Aug 2017 21:43:27 +0000 (14:43 -0700)
committerLaurent Mazuel <lmazuel@microsoft.com>
Thu, 17 Aug 2017 15:37:11 +0000 (08:37 -0700)
tests/integration/test_urllib3.py
vcr/patch.py

index 2884d3564783e2b2ca66033fc05e34f09900ecf6..51618c7f3f71a34343dfb0dfbc5971f99d4cd11e 100644 (file)
@@ -8,7 +8,6 @@ import vcr
 from vcr.patch import force_reset
 from assertions import assert_cassette_empty, assert_is_json
 urllib3 = pytest.importorskip("urllib3")
-cpool = pytest.importorskip("urllib3.connectionpool")
 
 
 @pytest.fixture(scope='module')
@@ -143,6 +142,7 @@ def test_https_with_cert_validation_disabled(tmpdir, httpbin_secure, pool_mgr):
 
 
 def test_urllib3_force_reset():
+    cpool = urllib3.connectionpool
     http_original = cpool.HTTPConnection
     https_original = cpool.HTTPSConnection
     verified_https_original = cpool.VerifiedHTTPSConnection
index 434e45cf5fa98c36324c18fd41f3501619f025cb..9de12a355fb2013533bbb7541ce208323ca679cd 100644 (file)
@@ -362,8 +362,22 @@ class ConnectionRemover(object):
 def reset_patchers():
     yield mock.patch.object(httplib, 'HTTPConnection', _HTTPConnection)
     yield mock.patch.object(httplib, 'HTTPSConnection', _HTTPSConnection)
+
     try:
-        import requests.packages.urllib3.connectionpool as cpool
+        import requests
+        if requests.__build__ < 0x021603:
+            # Avoid double unmock if requests 2.16.3
+            # First, this is pointless, requests.packages.urllib3 *IS* urllib3 (see packages.py)
+            # Second, this is unmocking twice the same classes with different namespaces
+            # and is creating weird issues and bugs:
+            # > AssertionError: assert <class 'urllib3.connection.HTTPConnection'>
+            # >   is <class 'requests.packages.urllib3.connection.HTTPConnection'>
+            # This assert should work!!!
+            # Note that this also means that now, requests.packages is never imported
+            # if requests 2.16.3 or greater is used with VCRPy.
+            import requests.packages.urllib3.connectionpool as cpool
+        else:
+            raise ImportError("Skip requests not vendored anymore")
     except ImportError:  # pragma: no cover
         pass
     else: