mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
New Feature: Ignore Some Requests
Add 2 new options, ignore_localhost and ignore_hosts, which can ignore requests so they aren't recorded in a cassette. Closes #74
This commit is contained in:
@@ -34,6 +34,8 @@ class Cassette(ContextDecorator):
|
||||
filter_headers=[],
|
||||
filter_query_parameters=[],
|
||||
before_record=None,
|
||||
ignore_hosts=[],
|
||||
ignore_localhost=[],
|
||||
):
|
||||
self._path = path
|
||||
self._serializer = serializer
|
||||
@@ -41,6 +43,11 @@ class Cassette(ContextDecorator):
|
||||
self._filter_headers = filter_headers
|
||||
self._filter_query_parameters = filter_query_parameters
|
||||
self._before_record = before_record
|
||||
self._ignore_hosts = ignore_hosts
|
||||
if ignore_localhost:
|
||||
self._ignore_hosts = list(set(
|
||||
self._ignore_hosts + ['localhost', '0.0.0.0', '127.0.0.1']
|
||||
))
|
||||
|
||||
# self.data is the list of (req, resp) tuples
|
||||
self.data = []
|
||||
@@ -72,7 +79,8 @@ class Cassette(ContextDecorator):
|
||||
request=request,
|
||||
filter_headers=self._filter_headers,
|
||||
filter_query_parameters=self._filter_query_parameters,
|
||||
before_record=self._before_record
|
||||
before_record=self._before_record,
|
||||
ignore_hosts=self._ignore_hosts
|
||||
)
|
||||
if not request:
|
||||
return
|
||||
@@ -88,7 +96,8 @@ class Cassette(ContextDecorator):
|
||||
request=request,
|
||||
filter_headers=self._filter_headers,
|
||||
filter_query_parameters=self._filter_query_parameters,
|
||||
before_record=self._before_record
|
||||
before_record=self._before_record,
|
||||
ignore_hosts=self._ignore_hosts
|
||||
)
|
||||
if not request:
|
||||
return
|
||||
|
||||
@@ -20,6 +20,8 @@ class VCR(object):
|
||||
'path',
|
||||
'query',
|
||||
],
|
||||
ignore_hosts=[],
|
||||
ignore_localhost=False,
|
||||
):
|
||||
self.serializer = serializer
|
||||
self.match_on = match_on
|
||||
@@ -44,6 +46,8 @@ class VCR(object):
|
||||
self.filter_headers = filter_headers
|
||||
self.filter_query_parameters = filter_query_parameters
|
||||
self.before_record = before_record
|
||||
self.ignore_hosts = ignore_hosts
|
||||
self.ignore_localhost = ignore_localhost
|
||||
|
||||
def _get_serializer(self, serializer_name):
|
||||
try:
|
||||
@@ -82,9 +86,21 @@ class VCR(object):
|
||||
"serializer": self._get_serializer(serializer_name),
|
||||
"match_on": self._get_matchers(matcher_names),
|
||||
"record_mode": kwargs.get('record_mode', self.record_mode),
|
||||
"filter_headers": kwargs.get('filter_headers', self.filter_headers),
|
||||
"filter_query_parameters": kwargs.get('filter_query_parameters', self.filter_query_parameters),
|
||||
"before_record": kwargs.get("before_record", self.before_record),
|
||||
"filter_headers": kwargs.get(
|
||||
'filter_headers', self.filter_headers
|
||||
),
|
||||
"filter_query_parameters": kwargs.get(
|
||||
'filter_query_parameters', self.filter_query_parameters
|
||||
),
|
||||
"before_record": kwargs.get(
|
||||
"before_record", self.before_record
|
||||
),
|
||||
"ignore_hosts": kwargs.get(
|
||||
'ignore_hosts', self.ignore_hosts
|
||||
),
|
||||
"ignore_localhost": kwargs.get(
|
||||
'ignore_localhost', self.ignore_localhost
|
||||
),
|
||||
}
|
||||
|
||||
return Cassette.load(path, **merged_config)
|
||||
|
||||
@@ -28,11 +28,14 @@ def filter_request(
|
||||
request,
|
||||
filter_headers,
|
||||
filter_query_parameters,
|
||||
before_record
|
||||
before_record,
|
||||
ignore_hosts
|
||||
):
|
||||
request = copy.copy(request) # don't mutate request object
|
||||
if hasattr(request, 'headers') and filter_headers:
|
||||
request = _remove_headers(request, filter_headers)
|
||||
if hasattr(request, 'host') and request.host in ignore_hosts:
|
||||
return None
|
||||
if filter_query_parameters:
|
||||
request = _remove_query_parameters(request, filter_query_parameters)
|
||||
if before_record:
|
||||
|
||||
Reference in New Issue
Block a user