1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

Ignore requests before trying to play them

Closes #79
This commit is contained in:
Kevin McCarthy
2014-05-12 21:55:59 -10:00
parent 3072c56ed2
commit 8d90dba16c
3 changed files with 30 additions and 17 deletions

View File

@@ -39,3 +39,17 @@ def test_ignore_localhost_and_httpbin(tmpdir, httpserver):
urlopen('http://httpbin.org')
urlopen(httpserver.url)
assert len(cass) == 0
def test_ignore_localhost_twice(tmpdir, httpserver):
httpserver.serve_content('Hello!')
cass_file = str(tmpdir.join('filter_qs.yaml'))
with vcr.use_cassette(cass_file, ignore_localhost=True) as cass:
urlopen(httpserver.url)
assert len(cass) == 0
urlopen('http://httpbin.org')
assert len(cass) == 1
with vcr.use_cassette(cass_file, ignore_localhost=True) as cass:
assert len(cass) == 1
urlopen(httpserver.url)
urlopen('http://httpbin.org')
assert len(cass) == 1

View File

@@ -80,15 +80,18 @@ class Cassette(ContextDecorator):
return self.rewound and self.record_mode == 'once' or \
self.record_mode == 'none'
def append(self, request, response):
'''Add a request, response pair to this cassette'''
request = filter_request(
def _filter_request(self, request):
return filter_request(
request=request,
filter_headers=self._filter_headers,
filter_query_parameters=self._filter_query_parameters,
before_record=self._before_record,
ignore_hosts=self._ignore_hosts
)
def append(self, request, response):
'''Add a request, response pair to this cassette'''
request = self._filter_request(request)
if not request:
return
self.data.append((request, response))
@@ -99,19 +102,19 @@ class Cassette(ContextDecorator):
internal API, returns an iterator with all responses matching
the request.
"""
request = filter_request(
request=request,
filter_headers=self._filter_headers,
filter_query_parameters=self._filter_query_parameters,
before_record=self._before_record,
ignore_hosts=self._ignore_hosts
)
request = self._filter_request(request)
if not request:
return
for index, (stored_request, response) in enumerate(self.data):
if requests_match(request, stored_request, self._match_on):
yield index, response
def can_play_response_for(self, request):
request = self._filter_request(request)
return request and request in self and \
self.record_mode != 'all' and \
self.rewound
def play_response(self, request):
'''
Get the response corresponding to a request, but only if it

View File

@@ -204,9 +204,7 @@ class VCRConnection:
'''Retrieve a the response'''
# 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:
if self.cassette.can_play_response_for(self._vcr_request):
log.info(
"Playing response for {0} from cassette".format(
self._vcr_request
@@ -215,7 +213,7 @@ class VCRConnection:
response = self.cassette.play_response(self._vcr_request)
return VCRHTTPResponse(response)
else:
if self.cassette.write_protected:
if self.cassette.write_protected and self.cassette._filter_request(self._vcr_request):
raise CannotOverwriteExistingCassetteException(
"Can't overwrite existing cassette (%r) in "
"your current record mode (%r)."
@@ -264,9 +262,7 @@ class VCRConnection:
"""
if hasattr(self, '_vcr_request') and \
self._vcr_request in self.cassette and \
self.cassette.record_mode != "all" and \
self.cassette.rewound:
self.cassette.can_play_response_for(self._vcr_request):
# We already have a response we are going to play, don't
# actually connect
return