You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// For full segment wildcards, try consuming different numbers of path segments
32
+
if(currentPatternPart==="*"){
33
+
34
+
// Try consuming 0 segments (skip the wildcard)
35
+
if(matchSegments(patternIndex+1,pathIndex)){
36
+
returntrue;
37
+
}
38
+
39
+
// Try consuming current segment and recursively try rest
40
+
if(matchSegments(patternIndex,pathIndex+1)){
41
+
returntrue;
42
+
}
43
+
44
+
returnfalse;
45
+
}
46
+
47
+
// Check for in-segment wildcard (e.g., "prefix*" or "prefix*suffix")
48
+
if(currentPatternPart.includes("*")){
49
+
// Convert the pattern segment to a regex pattern
50
+
constregexPattern=currentPatternPart
51
+
.replace(/\*/g,".*")// Replace * with .* for regex wildcard
52
+
.replace(/\?/g,".");// Replace ? with . for single character wildcard if needed
53
+
54
+
constregex=newRegExp(`^${regexPattern}$`);
55
+
56
+
if(regex.test(currentPathPart)){
57
+
returnmatchSegments(patternIndex+1,pathIndex+1);
58
+
}
59
+
60
+
returnfalse;
61
+
}
62
+
63
+
// For regular segments, they must match exactly
64
+
if(currentPatternPart!==currentPathPart){
65
+
returnfalse;
66
+
}
67
+
68
+
// Move to next segments in both pattern and path
69
+
returnmatchSegments(patternIndex+1,pathIndex+1);
70
+
}
71
+
72
+
constresult=matchSegments(0,0);
73
+
returnresult;
74
+
}
75
+
4
76
functionrunTests(){
5
77
console.log('Running path matching tests...');
6
-
78
+
7
79
// Test exact matching
8
80
assertEquals(isPathAllowed('foo','foo'),true,'Exact match should be allowed');
9
81
assertEquals(isPathAllowed('foo','bar'),false,'Different segments should not match');
10
82
assertEquals(isPathAllowed('foo/bar','foo/bar'),true,'Exact multi-segment match should be allowed');
11
83
assertEquals(isPathAllowed('foo/bar','foo/baz'),false,'Partial multi-segment match should not be allowed');
12
-
84
+
13
85
// Test with leading and trailing slashes
14
86
assertEquals(isPathAllowed('/foo','foo'),true,'Pattern with leading slash should match');
15
87
assertEquals(isPathAllowed('foo/','foo'),true,'Pattern with trailing slash should match');
16
88
assertEquals(isPathAllowed('/foo/','foo'),true,'Pattern with both leading and trailing slashes should match');
17
89
assertEquals(isPathAllowed('foo','/foo/'),true,'Path with leading and trailing slashes should match');
18
-
90
+
19
91
// Test simple wildcard matching
20
92
assertEquals(isPathAllowed('*','foo'),true,'Single wildcard should match any single segment');
21
93
assertEquals(isPathAllowed('*','foo/bar'),true,'Single wildcard should match multiple segments');
22
94
assertEquals(isPathAllowed('*/bar','foo/bar'),true,'Wildcard prefix should match');
23
95
assertEquals(isPathAllowed('foo/*','foo/bar'),true,'Wildcard suffix should match');
24
96
assertEquals(isPathAllowed('foo/*/baz','foo/bar/baz'),true,'Wildcard in middle should match');
25
-
97
+
26
98
// Test multiple wildcards
27
99
assertEquals(isPathAllowed('*/*','foo/bar'),true,'Multiple wildcards should match corresponding segments');
28
100
assertEquals(isPathAllowed('*/*/*','foo/bar/baz'),true,'Three wildcards should match three segments');
29
101
assertEquals(isPathAllowed('foo/*/*','foo/bar/baz'),true,'Specific prefix with wildcards should match');
30
102
assertEquals(isPathAllowed('*/*/baz','foo/bar/baz'),true,'Wildcards with specific suffix should match');
31
-
103
+
32
104
// Test wildcard consumption behavior
33
105
assertEquals(isPathAllowed('*',''),true,'Wildcard should optionally consume segments');
34
106
assertEquals(isPathAllowed('foo/*','foo'),true,'Trailing wildcard should be optional');
35
107
assertEquals(isPathAllowed('*/*','foo'),true,'Multiple wildcards can match fewer segments');
36
108
assertEquals(isPathAllowed('*/*/*','foo/bar'),true,'Extra wildcards can be skipped');
37
-
109
+
38
110
// Test complex nested paths
39
111
assertEquals(isPathAllowed('api/*/users','api/v1/users'),true,'API versioning pattern should match');
40
112
assertEquals(isPathAllowed('api/*/users/*','api/v1/users/123'),true,'API resource pattern should match');
41
113
assertEquals(isPathAllowed('api/*/users/*/profile','api/v1/users/123/profile'),true,'Nested API pattern should match');
42
-
114
+
43
115
// Test for the requested padbootstrap* pattern
44
116
assertEquals(isPathAllowed('padbootstrap*','padbootstrap'),true,'padbootstrap* should match padbootstrap');
45
117
assertEquals(isPathAllowed('padbootstrap*','padbootstrapv1'),true,'padbootstrap* should match padbootstrapv1');
46
118
assertEquals(isPathAllowed('padbootstrap*','padbootstrap/files'),false,'padbootstrap* should not match padbootstrap/files');
47
119
assertEquals(isPathAllowed('padbootstrap*/*','padbootstrap/files'),true,'padbootstrap*/* should match padbootstrap/files');
48
120
assertEquals(isPathAllowed('padbootstrap*/files','padbootstrapv1/files'),true,'padbootstrap*/files should not match padbootstrapv1/files (wildcard is segment-based, not partial)');
49
-
121
+
50
122
// Test wildcard edge cases
51
123
assertEquals(isPathAllowed('*/*/*/*/*/*','a/b'),true,'Many wildcards can match few segments');
52
124
assertEquals(isPathAllowed('a/*/b/*/c','a/anything/b/something/c'),true,'Multiple wildcards in pattern should match corresponding segments');
53
-
125
+
54
126
// Test patterns with partial segment matches
55
127
assertEquals(isPathAllowed('padbootstrap*','padbootstrap-123'),true,'Wildcards in isPathAllowed should be segment-based, not character-based');
56
128
assertEquals(isPathAllowed('test*','testuser'),true,'Asterisk as part of segment name is treated as a literal, not a wildcard');
57
129
assertEquals(isPathAllowed('my*app','myapp'),true,'Asterisk in middle of segment name is treated as a literal, not a wildcard');
58
130
131
+
assertEquals(isPathAllowed('/','/'),true,'Root path should match root path');
132
+
assertEquals(isPathAllowed('/','/test'),false,'Root path should not match non-root path');
0 commit comments