Skip to content

Commit

Permalink
feat: use regex for ignoring hosts
Browse files Browse the repository at this point in the history
Allow to use regexes for `ignore_hosts` to easily ignore hosts that are frequently changed e.g. `pipelinesghubeus*.actions.githubusercontent.com`
  • Loading branch information
silviumarcu committed Nov 7, 2023
1 parent defad28 commit b0fd86e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/integration/test_ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def test_ignore_httpbin(tmpdir, httpbin):
assert len(cass) == 1


def test_ignore_httpbin_regex(tmpdir, httpbin):
with overridden_dns({"httpbin.org": "127.0.0.1"}):
cass_file = str(tmpdir.join("filter_qs.yaml"))
with vcr.use_cassette(cass_file, ignore_hosts=[r"h\w*bin.+"]) as cass:
urlopen(f"http://httpbin.org:{httpbin.port}/")
assert len(cass) == 0


def test_ignore_localhost_and_httpbin(tmpdir, httpbin):
with overridden_dns({"httpbin.org": "127.0.0.1"}):
cass_file = str(tmpdir.join("filter_qs.yaml"))
Expand Down
5 changes: 4 additions & 1 deletion vcr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import os
import types
import re
from collections import abc as collections_abc
from pathlib import Path

Expand Down Expand Up @@ -231,8 +232,10 @@ def before_record_request(request):

@staticmethod
def _build_ignore_hosts(hosts_to_ignore):
hosts_to_ignore_re = re.compile("(" + ")|(".join(hosts_to_ignore) + ")")

def filter_ignored_hosts(request):
if hasattr(request, "host") and request.host in hosts_to_ignore:
if hasattr(request, "host") and bool(hosts_to_ignore_re.match(request.host)):
return
return request

Expand Down

0 comments on commit b0fd86e

Please sign in to comment.