-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch_test.go
More file actions
95 lines (91 loc) · 1.91 KB
/
Copy pathmatch_test.go
File metadata and controls
95 lines (91 loc) · 1.91 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"
"github.com/stretchr/testify/assert"
)
func TestMatchPathTemplate(t *testing.T) {
tests := []struct {
name string
tpl string
path string
want bool
}{
{
name: "matches single param",
tpl: "/products/{product}/positions",
path: "/products/iphonex64s/positions",
want: true,
},
{
name: "matches with leading/trailing slashes",
tpl: "///products/{product}/positions///",
path: "/products/iphonex64s/positions/",
want: true,
},
{
name: "does not match different static segment",
tpl: "/products/{product}/positions",
path: "/items/iphonex64s/positions",
want: false,
},
{
name: "does not match different suffix segment",
tpl: "/products/{product}/positions",
path: "/products/iphonex64s/price",
want: false,
},
{
name: "does not match when segment count differs (extra)",
tpl: "/products/{product}/positions",
path: "/products/iphonex64s/positions/extra",
want: false,
},
{
name: "does not match when segment count differs (missing)",
tpl: "/products/{product}/positions",
path: "/products/iphonex64s",
want: false,
},
{
name: "param must match non-empty segment",
tpl: "/products/{product}/positions",
path: "/products//positions",
want: false,
},
{
name: "no params exact match",
tpl: "/a/b/c",
path: "/a/b/c",
want: true,
},
{
name: "no params exact mismatch",
tpl: "/a/b/c",
path: "/a/b/d",
want: false,
},
{
name: "root matches root",
tpl: "/",
path: "/",
want: true,
},
{
name: "root does not match non-root",
tpl: "/",
path: "/a",
want: false,
},
{
name: "treat braces-only segments as params",
tpl: "/x/{id}/y/{slug}",
path: "/x/123/y/abc",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, MatchPathTemplate(tt.tpl, tt.path))
})
}
}