Skip to content

Commit 0265fcd

Browse files
authored
refactor: iterative wildcard collapsing and add test for consecutive wildcards (#1012)
Co-authored-by: srpvpn <[email protected]>
1 parent cf537d4 commit 0265fcd

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

context.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ func (x *Context) RoutePattern() string {
133133
return routePattern
134134
}
135135

136-
// replaceWildcards takes a route pattern and recursively replaces all
137-
// occurrences of "/*/" to "/".
136+
// replaceWildcards takes a route pattern and replaces all occurrences of
137+
// "/*/" with "/". It iteratively runs until no wildcards remain to
138+
// correctly handle consecutive wildcards.
138139
func replaceWildcards(p string) string {
139-
if strings.Contains(p, "/*/") {
140-
return replaceWildcards(strings.Replace(p, "/*/", "/", -1))
140+
for strings.Contains(p, "/*/") {
141+
p = strings.ReplaceAll(p, "/*/", "/")
141142
}
142143
return p
143144
}

context_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,14 @@ func TestRoutePattern(t *testing.T) {
9191
t.Fatalf("unexpected non-empty route pattern for nil context: %q", p)
9292
}
9393
}
94+
95+
// TestReplaceWildcardsConsecutive ensures multiple consecutive wildcards are
96+
// collapsed into a single slash.
97+
func TestReplaceWildcardsConsecutive(t *testing.T) {
98+
if p := replaceWildcards("/foo/*/*/*/bar"); p != "/foo/bar" {
99+
t.Fatalf("unexpected wildcard replacement: %s", p)
100+
}
101+
if p := replaceWildcards("/foo/*/*/*/bar/*"); p != "/foo/bar/*" {
102+
t.Fatalf("unexpected trailing wildcard behavior: %s", p)
103+
}
104+
}

0 commit comments

Comments
 (0)