]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
check that cassette contains things before returning
authorKevin McCarthy <me@kevinmccarthy.org>
Wed, 7 Aug 2013 04:34:56 +0000 (18:34 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Mon, 12 Aug 2013 01:05:40 +0000 (15:05 -1000)
tests/unit/test_cassettes.py
vcr/cassette.py
vcr/stubs/__init__.py

index 18104b687302605229e9bed9726554fcfa88a306..bd42fe2eef1bf5502ae84912681b836e39da296a 100644 (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')
index 27c93e9ef70848d8a37ad06e0225c1bb3c513713..6eefbc36931ee9474fb7fc9557007d8f0a48a85a 100644 (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'''
index 3c93677ec0e182222213e6b387df5c66940f41ed..693f2e03d0b481b0668c8248a8521643503acbf4 100644 (file)
@@ -65,22 +65,22 @@ class VCRConnectionMixin:
         '''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)