} for req, res in zip(self._requests, self._responses)])
def deserialize(self, source):
- '''Given a seritalized version, load the requests'''
+ '''Given a serialized version, load the requests'''
self._requests, self._responses = (
[r['request'] for r in source], [r['response'] for r in source])
self.play_count += 1
def append(self, request, response):
- '''Add a pair of request, response pairs to this cassette'''
+ '''Add a pair of request, response to this cassette'''
self._requests.append(request)
self._responses.append(response)
try:
return self._responses[self._requests.index(request)]
except ValueError:
- #todo: keyerror if not in cassette
- return None
+ raise KeyError
def __enter__(self):
'''Patch the fetching libraries we know about'''
'''Retrieve a the response'''
# Check to see if the cassette has a response for this request. If so,
# then return it
- response = self.cassette.response(self._request)
- if response:
+ if self._request in self.cassette:
+ response = self.cassette.response(self._request)
# Alert the cassette to the fact that we've served another
# response for the provided requests
self.cassette.mark_played()
return VCRHTTPResponse(response)
-
- # Otherwise, we made an actual request, and should return the response
- # we got from the actual connection
- response = HTTPConnection.getresponse(self)
- response = {
- 'status': {'code': response.status, 'message': response.reason},
- 'headers': dict(response.getheaders()),
- 'body': {'string': response.read()},
- }
- self.cassette.append(self._request, response)
+ else:
+ # Otherwise, we made an actual request, and should return the response
+ # we got from the actual connection
+ response = HTTPConnection.getresponse(self)
+ response = {
+ 'status': {'code': response.status, 'message': response.reason},
+ 'headers': dict(response.getheaders()),
+ 'body': {'string': response.read()},
+ }
+ self.cassette.append(self._request, response)
return VCRHTTPResponse(response)