Skip to content

Commit d33b19b

Browse files
committed
Fix Requests 2, Version Bump to 0.3.5
This fixes a compatiblity issue with the new version of requests. Bumps the release version to 0.3.5, and closes #39.
1 parent 2275749 commit d33b19b

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
language: python
22
before_install: openssl version
33
env:
4-
- WITH_REQUESTS="True"
4+
- WITH_REQUESTS="2.x"
5+
- WITH_REQUESTS="1.x"
56
- WITH_REQUESTS="False"
67
python:
78
- 2.6
89
- 2.7
910
- pypy
1011
install:
1112
- pip install PyYAML pytest --use-mirrors
12-
- if [ $WITH_REQUESTS = "True" ] ; then pip install requests; fi
13+
- if [ $WITH_REQUESTS = "1.x" ] ; then pip install requests==1.2.3; fi
14+
- if [ $WITH_REQUESTS = "2.x" ] ; then pip install requests; fi
1315
script: python setup.py test

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ This library is a work in progress, so the API might change on you.
259259
There are probably some [bugs](https://github.com/kevin1024/vcrpy/issues?labels=bug&page=1&state=open) floating around too.
260260

261261
##Changelog
262+
* 0.3.5: Fix compatibility with requests 2.x
262263
* 0.3.4: Bugfix: close file before renaming it. This fixes an issue on Windows. Thanks @smallcode for the fix.
263264
* 0.3.3: Bugfix for error message when an unreigstered custom matcher
264265
was used

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def run_tests(self):
1919
sys.exit(errno)
2020

2121
setup(name='vcrpy',
22-
version='0.3.4',
22+
version='0.3.5',
2323
description="A Python port of Ruby's VCR to make mocking HTTP easier",
2424
author='Kevin McCarthy',
2525
author_email='[email protected]',

tox.ini

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py26, py27, pypy, py26requests, py27requests, pypyrequests
7+
envlist = py26, py27, pypy, py26requests, py27requests, pypyrequests, py26oldrequests, py27oldrequests, pypyoldrequests
88

99
[testenv]
1010
commands =
@@ -13,6 +13,27 @@ deps =
1313
pytest
1414
PyYAML
1515

16+
[testenv:py26oldrequests]
17+
basepython = python2.6
18+
deps =
19+
pytest
20+
PyYAML
21+
requests==1.2.3
22+
23+
[testenv:py27oldrequests]
24+
basepython = python2.7
25+
deps =
26+
pytest
27+
PyYAML
28+
requests==1.2.3
29+
30+
[testenv:pypyoldrequests]
31+
basepython = pypy
32+
deps =
33+
pytest
34+
PyYAML
35+
requests==1.2.3
36+
1637
[testenv:py26requests]
1738
basepython = python2.6
1839
deps =

vcr/patch.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ def install(cassette):
3636
httplib.HTTPConnection.cassette = cassette
3737
httplib.HTTPSConnection.cassette = cassette
3838

39-
# patch requests
39+
# patch requests v1.x
4040
try:
4141
import requests.packages.urllib3.connectionpool as cpool
4242
from .stubs.requests_stubs import VCRVerifiedHTTPSConnection
4343
cpool.VerifiedHTTPSConnection = VCRVerifiedHTTPSConnection
4444
cpool.VerifiedHTTPSConnection.cassette = cassette
4545
cpool.HTTPConnection = VCRHTTPConnection
4646
cpool.HTTPConnection.cassette = cassette
47+
# patch requests v2.x
48+
cpool.HTTPConnectionPool.ConnectionCls = VCRHTTPConnection
49+
cpool.HTTPConnectionPool.cassette = cassette
50+
cpool.HTTPSConnectionPool.ConnectionCls = VCRHTTPSConnection
51+
cpool.HTTPSConnectionPool.cassette = cassette
4752
except ImportError: # pragma: no cover
4853
pass
4954

@@ -68,12 +73,16 @@ def reset():
6873
import requests.packages.urllib3.connectionpool as cpool
6974
cpool.VerifiedHTTPSConnection = _VerifiedHTTPSConnection
7075
cpool.HTTPConnection = _HTTPConnection
76+
cpool.HTTPConnectionPool.ConnectionCls = _HTTPConnection
77+
cpool.HTTPSConnectionPool.ConnectionCls = _HTTPSConnection
7178
except ImportError: # pragma: no cover
7279
pass
7380

7481
try:
7582
import urllib3.connectionpool as cpool
7683
cpool.VerifiedHTTPSConnection = _VerifiedHTTPSConnection
7784
cpool.HTTPConnection = _HTTPConnection
85+
cpool.HTTPConnectionPool.ConnectionCls = _HTTPConnection
86+
cpool.HTTPSConnectionPool.ConnectionCls = _HTTPSConnection
7887
except ImportError: # pragma: no cover
7988
pass

vcr/stubs/__init__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ def send(self, data):
7777
'''
7878
self._vcr_request.body = (self._vcr_request.body or '') + data
7979

80+
def close(self):
81+
self._restore_socket()
82+
self._baseclass.close(self)
83+
84+
def _restore_socket(self):
85+
"""
86+
Since some libraries (REQUESTS!!) decide to set options on
87+
connection.socket, I need to delete the socket attribute
88+
(which makes requests think this is a AppEngine connection)
89+
and then restore it when I want to make the actual request.
90+
This function restores it to its standard initial value
91+
(which is None)
92+
"""
93+
if not hasattr(self, 'sock'):
94+
self.sock = None
95+
8096
def _send_request(self, method, url, body, headers):
8197
"""
8298
Copy+pasted from python stdlib 2.6 source because it
@@ -132,6 +148,7 @@ def _send_output(self, message_body=None):
132148
if isinstance(message_body, str):
133149
msg += message_body
134150
message_body = None
151+
self._restore_socket()
135152
self._baseclass.send(self, msg)
136153
if message_body is not None:
137154
#message_body was not a string (i.e. it is a file) and
@@ -156,7 +173,10 @@ def getresponse(self, _=False):
156173
# Otherwise, we should send the request, then get the response
157174
# and return it.
158175

159-
# make the request
176+
# restore sock's value to None, since we need a real socket
177+
self._restore_socket()
178+
179+
#make the actual request
160180
self._baseclass.request(
161181
self,
162182
method=self._vcr_request.method,
@@ -189,6 +209,8 @@ class VCRHTTPConnection(VCRConnectionMixin, HTTPConnection):
189209

190210
def __init__(self, *args, **kwargs):
191211
HTTPConnection.__init__(self, *args, **kwargs)
212+
# see VCRConnectionMixin._restore_socket for the motivation here
213+
del self.sock
192214

193215

194216
class VCRHTTPSConnection(VCRConnectionMixin, HTTPSConnection):
@@ -203,3 +225,5 @@ class because HTTPConnection when this happens has been replaced by
203225
HTTPConnection.__init__(self, *args, **kwargs)
204226
self.key_file = kwargs.pop('key_file', None)
205227
self.cert_file = kwargs.pop('cert_file', None)
228+
# see VCRConnectionMixin._restore_socket for the motivation here
229+
del self.sock

0 commit comments

Comments
 (0)