vcrpy
vcrpy copied to clipboard
feat: use regex for ignoring hosts
Allow to use regexes for ignore_hosts
to easily ignore hosts that are frequently changed e.g. pipelinesghubeus*.actions.githubusercontent.com
I would like to note that this change would break API backwards-compatibility (e.g. because .
no longer matches just literal dots) and hence would need a major version bump if merged.
@hartwork do you have any suggestion on how to modify it to make it API backwards compatible?
The signature of ignore_hosts
could be like this:
ignore_hosts: set[str | re.Pattern]
In the _build_ignore_hosts
you can check the type of each item of set
and do the validation for strings and regex.
Correct me if I am wrong, but that will work only if the regex pattern is compiled, re.compile("regex")
. If it's passed as a normal string r"regex"
then it's just a plain string when checking the type so it will use the non-regex implementation.
Even if the function signature is changed, that still has an interaction with the ignore_localhost
option which adds multiple items to the (potentially empty) hosts_to_ignore
set. If the handling of that that option was changed to just add its own filter function, then the function _build_ignore_hosts
could change to the following:
@staticmethod
def _build_ignore_hosts(hosts_to_ignore):
if isinstance(re.Pattern):
def filter_ignored_hosts(request):
# DISCUSS: Would need to decide if search() or match() is best here.
# search() tends to be more user friendly behavior
# match() is more specific though.
if hasattr(request, "host") and hosts_to_ignore.search(request.host):
return
return request
else:
hosts_to_ignore = set(hosts_to_ignore)
def filter_ignored_hosts(request):
if hasattr(request, "host") and request.host in hosts_to_ignore:
return
return request
return filter_ignored_hosts
Then in the config.py code:
if ignore_localhost:
filter_functions.append(self._build_ignore_hosts(("localhost", "0.0.0.0", "127.0.0.1")))
if ignore_hosts:
filter_functions.append(self._build_ignore_hosts(ignore_hosts))
This should be backwards compatible, at the cost of splitting out the localhost filter function from the existing host filters.