Repository navigation
Expand file tree
/
Copy pathfilter_test.go
More file actions
95 lines (83 loc) · 2.5 KB
/
Copy pathfilter_test.go
File metadata and controls
95 lines (83 loc) · 2.5 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
package main
import (
"testing"
)
func TestFilterPaths(t *testing.T) {
tests := []struct {
path string
patterns []string
want bool
}{
// Exact match
{"/api/users", []string{"/api/users"}, true},
{"/api/users", []string{"/api/posts"}, false},
// Wildcard match
{"/api/v1/users", []string{"/api/v1/*"}, true},
{"/api/v1/posts", []string{"/api/v1/*"}, true},
{"/api/v2/users", []string{"/api/v1/*"}, false},
// Multiple patterns
{"/api/users", []string{"/api/posts", "/api/users"}, true},
{"/users/123", []string{"/users/?*"}, true},
// Exclude pattern
{"/api/users", []string{"/api/*", "!/api/users"}, false},
{"/api/users", []string{"/api/*", "!/api/users/*"}, true},
{"/api/posts", []string{"/api/*", "!/api/users"}, true},
{"/api/v2/projects/{projectName}/credentials/{ID}", []string{"/api/*", "!/api/users/*"}, false},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
spec := &OpenAPISpec{Paths: map[string]interface{}{tt.path: struct{}{}}}
include, exclude := FilterPatterns(tt.patterns)
kept, _ := FilterPaths(spec, include, exclude)
_, got := kept[tt.path]
if got != tt.want {
t.Errorf("FilterPaths(%q, %v) kept=%v, want %v",
tt.path, tt.patterns, got, tt.want)
}
})
}
}
func TestMatchesAnyPattern(t *testing.T) {
tests := []struct {
path string
patterns []string
want bool
}{
// Exact match
{"/api/users", []string{"/api/users"}, true},
{"/api/users", []string{"/api/posts"}, false},
// Wildcard match
{"/api/v1/users", []string{"/api/v1/*"}, true},
{"/api/v1/posts", []string{"/api/v1/*"}, true},
{"/api/v2/users", []string{"/api/v1/*"}, false},
// Multiple patterns
{"/api/users", []string{"/api/posts", "/api/users"}, true},
{"/users/123", []string{"/users/?*"}, true},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
if got := matchesAnyPattern(tt.path, tt.patterns); got != tt.want {
t.Errorf("matchesAnyPattern(%q, %v) = %v, want %v",
tt.path, tt.patterns, got, tt.want)
}
})
}
}
func TestExtractComponentName(t *testing.T) {
tests := []struct {
ref string
want string
}{
{"#/components/schemas/User", "User"},
{"#/components/schemas/ErrorResponse", "ErrorResponse"},
{"#/components/schemas/Model_v1", "Model_v1"},
{"invalid", ""},
}
for _, tt := range tests {
t.Run(tt.ref, func(t *testing.T) {
if got := extractComponentName(tt.ref); got != tt.want {
t.Errorf("extractComponentName(%q) = %q, want %q", tt.ref, got, tt.want)
}
})
}
}