diff --git a/tests/integration/test_requests.py b/tests/integration/test_requests.py index e36da18..9062d7d 100644 --- a/tests/integration/test_requests.py +++ b/tests/integration/test_requests.py @@ -24,30 +24,30 @@ def scheme(request): def test_status_code(scheme, tmpdir): '''Ensure that we can read the status code''' url = scheme + '://httpbin.org/' - with vcr.use_cassette(str(tmpdir.join('atts.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('atts.yaml'))): status_code = requests.get(url).status_code - with vcr.use_cassette(str(tmpdir.join('atts.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('atts.yaml'))): assert status_code == requests.get(url).status_code def test_headers(scheme, tmpdir): '''Ensure that we can read the headers back''' url = scheme + '://httpbin.org/' - with vcr.use_cassette(str(tmpdir.join('headers.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('headers.yaml'))): headers = requests.get(url).headers - with vcr.use_cassette(str(tmpdir.join('headers.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('headers.yaml'))): assert headers == requests.get(url).headers def test_body(tmpdir, scheme): '''Ensure the responses are all identical enough''' url = scheme + '://httpbin.org/bytes/1024' - with vcr.use_cassette(str(tmpdir.join('body.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('body.yaml'))): content = requests.get(url).content - with vcr.use_cassette(str(tmpdir.join('body.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('body.yaml'))): assert content == requests.get(url).content @@ -55,10 +55,10 @@ def test_auth(tmpdir, scheme): '''Ensure that we can handle basic auth''' auth = ('user', 'passwd') url = scheme + '://httpbin.org/basic-auth/user/passwd' - with vcr.use_cassette(str(tmpdir.join('auth.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('auth.yaml'))): one = requests.get(url, auth=auth) - with vcr.use_cassette(str(tmpdir.join('auth.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('auth.yaml'))): two = requests.get(url, auth=auth) assert one.content == two.content assert one.status_code == two.status_code @@ -68,7 +68,7 @@ def test_auth_failed(tmpdir, scheme): '''Ensure that we can save failed auth statuses''' auth = ('user', 'wrongwrongwrong') url = scheme + '://httpbin.org/basic-auth/user/passwd' - with vcr.use_cassette(str(tmpdir.join('auth-failed.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('auth-failed.yaml'))): # Ensure that this is empty to begin with assert_cassette_empty(cass) one = requests.get(url, auth=auth) @@ -81,10 +81,10 @@ def test_post(tmpdir, scheme): '''Ensure that we can post and cache the results''' data = {'key1': 'value1', 'key2': 'value2'} url = scheme + '://httpbin.org/post' - with vcr.use_cassette(str(tmpdir.join('requests.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('requests.yaml'))): req1 = requests.post(url, data).content - with vcr.use_cassette(str(tmpdir.join('requests.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('requests.yaml'))): req2 = requests.post(url, data).content assert req1 == req2 @@ -93,7 +93,7 @@ def test_post(tmpdir, scheme): def test_redirects(tmpdir, scheme): '''Ensure that we can handle redirects''' url = scheme + '://httpbin.org/redirect-to?url=bytes/1024' - with vcr.use_cassette(str(tmpdir.join('requests.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('requests.yaml'))): content = requests.get(url).content with vcr.use_cassette(str(tmpdir.join('requests.yaml'))) as cass: @@ -124,11 +124,11 @@ def test_gzip(tmpdir, scheme): url = scheme + '://httpbin.org/gzip' response = requests.get(url) - with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): response = requests.get(url) assert_is_json(response.content) - with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))) as cass: + with vcr.use_cassette(str(tmpdir.join('gzip.yaml'))): assert_is_json(response.content) @@ -143,8 +143,8 @@ def test_session_and_connection_close(tmpdir, scheme): with vcr.use_cassette(str(tmpdir.join('session_connection_closed.yaml'))): session = requests.session() - resp = session.get('http://httpbin.org/get', headers={'Connection': 'close'}) - resp = session.get('http://httpbin.org/get', headers={'Connection': 'close'}) + session.get('http://httpbin.org/get', headers={'Connection': 'close'}) + session.get('http://httpbin.org/get', headers={'Connection': 'close'}) def test_https_with_cert_validation_disabled(tmpdir): @@ -164,9 +164,28 @@ def test_session_can_make_requests_after_requests_unpatched(tmpdir): session.get('http://httpbin.org/status/200') -def test_nested_context_managers_with_session_created_before_first_nesting(scheme, tmpdir): +def test_session_created_before_use_cassette_is_patched(tmpdir, scheme): + url = scheme + '://httpbin.org/bytes/1024' + # Record arbitrary, random data to the cassette + with vcr.use_cassette(str(tmpdir.join('session_created_outside.yaml'))): + session = requests.session() + body = session.get(url).content + + # Create a session outside of any cassette context manager + session = requests.session() + # Make a request to make sure that a connectionpool is instantiated + session.get(scheme + '://httpbin.org/get') + + with vcr.use_cassette(str(tmpdir.join('session_created_outside.yaml'))): + # These should only be the same if the patching succeeded. + assert session.get(url).content == body + + +def test_nested_cassettes_with_session_created_before_nesting(scheme, tmpdir): ''' This tests ensures that a session that was created while one cassette was + active is patched to the use the responses of a second cassette when it + is enabled. ''' url = scheme + '://httpbin.org/bytes/1024' with vcr.use_cassette(str(tmpdir.join('first_nested.yaml'))): diff --git a/tox.ini b/tox.ini index bf7a840..5797c48 100644 --- a/tox.ini +++ b/tox.ini @@ -40,220 +40,150 @@ deps = pytest pytest-localserver PyYAML + ipdb [testenv:py26requests1] basepython = python2.6 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==1.2.3 [testenv:py27requests1] basepython = python2.7 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==1.2.3 [testenv:py33requests1] basepython = python3.3 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==1.2.3 [testenv:pypyrequests1] basepython = pypy deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==1.2.3 [testenv:py26requests24] basepython = python2.6 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.4.0 [testenv:py27requests24] basepython = python2.7 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.4.0 [testenv:py33requests24] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.4.0 [testenv:py34requests24] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.4.0 [testenv:pypyrequests24] basepython = pypy deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.4.0 [testenv:py26requests23] basepython = python2.6 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.3.0 [testenv:py27requests23] basepython = python2.7 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.3.0 [testenv:py33requests23] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.3.0 [testenv:py34requests23] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.3.0 [testenv:pypyrequests23] basepython = pypy deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.3.0 [testenv:py26requests22] basepython = python2.6 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.2.1 [testenv:py27requests22] basepython = python2.7 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.2.1 [testenv:py33requests22] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.2.1 [testenv:py34requests22] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.2.1 + [testenv:pypyrequests22] basepython = pypy deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} requests==2.2.1 [testenv:py26httplib2] basepython = python2.6 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} httplib2 [testenv:py27httplib2] basepython = python2.7 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} httplib2 [testenv:py33httplib2] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} httplib2 [testenv:py34httplib2] basepython = python3.4 deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} httplib2 [testenv:pypyhttplib2] basepython = pypy deps = - mock - pytest - pytest-localserver - PyYAML + {[testenv]deps} httplib2 diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index 772731e..df67b1d 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -119,7 +119,7 @@ class VCRHTTPResponse(HTTPResponse): return default -class VCRConnection: +class VCRConnection(object): # A reference to the cassette that's currently being patched in cassette = None @@ -205,7 +205,7 @@ class VCRConnection: pass def getresponse(self, _=False): - '''Retrieve a the response''' + '''Retrieve the response''' # Check to see if the cassette has a response for this request. If so, # then return it if self.cassette.can_play_response_for(self._vcr_request):