-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_match_test.go
More file actions
211 lines (188 loc) · 5.64 KB
/
path_match_test.go
File metadata and controls
211 lines (188 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package farp
import "testing"
func TestMatchPath(t *testing.T) {
tests := []struct {
pattern string
path string
want bool
}{
// Exact matches
{"/health", "/health", true},
{"/health", "/healthz", false},
{"/api/v1/users", "/api/v1/users", true},
{"/api/v1/users", "/api/v2/users", false},
// Single-segment wildcard (*)
{"/api/*/users", "/api/v1/users", true},
{"/api/*/users", "/api/v2/users", true},
{"/api/*/users", "/api/v1/posts", false},
{"/api/*/users", "/api/users", false}, // * requires exactly one segment
{"/api/*/users", "/api/v1/v2/users", false}, // * matches only one segment
{"/api/users/*", "/api/users/123", true},
{"/api/users/*", "/api/users/123/edit", false},
// Multi-segment wildcard (**)
{"/api/**", "/api", true}, // ** matches zero segments
{"/api/**", "/api/v1", true},
{"/api/**", "/api/v1/users", true},
{"/api/**", "/api/v1/users/123", true},
{"/internal/**", "/internal", true},
{"/internal/**", "/internal/foo/bar/baz", true},
{"/**", "/anything/at/all", true},
{"/**", "/", true},
// ** in the middle
{"/api/**/status", "/api/status", true},
{"/api/**/status", "/api/v1/status", true},
{"/api/**/status", "/api/v1/users/status", true},
{"/api/**/status", "/api/v1/users/health", false},
// Mixed wildcards
{"/api/*/users/**", "/api/v1/users", true},
{"/api/*/users/**", "/api/v1/users/123", true},
{"/api/*/users/**", "/api/v1/users/123/edit", true},
{"/api/*/users/**", "/api/users/123", false}, // * requires one segment
// Root path
{"/", "/", true},
{"/", "/api", false},
// Pattern with trailing slash normalization
{"/_farp/**", "/_farp/manifest", true},
{"/_farp/**", "/_farp/schemas/openapi", true},
{"/_/**", "/_/health", true},
{"/_/**", "/_/info", true},
// OpenAPI/AsyncAPI prefix patterns
{"/openapi*", "/openapi.json", true},
{"/openapi*", "/openapi.yaml", true},
{"/asyncapi*", "/asyncapi.json", true},
{"/docs/**", "/docs", true},
{"/docs/**", "/docs/index.html", true},
{"/docs", "/docs", true},
{"/docs", "/docs/index.html", false},
}
for _, tt := range tests {
t.Run(tt.pattern+"→"+tt.path, func(t *testing.T) {
got := MatchPath(tt.pattern, tt.path)
if got != tt.want {
t.Errorf("MatchPath(%q, %q) = %v, want %v", tt.pattern, tt.path, got, tt.want)
}
})
}
}
func TestShouldIncludePath(t *testing.T) {
tests := []struct {
name string
path string
rules []PathRule
want bool
}{
{
name: "no rules - include by default",
path: "/api/users",
rules: nil,
want: true,
},
{
name: "exclude rule matches",
path: "/internal/debug",
rules: []PathRule{
{Pattern: "/internal/**", Action: PathRuleExclude},
},
want: false,
},
{
name: "include rule matches",
path: "/api/v1/users",
rules: []PathRule{
{Pattern: "/api/**", Action: PathRuleInclude},
},
want: true,
},
{
name: "first match wins - exclude before include",
path: "/api/internal/debug",
rules: []PathRule{
{Pattern: "/api/internal/**", Action: PathRuleExclude},
{Pattern: "/api/**", Action: PathRuleInclude},
},
want: false,
},
{
name: "first match wins - include before exclude",
path: "/api/v1/users",
rules: []PathRule{
{Pattern: "/api/v1/**", Action: PathRuleInclude},
{Pattern: "/api/**", Action: PathRuleExclude},
},
want: true,
},
{
name: "no match - default include",
path: "/other/path",
rules: []PathRule{
{Pattern: "/api/**", Action: PathRuleExclude},
},
want: true,
},
{
name: "exact exclude",
path: "/health",
rules: []PathRule{
{Pattern: "/health", Action: PathRuleExclude},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ShouldIncludePath(tt.path, tt.rules)
if got != tt.want {
t.Errorf("ShouldIncludePath(%q) = %v, want %v", tt.path, got, tt.want)
}
})
}
}
func TestBuildPathRules(t *testing.T) {
userRules := []PathRule{
{Pattern: "/api/users/*", Action: PathRuleExclude},
}
// With internal exclusion enabled
rules := BuildPathRules(true, userRules)
if len(rules) <= len(userRules) {
t.Error("expected internal rules to be prepended")
}
// Internal paths should be excluded
if ShouldIncludePath("/_farp/manifest", rules) {
t.Error("/_farp/manifest should be excluded with internal rules")
}
if ShouldIncludePath("/_/health", rules) {
t.Error("/_/health should be excluded with internal rules")
}
// User rule should also work
if ShouldIncludePath("/api/users/123", rules) {
t.Error("/api/users/123 should be excluded by user rule")
}
// Non-matching paths should be included
if !ShouldIncludePath("/api/v1/projects", rules) {
t.Error("/api/v1/projects should be included")
}
// Without internal exclusion
rulesNoInternal := BuildPathRules(false, userRules)
if len(rulesNoInternal) != len(userRules) {
t.Error("expected only user rules when internal exclusion disabled")
}
// Internal paths should be included when disabled
if !ShouldIncludePath("/_farp/manifest", rulesNoInternal) {
t.Error("/_farp/manifest should be included when internal exclusion disabled")
}
}
func TestInternalPathRules(t *testing.T) {
rules := InternalPathRules()
excluded := []string{"/", "/_farp/manifest", "/_farp/schemas/openapi", "/_/health", "/_/info", "/docs", "/docs/index.html"}
for _, path := range excluded {
if ShouldIncludePath(path, rules) {
t.Errorf("%q should be excluded by internal rules", path)
}
}
included := []string{"/api/v1/users", "/api/v1/projects", "/graphql"}
for _, path := range included {
if !ShouldIncludePath(path, rules) {
t.Errorf("%q should be included (not internal)", path)
}
}
}