mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 17:45:35 +00:00
Add Support for Requests Library
Added support and tests for the Requests library. At least basic support seems to be working. The tests should run fine if you don't have requests installed; it just won't verify the requests- specific tests.
This commit is contained in:
19
vcr/patch.py
19
vcr/patch.py
@@ -2,21 +2,40 @@ import httplib
|
||||
from contextlib import contextmanager
|
||||
from .stubs import VCRHTTPConnection, VCRHTTPSConnection
|
||||
|
||||
|
||||
_HTTPConnection = httplib.HTTPConnection
|
||||
_HTTPSConnection = httplib.HTTPSConnection
|
||||
|
||||
try:
|
||||
import requests.packages.urllib3.connectionpool
|
||||
_VerifiedHTTPSConnection = requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def install(cassette_path):
|
||||
httplib.HTTPConnection = httplib.HTTP._connection_class = VCRHTTPConnection
|
||||
httplib.HTTPSConnection = httplib.HTTPS._connection_class = VCRHTTPSConnection
|
||||
httplib.HTTPConnection._vcr_cassette_path = cassette_path
|
||||
httplib.HTTPSConnection._vcr_cassette_path = cassette_path
|
||||
try:
|
||||
import requests.packages.urllib3.connectionpool
|
||||
from .requests_stubs import VCRVerifiedHTTPSConnection
|
||||
requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection = VCRVerifiedHTTPSConnection
|
||||
requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection._vcr_cassette_path = cassette_path
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def reset():
|
||||
httplib.HTTPConnection = httplib.HTTP._connection_class = _HTTPConnection
|
||||
httplib.HTTPSConnection = httplib.HTTPS._connection_class = \
|
||||
_HTTPSConnection
|
||||
try:
|
||||
import requests.packages.urllib3.connectionpool
|
||||
requests.packages.urllib3.connectionpool.VerifiedHTTPSConnection = _VerifiedHTTPSConnection
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
@contextmanager
|
||||
|
||||
7
vcr/requests_stubs.py
Normal file
7
vcr/requests_stubs.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from requests.packages.urllib3.connectionpool import VerifiedHTTPSConnection
|
||||
from .stubs import VCRHTTPSConnection
|
||||
|
||||
class VCRVerifiedHTTPSConnection(VCRHTTPSConnection, VerifiedHTTPSConnection):
|
||||
|
||||
_baseclass = VerifiedHTTPSConnection
|
||||
|
||||
13
vcr/stubs.py
13
vcr/stubs.py
@@ -12,15 +12,28 @@ class VCRHTTPResponse(object):
|
||||
self.recorded_response = recorded_response
|
||||
self.reason = recorded_response['status']['message']
|
||||
self.status = recorded_response['status']['code']
|
||||
self.version = None
|
||||
self._content = StringIO(self.recorded_response['body']['string'])
|
||||
|
||||
self.msg = HTTPMessage(StringIO(''))
|
||||
for k, v in self.recorded_response['headers'].iteritems():
|
||||
self.msg.addheader(k, v)
|
||||
|
||||
self.length = self.msg.getheader('content-length') or None
|
||||
|
||||
def read(self, chunked=False):
|
||||
# Note: I'm pretty much ignoring any chunking stuff because
|
||||
# I don't really understand what it is or how it works.
|
||||
return self._content.read()
|
||||
|
||||
def isclosed(self):
|
||||
# Urllib3 seems to call this because it actually uses
|
||||
# the weird chunking support in httplib
|
||||
return True
|
||||
|
||||
def getheaders(self):
|
||||
return self.recorded_response['headers'].iteritems()
|
||||
|
||||
|
||||
class VCRConnectionMixin:
|
||||
def _load_old_response(self):
|
||||
|
||||
Reference in New Issue
Block a user