1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-10 01:25:34 +00:00

check that cassette contains things before returning

This commit is contained in:
Kevin McCarthy
2013-08-06 18:34:56 -10:00
parent 37504c8982
commit 166facd0d0
3 changed files with 18 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
import mock
import pytest
import yaml
from vcr.cassette import Cassette
@@ -56,4 +56,5 @@ def test_cassette_response():
def test_cassette_missing_response():
a = Cassette('test')
assert not a.response('foo')
with pytest.raises(KeyError):
a.response('foo')

View File

@@ -38,7 +38,7 @@ class Cassette(object):
} 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])
@@ -49,7 +49,7 @@ class Cassette(object):
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)
@@ -70,8 +70,7 @@ class Cassette(object):
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'''

View File

@@ -65,13 +65,13 @@ class VCRConnectionMixin:
'''Retrieve a the response'''
# Check to see if the cassette has a response for this request. If so,
# then return it
if self._request in self.cassette:
response = self.cassette.response(self._request)
if response:
# Alert the cassette to the fact that we've served another
# response for the provided requests
self.cassette.mark_played()
return VCRHTTPResponse(response)
else:
# Otherwise, we made an actual request, and should return the response
# we got from the actual connection
response = HTTPConnection.getresponse(self)