-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouterstd_test.go
237 lines (222 loc) · 5.94 KB
/
routerstd_test.go
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package eudore_test
import (
"context"
"fmt"
"net/http"
"testing"
. "github.com/eudore/eudore"
)
func TestRouterStdAny(t *testing.T) {
// 扩展RouterStd允许的方法
DefaultRouterAllMethod = append(DefaultRouterAllMethod, "LOCK", "UNLOCK")
DefaultRouterAnyMethod = append(DefaultRouterAnyMethod, "LOCK", "UNLOCK")
defer func() {
DefaultRouterAllMethod = DefaultRouterAllMethod[:len(DefaultRouterAllMethod)-2]
DefaultRouterAnyMethod = DefaultRouterAnyMethod[:len(DefaultRouterAnyMethod)-2]
}()
r, c := newCSR(nil)
// Any方法覆盖
r.GetFunc("/get/:val")
r.GetFunc("/get/:val", func(ctx Context) {
ctx.WriteString("method is get1")
})
r.AnyFunc("/get/:val", func(ctx Context) {
ctx.WriteString("method is any")
})
r.GetFunc("/get/:val", func(ctx Context) {
ctx.WriteString("method is get2")
})
r.PostFunc("/get/:val", func(ctx Context) {
ctx.WriteString("method is post")
})
r.AddHandler("LOCK", "/get/:val", func(ctx Context) {
ctx.WriteString("method is lock")
})
r.GetFunc("/index", HandlerEmpty)
r.AddHandler("404,444", "", HandlerRouter404)
r.AddHandler("405", "", HandlerRouter405)
routes := []struct {
method string
path string
body string
}{
{"GET", "/get/1", "method is get"},
{"POST", "/get/2", "method is post"},
{"PUT", "/get/3", "method is any"},
{"LOCK", "/get/4", "method is lock"},
{"COPY", "/get/5", "405"},
{"GET", "/get", "404"},
{"POST", "/get", "404"},
{"PUT", "/get", "404"},
{"POST", "/index", "405"},
{"GET", "/index", ""},
}
for _, route := range routes {
err := c.NewRequest(route.method, route.path,
NewClientCheckBody(route.body),
)
if err != nil {
t.Error(route.method, route.path, err)
}
}
}
func TestRouterStdCheck(t *testing.T) {
r, c := newCSR(NewRouterCoreMux())
r.AnyFunc("/1/:num|num version=1", HandlerEmpty)
r.AnyFunc("/1/222", HandlerEmpty)
r.AnyFunc("/2/:num|num", HandlerEmpty)
r.AnyFunc("/2/:num|", HandlerEmpty)
r.AnyFunc("/2/:", HandlerEmpty)
r.AnyFunc("/3/:num|num/22", HandlerEmpty)
r.AnyFunc("/3/:num|num/*", HandlerEmpty)
r.AnyFunc("/4/*num|num", HandlerEmpty)
r.AnyFunc("/4/*num|num", HandlerEmpty)
r.AnyFunc("/4/*", HandlerEmpty)
r.AnyFunc("/5/*num|num", HandlerEmpty)
r.AnyFunc("/api/v1/2", HandlerEmpty)
r.AnyFunc("/api/v1/1", HandlerEmpty)
r.AnyFunc("/*num|^\\d+$", HandlerEmpty)
r.AnyFunc("/api/v1/*|{^0/api\\S+$}", HandlerEmpty)
r.AnyFunc("/api/v1/*|{\\s+{}}", HandlerEmpty)
r.AnyFunc("{/api/v1/*\\}}", HandlerEmpty)
r.AnyFunc("/api/v1/{{*}}", HandlerEmpty)
r.AnyFunc("/api/*", HandlerEmpty)
r.AnyFunc("/api/*", HandlerEmpty)
r.AddHandler(MethodOptions, "/", HandlerEmpty)
r.AddHandler(MethodConnect, "/", HandlerEmpty)
r.AddHandler(MethodTrace, "/", HandlerEmpty)
// 请求测试
c.NewRequest("GET", "/1/1")
c.NewRequest("POST", "/1/222")
c.NewRequest("PUT", "/2/3")
c.NewRequest("PUT", "/3/11/3")
c.NewRequest("PUT", "/3/11/22")
c.NewRequest("PUT", "/4/22")
c.NewRequest("PUT", "/5/22")
c.NewRequest("PUT", "/:{num}")
}
func newCSR(core RouterCore) (Router, Client) {
s := NewServer(nil)
c := NewClient()
r := NewRouter(core)
get := NewContextBaseFunc(context.Background())
s.SetHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := get()
ctx.Reset(w, req)
ctx.SetHandlers(-1, r.Match(ctx.Method(), ctx.Path(), ctx.Params()))
ctx.Next()
}))
ctx := context.WithValue(context.Background(),
ContextKeyServer, s,
)
ctx = context.WithValue(ctx,
ContextKeyFuncCreator, DefaultFuncCreator,
)
r.(interface{ Mount(context.Context) }).Mount(ctx)
c.(interface{ Mount(context.Context) }).Mount(ctx)
return r, c
}
func TestRouterStdSplit(t *testing.T) {
paths := []struct {
path string
sub string
}{
{"/ a=1", "/"},
{"/{} a=1", "/{}"},
{"/{{} a=1", "/{{}"},
{"/{}} a=1", "/{}}"},
{"/{ } a=1", "/{ }"},
{"/{ } \\d", "/{ }"},
}
for i := range paths {
if getRoutePathT(paths[i].path) != paths[i].sub {
t.Error(i, data[i], getRoutePathT(paths[i].path))
}
}
datas := []struct {
path string
strs []string
}{
{"/", []string{"/"}},
{"/api/note/", []string{"/api/note/"}},
{"//api/*", []string{"//api/", "*"}},
{"//api/*name", []string{"//api/", "*name"}},
{"/api/get/", []string{"/api/get/"}},
{"/api/get", []string{"/api/get"}},
{"/api/:get", []string{"/api/", ":get"}},
{"/api/:get/*", []string{"/api/", ":get", "/", "*"}},
{"{/api/**{}:get/*}", []string{"/api/**{", ":get", "/", "*}"}},
{"/api/:name/info/*", []string{"/api/", ":name", "/info/", "*"}},
{"/api/:name|^\\d+$/info", []string{"/api/", ":name|^\\d+$", "/info"}},
{"/api/*|{^0/api\\S+$}", []string{"/api/", "*|^0/api\\S+$"}},
{"/api/*|^\\$\\d+$", []string{"/api/", "*|^\\$\\d+$"}},
}
for i := range datas {
if fmt.Sprint(getSplitPathT(datas[i].path)) != fmt.Sprint(datas[i].strs) {
t.Error(i, data[i])
}
}
}
func getRoutePathT(path string) string {
var isblock bool
var last rune
for i, b := range path {
if isblock {
if b == '}' && last != '\\' {
isblock = false
}
last = b
continue
}
switch b {
case '{':
isblock = true
case ' ':
return path[:i]
}
}
return path
}
func getSplitPathT(path string) []string {
var strs []string
bytes := make([]byte, 0, 64)
var isblock, isconst bool
for _, b := range path {
// block pattern
if isblock {
if b == '}' {
if len(bytes) != 0 && bytes[len(bytes)-1] != '\\' {
isblock = false
continue
}
// escaping }
bytes = bytes[:len(bytes)-1]
}
bytes = append(bytes, string(b)...)
continue
}
switch b {
case '/':
// constant mode, creates a new string in non-constant mode
if !isconst {
isconst = true
strs = append(strs, string(bytes))
bytes = bytes[:0]
}
case ':', '*':
// variable pattern or wildcard pattern
isconst = false
strs = append(strs, string(bytes))
bytes = bytes[:0]
case '{':
isblock = true
continue
}
bytes = append(bytes, string(b)...)
}
strs = append(strs, string(bytes))
if strs[0] == "" {
strs = strs[1:]
}
return strs
}