mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
check that cassette contains things before returning
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import mock
|
import pytest
|
||||||
import yaml
|
import yaml
|
||||||
from vcr.cassette import Cassette
|
from vcr.cassette import Cassette
|
||||||
|
|
||||||
@@ -56,4 +56,5 @@ def test_cassette_response():
|
|||||||
|
|
||||||
def test_cassette_missing_response():
|
def test_cassette_missing_response():
|
||||||
a = Cassette('test')
|
a = Cassette('test')
|
||||||
assert not a.response('foo')
|
with pytest.raises(KeyError):
|
||||||
|
a.response('foo')
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ class Cassette(object):
|
|||||||
} for req, res in zip(self._requests, self._responses)])
|
} for req, res in zip(self._requests, self._responses)])
|
||||||
|
|
||||||
def deserialize(self, source):
|
def deserialize(self, source):
|
||||||
'''Given a seritalized version, load the requests'''
|
'''Given a serialized version, load the requests'''
|
||||||
self._requests, self._responses = (
|
self._requests, self._responses = (
|
||||||
[r['request'] for r in source], [r['response'] for r in source])
|
[r['request'] for r in source], [r['response'] for r in source])
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ class Cassette(object):
|
|||||||
self.play_count += 1
|
self.play_count += 1
|
||||||
|
|
||||||
def append(self, request, response):
|
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._requests.append(request)
|
||||||
self._responses.append(response)
|
self._responses.append(response)
|
||||||
|
|
||||||
@@ -70,8 +70,7 @@ class Cassette(object):
|
|||||||
try:
|
try:
|
||||||
return self._responses[self._requests.index(request)]
|
return self._responses[self._requests.index(request)]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
#todo: keyerror if not in cassette
|
raise KeyError
|
||||||
return None
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
'''Patch the fetching libraries we know about'''
|
'''Patch the fetching libraries we know about'''
|
||||||
|
|||||||
@@ -65,13 +65,13 @@ class VCRConnectionMixin:
|
|||||||
'''Retrieve a the response'''
|
'''Retrieve a the response'''
|
||||||
# Check to see if the cassette has a response for this request. If so,
|
# Check to see if the cassette has a response for this request. If so,
|
||||||
# then return it
|
# then return it
|
||||||
|
if self._request in self.cassette:
|
||||||
response = self.cassette.response(self._request)
|
response = self.cassette.response(self._request)
|
||||||
if response:
|
|
||||||
# Alert the cassette to the fact that we've served another
|
# Alert the cassette to the fact that we've served another
|
||||||
# response for the provided requests
|
# response for the provided requests
|
||||||
self.cassette.mark_played()
|
self.cassette.mark_played()
|
||||||
return VCRHTTPResponse(response)
|
return VCRHTTPResponse(response)
|
||||||
|
else:
|
||||||
# Otherwise, we made an actual request, and should return the response
|
# Otherwise, we made an actual request, and should return the response
|
||||||
# we got from the actual connection
|
# we got from the actual connection
|
||||||
response = HTTPConnection.getresponse(self)
|
response = HTTPConnection.getresponse(self)
|
||||||
|
|||||||
Reference in New Issue
Block a user