mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
PEP8 cleanup and make it actually work.
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
#VCR.py
|
||||
|
||||
This is a proof-of-concept start at a python version of [Ruby's VCR
|
||||
library](https://github.com/myronmarston/vcr). It
|
||||
doesn't actually work.
|
||||
library](https://github.com/myronmarston/vcr).
|
||||
|
||||
#What it is supposed to do
|
||||
Simplify testing by recording all HTTP interactions and saving them to
|
||||
@@ -10,10 +9,6 @@ Simplify testing by recording all HTTP interactions and saving them to
|
||||
again, they all just hit the text files instead of the internet. This speeds up
|
||||
your tests and lets you work offline.
|
||||
|
||||
#What it actually does
|
||||
Uses up all your memory
|
||||
|
||||
|
||||
#Similar libraries in Python
|
||||
Neither of these really implement the API I want, but I have cribbed some code
|
||||
from them.
|
||||
|
||||
37
vcr/stubs.py
37
vcr/stubs.py
@@ -1,16 +1,19 @@
|
||||
from httplib import HTTPConnection
|
||||
from cStringIO import StringIO
|
||||
from .files import save_cassette, load_cassette
|
||||
from .cassette import Cassette
|
||||
|
||||
|
||||
class VCRHTTPResponse(object):
|
||||
def __init__(self,recorded_response):
|
||||
def __init__(self, recorded_response):
|
||||
self.recorded_response = recorded_response
|
||||
self.msg = recorded_response['status']['message']
|
||||
self.reason = recorded_response['status']['message']
|
||||
self.status = recorded_response['status']['code']
|
||||
self._content = StringIO(self.recorded_response['body']['string'])
|
||||
|
||||
def read(self,chunked=False):
|
||||
return self.recorded_response['body']['string']
|
||||
def read(self, chunked=False):
|
||||
return self._content.read()
|
||||
|
||||
def getheaders(self):
|
||||
return self.recorded_response['headers']
|
||||
@@ -18,34 +21,34 @@ class VCRHTTPResponse(object):
|
||||
|
||||
class VCRHTTPConnection(HTTPConnection):
|
||||
|
||||
def __init__(self,*args,**kwargs):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._cassette = Cassette()
|
||||
HTTPConnection.__init__(self,*args,**kwargs)
|
||||
HTTPConnection.__init__(self, *args, **kwargs)
|
||||
|
||||
def _save_cassette(self):
|
||||
save_cassette(self._vcr_cassette_path,self._cassette)
|
||||
save_cassette(self._vcr_cassette_path, self._cassette)
|
||||
|
||||
def request(self,method,url,body=None,headers={}):
|
||||
def request(self, method, url, body=None, headers={}):
|
||||
old_cassette = load_cassette(self._vcr_cassette_path)
|
||||
if old_cassette:
|
||||
return
|
||||
self._cassette.requests.append(dict(
|
||||
method = method,
|
||||
url = url,
|
||||
body = body,
|
||||
headers = headers
|
||||
method=method,
|
||||
url=url,
|
||||
body=body,
|
||||
headers=headers
|
||||
))
|
||||
return HTTPConnection.request(self,method,url,body=body,headers=headers)
|
||||
return HTTPConnection.request(self, method, url, body=body, headers=headers)
|
||||
|
||||
def getresponse(self,buffering=False):
|
||||
def getresponse(self, buffering=False):
|
||||
old_cassette = load_cassette(self._vcr_cassette_path)
|
||||
if old_cassette:
|
||||
return VCRHTTPResponse(old_cassette[0]['response'])
|
||||
response = HTTPConnection.getresponse(self,buffering=buffering)
|
||||
response = HTTPConnection.getresponse(self, buffering=buffering)
|
||||
self._cassette.responses.append({
|
||||
'status':{'code':response.status,'message':response.reason},
|
||||
'headers':dict(response.getheaders()),
|
||||
'body':{'string':response.read()},
|
||||
'status': {'code': response.status, 'message': response.reason},
|
||||
'headers': dict(response.getheaders()),
|
||||
'body': {'string': response.read()},
|
||||
})
|
||||
self._save_cassette()
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user