From bc5199c89383a6486602242c89b3906a87c28fcb Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Wed, 23 Apr 2014 19:30:43 -1000 Subject: [PATCH] improve socket mocking to hopefully pass both new python3.4 socket connection stuff and requests tests --- .travis.yml | 4 ++-- README.md | 3 ++- vcr/stubs/__init__.py | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 58a4bf9..723bfee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,11 +14,11 @@ matrix: - env: WITH_LIB="boto" exclude: - env: WITH_LIB="boto" - python: 3.4 + python: 3.3 python: - 2.6 - 2.7 -- 3.4 +- 3.3 - pypy install: - pip install PyYAML pytest --use-mirrors diff --git a/README.md b/README.md index ced9e18..24d4e8b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ to do is delete your existing cassette files, and run your tests again. All of the mocked responses will be updated with the new API. ## Compatibility Notes -VCR.py supports Python 2.6 and 2.7, 3.3, and [pypy](http://pypy.org). +VCR.py supports Python 2.6 and 2.7, 3.4, and [pypy](http://pypy.org). The following http libraries are supported: @@ -281,6 +281,7 @@ Also, in order for the boto tests to run, you will need an AWS key. Refer to th ## Changelog +* 1.0.0 (in development) - Bump supported Python3 version to 3.4, fix some bugs with Boto support (thanks @marusich), fix error with URL field capitalization in README (thanks @simon-weber) * 0.7.0: VCR.py now supports Python 3! (thanks @asundg) Also I refactored the stub connections quite a bit to add support for the putrequest and putheader calls. This version also adds support for httplib2 (thanks @nilp0inter). I have added a couple tests for bobo since it is an http client in its own right. Finally, this version includes a fix for a bug where requests wasn't being patched properly (thanks @msabramo). * 0.6.0: Store response headers as a list since a HTTP response can have the same header twice (happens with set-cookie sometimes). This has the added benefit of preserving the order of headers. Thanks @smallcode for the bug report leading to this change. I have made an effort to ensure backwards compatibility with the old cassettes' header storage mechanism, but if you want to upgrade to the new header storage, you should delete your cassettes and re-record them. Also this release adds better error messages (thanks @msabramo) and adds support for using VCR as a decorator (thanks @smallcode for the motivation) * 0.5.0: Change the `response_of` method to `responses_of` since cassettes can now contain more than one response for a request. Since this changes the API, I'm bumping the version. Also includes 2 bugfixes: a better error message when attempting to overwrite a cassette file, and a fix for a bug with requests sessions (thanks @msabramo) diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index 3d32570..07f8660 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -16,6 +16,27 @@ from vcr.request import Request from vcr.errors import CannotOverwriteExistingCassetteException from . import compat +class VCRFakeSocket(object): + """ + A socket that doesn't do anything! + Used when playing back casssettes, when there + is no actual open socket. + """ + + def close(self): + pass + + def settimeout(self, *args, **kwargs): + pass + + def fileno(self): + """ + This is kinda crappy. requests will watch + this descriptor and make sure it's not closed. + Return file descriptor 0 since that's stdin. + """ + return 0 # wonder how bad this is.... + def parse_headers_backwards_compat(header_dict): """ @@ -220,6 +241,17 @@ class VCRConnection: return self.real_connection.connect(*args, **kwargs) + @property + def sock(self): + if self.real_connection.sock: + return self.real_connection.sock + return VCRFakeSocket() + + @sock.setter + def sock(self, value): + if self.real_connection.sock: + self.real_connection.sock = value + def __init__(self, *args, **kwargs): # need to temporarily reset here because the real connection # inherits from the thing that we are mocking out. Take out @@ -228,7 +260,6 @@ class VCRConnection: reset() self.real_connection = self._baseclass(*args, **kwargs) install(self.cassette) - self.sock = self.real_connection.sock class VCRHTTPConnection(VCRConnection):