diff --git a/README.md b/README.md index 0eab321..86d29f3 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ part of the API. The fields are as follows: * `responses`: A list of the responses made. * `play_count`: The number of times this cassette has had a response played back -* `response_of(request)`: Access the response for a given request. +* `responses_of(request)`: Access the responses that match a given request The Request object has the following properties diff --git a/tests/unit/test_cassettes.py b/tests/unit/test_cassettes.py index bc95219..39b6f42 100644 --- a/tests/unit/test_cassettes.py +++ b/tests/unit/test_cassettes.py @@ -44,14 +44,22 @@ def test_cassette_contains(): @mock.patch('vcr.cassette.requests_match', _mock_requests_match) -def test_cassette_response_of(): +def test_cassette_responses_of(): a = Cassette('test') a.append('foo', 'bar') - assert a.response_of('foo') == 'bar' + assert a.responses_of('foo') == ['bar'] @mock.patch('vcr.cassette.requests_match', _mock_requests_match) def test_cassette_get_missing_response(): a = Cassette('test') with pytest.raises(KeyError): - a.response_of('foo') + a.responses_of('foo') + +@mock.patch('vcr.cassette.requests_match', _mock_requests_match) +def test_cassette_cant_read_same_request_twice(): + a = Cassette('test') + a.append('foo','bar') + a.play_response('foo') + with pytest.raises(KeyError): + a.play_response('foo') diff --git a/vcr/cassette.py b/vcr/cassette.py index c4e711b..8e6816f 100644 --- a/vcr/cassette.py +++ b/vcr/cassette.py @@ -61,10 +61,10 @@ class Cassette(object): self.data.append((request, response)) self.dirty = True - def response_of(self, request): + def play_response(self, request): ''' - Find the response corresponding to a request - + Get the response corresponding to a request, but only if it + hasn't been played back before, and mark it as playe.d ''' for index, (stored_request, response) in enumerate(self.data): if requests_match(request, stored_request, self._match_on): @@ -75,6 +75,19 @@ class Cassette(object): # if the cassette doesn't contain the request asked for. raise KeyError + def responses_of(self, request): + ''' + Find the responses corresponding to a request. + This function isn't actually used by VCR internally, but is + provided as an external API. + ''' + responses = [resp for req, resp in self.data if requests_match(req, request, self._match_on)] + 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 + def _as_dict(self): return {"requests": self.requests, "responses": self.responses} diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index d389f82..5eb7548 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -166,7 +166,7 @@ class VCRConnectionMixin: # Check to see if the cassette has a response for this request. If so, # then return it if self._vcr_request in self.cassette and self.cassette.record_mode != "all" and self.cassette.rewound: - response = self.cassette.response_of(self._vcr_request) + response = self.cassette.play_response(self._vcr_request) return VCRHTTPResponse(response) else: if self.cassette.write_protected: