Skip to content

Commit

Permalink
Regex support added for IGNORE_URLS with test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurav Sharma authored and Saurav Sharma committed Feb 15, 2024
1 parent 00d163f commit b9d7498
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
16 changes: 8 additions & 8 deletions django_guid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ def is_url_in_ignored_list(request: Union['HttpRequest', 'HttpResponse']) -> boo
:return: Boolean
"""
endpoint = request.path.strip('/')
for ignore_url in settings.ignore_urls:
pattern = ignore_url.replace('*', r'[\s\S]+') # noqa
pattern = '^' + pattern + '$'
search = re.search(pattern, endpoint)
if search:
# logger.info("URL Ignored")
return True
# logger.info("URL not Ignored")

IGNORE_URLS = []
for url in settings.ignore_urls:
url_regex = url.replace('*', r'[\s\S]*') # noqa
url_regex = '^' + url_regex + '$'
IGNORE_URLS.append(re.compile(url_regex))
if any(url.match(endpoint) for url in IGNORE_URLS):
return True
return False
44 changes: 44 additions & 0 deletions tests/functional/test_sync_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,47 @@ def test_url_ignored(client, caplog):
('Received signal `request_finished`, clearing guid', None),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected


def test_url_ignored_without_regex(client, caplog):
"""
Test that a URL without regex pattern specified in IGNORE_URLS is ignored.
:param client: Django client
:param caplog: Caplog fixture
"""
from django.conf import settings as django_settings

mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['IGNORE_URLS'] = {'no-guid'}
with override_settings(DJANGO_GUID=mocked_settings):
client.get('/no-guid', **{'HTTP_Correlation-ID': 'bad-guid'})
# No log message should have a GUID, aka `None` on index 1.
expected = [
('sync middleware called', None),
('This log message should NOT have a GUID - the URL is in IGNORE_URLS', None),
('Some warning in a function', None),
('Received signal `request_finished`, clearing guid', None),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected


def test_url_ignored_with_regex(client, caplog):
"""
Test that a URL with regex pattern specified in IGNORE_URLS is ignored.
:param client: Django client
:param caplog: Caplog fixture
"""
from django.conf import settings as django_settings

mocked_settings = deepcopy(django_settings.DJANGO_GUID)
mocked_settings['IGNORE_URLS'] = {'no-guid/*'}
with override_settings(DJANGO_GUID=mocked_settings):
client.get('/no-guid/regex-test', **{'HTTP_Correlation-ID': 'bad-guid'})
# No log message should have a GUID, aka `None` on index 1.
expected = [
('sync middleware called', None),
('This log message should NOT have a GUID - the URL is in IGNORE_URLS', None),
('Some warning in a function', None),
('Received signal `request_finished`, clearing guid', None),
]
assert [(x.message, x.correlation_id) for x in caplog.records] == expected

0 comments on commit b9d7498

Please sign in to comment.