From ec60af0214fd2d94159af14ec4169c44909fe525 Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Tue, 15 Aug 2017 16:39:33 -0700 Subject: [PATCH 1/3] Fix mixup between httplib and urllib3 --- tests/integration/test_urllib3.py | 19 +++++++++++++++++++ vcr/patch.py | 8 ++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_urllib3.py b/tests/integration/test_urllib3.py index 0c02c40..2884d35 100644 --- a/tests/integration/test_urllib3.py +++ b/tests/integration/test_urllib3.py @@ -5,8 +5,10 @@ import pytest import pytest_httpbin 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') @@ -138,3 +140,20 @@ def test_gzip(tmpdir, httpbin_both, verify_pool_mgr): def test_https_with_cert_validation_disabled(tmpdir, httpbin_secure, pool_mgr): with vcr.use_cassette(str(tmpdir.join('cert_validation_disabled.yaml'))): pool_mgr.request('GET', httpbin_secure.url) + + +def test_urllib3_force_reset(): + http_original = cpool.HTTPConnection + https_original = cpool.HTTPSConnection + verified_https_original = cpool.VerifiedHTTPSConnection + with vcr.use_cassette(path='test'): + first_cassette_HTTPConnection = cpool.HTTPConnection + first_cassette_HTTPSConnection = cpool.HTTPSConnection + first_cassette_VerifiedHTTPSConnection = cpool.VerifiedHTTPSConnection + with force_reset(): + assert cpool.HTTPConnection is http_original + assert cpool.HTTPSConnection is https_original + assert cpool.VerifiedHTTPSConnection is verified_https_original + assert cpool.HTTPConnection is first_cassette_HTTPConnection + assert cpool.HTTPSConnection is first_cassette_HTTPSConnection + assert cpool.VerifiedHTTPSConnection is first_cassette_VerifiedHTTPSConnection diff --git a/vcr/patch.py b/vcr/patch.py index bc93ad9..434e45c 100644 --- a/vcr/patch.py +++ b/vcr/patch.py @@ -386,11 +386,11 @@ def reset_patchers(): pass else: yield mock.patch.object(cpool, 'VerifiedHTTPSConnection', _VerifiedHTTPSConnection) - yield mock.patch.object(cpool, 'HTTPConnection', _HTTPConnection) - yield mock.patch.object(cpool, 'HTTPSConnection', _HTTPSConnection) + yield mock.patch.object(cpool, 'HTTPConnection', _cpoolHTTPConnection) + yield mock.patch.object(cpool, 'HTTPSConnection', _cpoolHTTPSConnection) if hasattr(cpool.HTTPConnectionPool, 'ConnectionCls'): - yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls', _HTTPConnection) - yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls', _HTTPSConnection) + yield mock.patch.object(cpool.HTTPConnectionPool, 'ConnectionCls', _cpoolHTTPConnection) + yield mock.patch.object(cpool.HTTPSConnectionPool, 'ConnectionCls', _cpoolHTTPSConnection) try: import botocore.vendored.requests.packages.urllib3.connectionpool as cpool From 262ad903cb1e9d6b815333d9a6e084f06b3db291 Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Wed, 16 Aug 2017 14:43:27 -0700 Subject: [PATCH 2/3] Don't unmock requests.packages if not necessary --- tests/integration/test_urllib3.py | 2 +- vcr/patch.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_urllib3.py b/tests/integration/test_urllib3.py index 2884d35..51618c7 100644 --- a/tests/integration/test_urllib3.py +++ b/tests/integration/test_urllib3.py @@ -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 diff --git a/vcr/patch.py b/vcr/patch.py index 434e45c..9de12a3 100644 --- a/vcr/patch.py +++ b/vcr/patch.py @@ -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 + # > is + # 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: From 81d453f7d3861b311efc594ca58ea206ee4791cc Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Wed, 16 Aug 2017 14:45:32 -0700 Subject: [PATCH 3/3] Add requests 2.18.4 to build --- .travis.yml | 1 + tox.ini | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1610e5f..2f7fac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ env: - TOX_SUFFIX="requests211" - TOX_SUFFIX="requests213" - TOX_SUFFIX="requests216" + - TOX_SUFFIX="requests218" - TOX_SUFFIX="requests1" - TOX_SUFFIX="httplib2" - TOX_SUFFIX="boto" diff --git a/tox.ini b/tox.ini index 6402007..93ebad5 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = {py26,py27,py35,py36,pypy,pypy3}-{flakes,requests216,requests213,requests211,requests27,requests26,requests25,requests24,requests23,requests22,requests1,httplib2,urllib319,urllib3110,urllib3121,tornado3,tornado4,boto,boto3,aiohttp} +envlist = {py26,py27,py35,py36,pypy,pypy3}-{flakes,requests218,requests216,requests213,requests211,requests27,requests26,requests25,requests24,requests23,requests22,requests1,httplib2,urllib319,urllib3110,urllib3121,tornado3,tornado4,boto,boto3,aiohttp} [testenv:flakes] skipsdist = True @@ -20,6 +20,7 @@ deps = pytest-httpbin PyYAML requests1: requests==1.2.3 + requests218: requests==2.18.4 requests216: requests==2.16.3 requests213: requests==2.13.0 requests211: requests==2.11.1