1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

Fix API by adding 'responses_of' method

`responses_of` replaces `response_of`, since each request can have
several matching responses now.

This breaks backwards compatibility if you are using the
response_of method, so a version bump will be required.
This commit is contained in:
Kevin McCarthy
2013-12-01 14:26:35 -10:00
parent b84f8e963b
commit 188b57a2fa
4 changed files with 29 additions and 8 deletions

View File

@@ -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}