-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwildcard_test.go
More file actions
113 lines (103 loc) · 2.86 KB
/
wildcard_test.go
File metadata and controls
113 lines (103 loc) · 2.86 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
package httpx
import "testing"
type mockRouterFeatureProvider struct {
namedWildcard bool
}
func (m mockRouterFeatureProvider) SupportsRouterFeature(feature RouterFeature) bool {
return feature == RouterFeatureNamedWildcard && m.namedWildcard
}
func TestFixWildcardPathIfNeed(t *testing.T) {
tests := []struct {
name string
path string
supports bool
wantPath string
wantParam string
}{
{
name: "no wildcard path keeps original",
path: "/users/:id",
supports: false,
wantPath: "/users/:id",
wantParam: "",
},
{
name: "named wildcard with support keeps original and returns name",
path: "/files/*filepath",
supports: true,
wantPath: "/files/*filepath",
wantParam: "filepath",
},
{
name: "named wildcard without support converts to anonymous",
path: "/files/*filepath",
supports: false,
wantPath: "/files/*",
wantParam: "*",
},
{
name: "anonymous wildcard remains anonymous",
path: "/files/*",
supports: false,
wantPath: "/files/*",
wantParam: "*",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPath, gotParam := FixWildcardPathIfNeed(
mockRouterFeatureProvider{namedWildcard: tt.supports},
tt.path,
)
if gotPath != tt.wantPath {
t.Fatalf("path mismatch: got %q, want %q", gotPath, tt.wantPath)
}
if gotParam != tt.wantParam {
t.Fatalf("param mismatch: got %q, want %q", gotParam, tt.wantParam)
}
})
}
}
func TestToAnonymousWildcardPath(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{name: "empty path", in: "", want: ""},
{name: "no wildcard", in: "/health", want: "/health"},
{name: "named wildcard", in: "/files/*filepath", want: "/files/*"},
{name: "anonymous wildcard", in: "/files/*", want: "/files/*"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := toAnonymousWildcardPath(tt.in)
if got != tt.want {
t.Fatalf("toAnonymousWildcardPath(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}
func TestWildcardParamName(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{name: "no wildcard", in: "/users/:id", want: ""},
{name: "named wildcard", in: "/files/*filepath", want: "filepath"},
{name: "anonymous wildcard", in: "/files/*", want: "*"},
// Current implementation only extracts the first wildcard when multiple are present.
// This does not mean multiple wildcard params are supported by routers.
{name: "multiple wildcards uses first one", in: "/a/*x/b/*y", want: "x"},
{name: "wildcard in middle segment", in: "/a/*name/detail", want: "name"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wildcardParamName(tt.in)
if got != tt.want {
t.Fatalf("wildcardParamName(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}