Skip to content

Commit

Permalink
Fix Requests 2, Version Bump to 0.3.5
Browse files Browse the repository at this point in the history
This fixes a compatiblity issue with the new version of requests.
Bumps the release version to 0.3.5, and closes #39.
  • Loading branch information
kevin1024 committed Oct 25, 2013
1 parent 2275749 commit d33b19b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
language: python
before_install: openssl version
env:
- WITH_REQUESTS="True"
- WITH_REQUESTS="2.x"
- WITH_REQUESTS="1.x"
- WITH_REQUESTS="False"
python:
- 2.6
- 2.7
- pypy
install:
- pip install PyYAML pytest --use-mirrors
- if [ $WITH_REQUESTS = "True" ] ; then pip install requests; fi
- if [ $WITH_REQUESTS = "1.x" ] ; then pip install requests==1.2.3; fi
- if [ $WITH_REQUESTS = "2.x" ] ; then pip install requests; fi
script: python setup.py test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ This library is a work in progress, so the API might change on you.
There are probably some [bugs](https://github.com/kevin1024/vcrpy/issues?labels=bug&page=1&state=open) floating around too.

##Changelog
* 0.3.5: Fix compatibility with requests 2.x
* 0.3.4: Bugfix: close file before renaming it. This fixes an issue on Windows. Thanks @smallcode for the fix.
* 0.3.3: Bugfix for error message when an unreigstered custom matcher
was used
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def run_tests(self):
sys.exit(errno)

setup(name='vcrpy',
version='0.3.4',
version='0.3.5',
description="A Python port of Ruby's VCR to make mocking HTTP easier",
author='Kevin McCarthy',
author_email='me@kevinmccarthy.org',
Expand Down
23 changes: 22 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = py26, py27, pypy, py26requests, py27requests, pypyrequests
envlist = py26, py27, pypy, py26requests, py27requests, pypyrequests, py26oldrequests, py27oldrequests, pypyoldrequests

[testenv]
commands =
Expand All @@ -13,6 +13,27 @@ deps =
pytest
PyYAML

[testenv:py26oldrequests]
basepython = python2.6
deps =
pytest
PyYAML
requests==1.2.3

[testenv:py27oldrequests]
basepython = python2.7
deps =
pytest
PyYAML
requests==1.2.3

[testenv:pypyoldrequests]
basepython = pypy
deps =
pytest
PyYAML
requests==1.2.3

[testenv:py26requests]
basepython = python2.6
deps =
Expand Down
11 changes: 10 additions & 1 deletion vcr/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ def install(cassette):
httplib.HTTPConnection.cassette = cassette
httplib.HTTPSConnection.cassette = cassette

# patch requests
# patch requests v1.x
try:
import requests.packages.urllib3.connectionpool as cpool
from .stubs.requests_stubs import VCRVerifiedHTTPSConnection
cpool.VerifiedHTTPSConnection = VCRVerifiedHTTPSConnection
cpool.VerifiedHTTPSConnection.cassette = cassette
cpool.HTTPConnection = VCRHTTPConnection
cpool.HTTPConnection.cassette = cassette
# patch requests v2.x
cpool.HTTPConnectionPool.ConnectionCls = VCRHTTPConnection
cpool.HTTPConnectionPool.cassette = cassette
cpool.HTTPSConnectionPool.ConnectionCls = VCRHTTPSConnection
cpool.HTTPSConnectionPool.cassette = cassette
except ImportError: # pragma: no cover
pass

Expand All @@ -68,12 +73,16 @@ def reset():
import requests.packages.urllib3.connectionpool as cpool
cpool.VerifiedHTTPSConnection = _VerifiedHTTPSConnection
cpool.HTTPConnection = _HTTPConnection
cpool.HTTPConnectionPool.ConnectionCls = _HTTPConnection
cpool.HTTPSConnectionPool.ConnectionCls = _HTTPSConnection
except ImportError: # pragma: no cover
pass

try:
import urllib3.connectionpool as cpool
cpool.VerifiedHTTPSConnection = _VerifiedHTTPSConnection
cpool.HTTPConnection = _HTTPConnection
cpool.HTTPConnectionPool.ConnectionCls = _HTTPConnection
cpool.HTTPSConnectionPool.ConnectionCls = _HTTPSConnection
except ImportError: # pragma: no cover
pass
26 changes: 25 additions & 1 deletion vcr/stubs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ def send(self, data):
'''
self._vcr_request.body = (self._vcr_request.body or '') + data

def close(self):
self._restore_socket()
self._baseclass.close(self)

def _restore_socket(self):
"""
Since some libraries (REQUESTS!!) decide to set options on
connection.socket, I need to delete the socket attribute
(which makes requests think this is a AppEngine connection)
and then restore it when I want to make the actual request.
This function restores it to its standard initial value
(which is None)
"""
if not hasattr(self, 'sock'):
self.sock = None

def _send_request(self, method, url, body, headers):
"""
Copy+pasted from python stdlib 2.6 source because it
Expand Down Expand Up @@ -132,6 +148,7 @@ def _send_output(self, message_body=None):
if isinstance(message_body, str):
msg += message_body
message_body = None
self._restore_socket()
self._baseclass.send(self, msg)
if message_body is not None:
#message_body was not a string (i.e. it is a file) and
Expand All @@ -156,7 +173,10 @@ def getresponse(self, _=False):
# Otherwise, we should send the request, then get the response
# and return it.

# make the request
# restore sock's value to None, since we need a real socket
self._restore_socket()

#make the actual request
self._baseclass.request(
self,
method=self._vcr_request.method,
Expand Down Expand Up @@ -189,6 +209,8 @@ class VCRHTTPConnection(VCRConnectionMixin, HTTPConnection):

def __init__(self, *args, **kwargs):
HTTPConnection.__init__(self, *args, **kwargs)
# see VCRConnectionMixin._restore_socket for the motivation here
del self.sock


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

0 comments on commit d33b19b

Please sign in to comment.