]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
PEP8 cleanup and make it actually work.
authorKevin McCarthy <me@kevinmccarthy.org>
Thu, 31 May 2012 07:03:05 +0000 (21:03 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Thu, 31 May 2012 07:03:05 +0000 (21:03 -1000)
README.md
vcr/stubs.py

index 7dd1653eb21a580b8845e51f10c9728aa490e392..885fa59b846307b26b6efd69f43b1401369ef3f3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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.
index ff3a28ec8518605f3a8c323682d66f382f031712..b19c70046931e687da0ee01e490fde2cdd705115 100644 (file)
@@ -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