Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture multiple named groups again #2432

Merged
merged 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/django/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_regex(resolver_or_pattern):

class RavenResolver(object):
_optional_group_matcher = re.compile(r"\(\?\:([^\)]+)\)")
_named_group_matcher = re.compile(r"\(\?P<(\w+)>.*\)")
_named_group_matcher = re.compile(r"\(\?P<(\w+)>[^\)]+\)+")
_non_named_group_matcher = re.compile(r"\([^\)]+\)")
# [foo|bar|baz]
_either_option_matcher = re.compile(r"\[([^\]]+)\|([^\]]+)\]")
Expand Down
29 changes: 19 additions & 10 deletions tests/integrations/django/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
example_url_conf = (
url(r"^api/(?P<project_id>[\w_-]+)/store/$", lambda x: ""),
url(r"^api/(?P<version>(v1|v2))/author/$", lambda x: ""),
url(
r"^api/(?P<project_id>[^\/]+)/product/(?P<pid>(?:\d+|[A-Fa-f0-9-]{32,36}))/$",
lambda x: "",
),
url(r"^report/", lambda x: ""),
url(r"^example/", include(included_url_conf)),
url(
r"^(?P<slug>[$\\-_.+!*(),\\w//]+)/$", lambda x: ""
), # example of complex regex from django-cms
)


Expand Down Expand Up @@ -56,14 +57,12 @@ def test_legacy_resolver_included_match():
assert result == "/example/foo/bar/{param}"


def test_complex_regex_from_django_cms():
"""
Reference: https://github.com/getsentry/sentry-python/issues/1527
"""

def test_capture_multiple_named_groups():
resolver = RavenResolver()
result = resolver.resolve("/,/", example_url_conf)
assert result == "/{slug}/"
result = resolver.resolve(
"/api/myproject/product/cb4ef1caf3554c34ae134f3c1b3d605f/", example_url_conf
)
assert result == "/api/{project_id}/product/{pid}/"


@pytest.mark.skipif(django.VERSION < (2, 0), reason="Requires Django > 2.0")
Expand All @@ -74,3 +73,13 @@ def test_legacy_resolver_newstyle_django20_urlconf():
resolver = RavenResolver()
result = resolver.resolve("/api/v2/1234/store/", url_conf)
assert result == "/api/v2/{project_id}/store/"


@pytest.mark.skipif(django.VERSION < (2, 0), reason="Requires Django > 2.0")
def test_legacy_resolver_newstyle_django20_urlconf_multiple_groups():
from django.urls import path

url_conf = (path("api/v2/<int:project_id>/product/<int:pid>", lambda x: ""),)
resolver = RavenResolver()
result = resolver.resolve("/api/v2/1234/product/5689", url_conf)
assert result == "/api/v2/{project_id}/product/{pid}"