import pytest
import yaml
import mock
-from vcr.cassette import Cassette
+from vcr.cassette import Cassette, UnhandledHTTPRequestError
def test_cassette_load(tmpdir):
@mock.patch('vcr.cassette.requests_match', _mock_requests_match)
def test_cassette_get_missing_response():
a = Cassette('test')
- with pytest.raises(KeyError):
+ with pytest.raises(UnhandledHTTPRequestError):
a.responses_of('foo')
@mock.patch('vcr.cassette.requests_match', _mock_requests_match)
a = Cassette('test')
a.append('foo','bar')
a.play_response('foo')
- with pytest.raises(KeyError):
+ with pytest.raises(UnhandledHTTPRequestError):
a.play_response('foo')
from .matchers import requests_match, url, method
+class UnhandledHTTPRequestError(Exception):
+ pass
+
+
class Cassette(ContextDecorator):
'''A container for recorded requests and responses'''
if self.play_counts[index] == 0:
self.play_counts[index] += 1
return response
- # I decided that a KeyError is the best exception to raise
- # if the cassette doesn't contain the request asked for.
- raise KeyError
+ # The cassette doesn't contain the request asked for.
+ raise UnhandledHTTPRequestError(
+ "The cassette (%r) doesn't contain the request (%r) asked for"
+ % (self._path,request))
def responses_of(self, request):
'''
if responses:
return responses
- # I decided that a KeyError is the best exception to raise
- # if the cassette doesn't contain the request asked for.
- raise KeyError
+ # The cassette doesn't contain the request asked for.
+ raise UnhandledHTTPRequestError(
+ "The cassette (%r) doesn't contain the request (%r) asked for"
+ % (self._path,request))
def _as_dict(self):
return {"requests": self.requests, "responses": self.responses}