-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathroute_test.go
More file actions
197 lines (185 loc) · 4.26 KB
/
Copy pathroute_test.go
File metadata and controls
197 lines (185 loc) · 4.26 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
package xmux
import (
"log"
"regexp"
"strings"
"testing"
)
type routerTest struct {
title string
method string
pattern string
url string
keys []string
values []string
isSlash bool
shouldMatch bool
}
// 测试路径是否能匹配
func TestPattern(t *testing.T) {
tests := []routerTest{
{
title: "完全匹配",
pattern: "/aaaa/bbbb",
url: "/aaaa/bbbb",
keys: nil,
values: nil,
isSlash: false,
method: "GET",
shouldMatch: true,
},
{
title: "字符串正则匹配",
pattern: "/aaaa/{bbbb}",
url: "/aaaa/bbbb",
method: "GET",
isSlash: false,
keys: []string{"bbbb"},
values: []string{"bbbb"},
shouldMatch: true,
},
{
title: "数字也是字符串",
pattern: "/aaaa/{bbbb}",
url: "/aaaa/12334",
method: "POST",
isSlash: false,
keys: []string{"bbbb"},
values: []string{"12334"},
shouldMatch: true,
},
{
title: "带参数的字符串匹配",
pattern: "/aaaa/{string:bbbb}",
url: "/aaaa/hioj",
method: "GET",
isSlash: false,
keys: []string{"bbbb"},
values: []string{"hioj"},
shouldMatch: true,
},
{
title: "int类型正则匹配",
pattern: "/aaaa/{int:bbbb}",
method: "GET",
isSlash: false,
url: "/aaaa/joijoa324",
shouldMatch: false,
},
{
title: "int类型正则匹配",
pattern: "/aaaa/{int:bbbb}",
url: "/aaaa/334",
keys: []string{"bbbb"},
method: "GET",
isSlash: false,
values: []string{"65555555555555"},
shouldMatch: true,
},
{
title: "多路径截断",
pattern: "/aaaa////{int:bbbb}",
url: "/aaaa/334",
method: "POST",
isSlash: false,
keys: []string{"bbbb"},
values: []string{"334"},
shouldMatch: true,
},
{
title: "多路径截断",
pattern: "/aaaa///{string:bbbb}///",
url: "/aaaa/sdf/",
method: "POST",
isSlash: true,
keys: []string{"bbbb"},
values: []string{"sdf"},
shouldMatch: true,
},
{
title: "多路径截断",
pattern: "/aaaa///{string:bbbb}///",
url: "/aaaa/sdf/",
method: "POST",
isSlash: true,
keys: []string{"bbbb"},
values: []string{"sdf"},
shouldMatch: true,
},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
testPattern(t, test)
})
}
}
func testPattern(t *testing.T, test routerTest) {
t.Log("before: ", test.pattern)
test.pattern = PrettySlash(test.pattern)
t.Log("after: ", test.pattern)
// r.makeRoute(test.pattern)
patternMatched := false
url, _, ok := makeRoute(test.pattern)
if !ok {
patternMatched = url == test.url
} else {
re := regexp.MustCompile(url)
patternMatched = re.MatchString(test.url)
}
if patternMatched != test.shouldMatch {
t.Errorf("not matched")
}
}
// 正则的url 换成 正常的url
func testFormat(path string, newpath string) string {
start := strings.Index(path, "{")
end := strings.Index(path, "}")
//正则匹配的
re := strings.Trim(path[start+1:end], " ")
if re == "" {
log.Fatal("invalid uri " + path)
} else {
prefix := path[:start]
//判断:
ts := strings.Split(re, ":")
if len(ts) == 3 {
//正则 匹配
// /asdf/{re:([a-z]{1,3})([0-9]{1,2}):ch,num}
if ts[0] == "re" {
// 检测参数是否匹配, 同时禁止匹配()
pfc := strings.Count(ts[1], "(")
sfc := strings.Count(ts[1], ")")
if pfc != sfc {
log.Fatal("can not include ( or ) ," + path)
}
//查看后面参数是否匹配
vl := strings.Split(ts[2], ",")
if len(vl) != sfc {
log.Fatal("variable not matched , " + path)
}
prefix += ts[1]
} else {
log.Fatal("invalid uri ," + path)
}
} else if len(ts) == 2 {
if ts[0] == "int" {
prefix += "(\\d+)"
} else {
prefix += "(\\w+)"
}
} else if len(ts) == 1 {
prefix += "(\\w+)"
} else {
log.Fatal("invalid uri ," + path)
}
newpath += prefix
if end+1 == len(path) {
// last url
newpath += "$"
return newpath
} else {
return testFormat(path[end+1:], newpath)
}
}
return ""
}