From 4ad6be7787d305539ac9852ed3425622d7b8755f Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Wed, 30 May 2012 21:03:05 -1000 Subject: [PATCH] PEP8 cleanup and make it actually work. --- README.md | 7 +------ vcr/stubs.py | 37 ++++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 7dd1653..885fa59 100644 --- 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. diff --git a/vcr/stubs.py b/vcr/stubs.py index ff3a28e..b19c700 100644 --- a/vcr/stubs.py +++ b/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