-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
150 lines (133 loc) · 4.1 KB
/
router_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
package eudore_test
import (
"context"
"fmt"
"net/http"
"testing"
. "github.com/eudore/eudore"
)
type Test014Controller struct {
ControllerAutoRoute
}
func (ctl *Test014Controller) Get(ctx Context) interface{} {
return "Test014Controller"
}
func TestRouterStdAdd(t *testing.T) {
type String struct {
Data string
}
ctx := context.WithValue(context.Background(),
ContextKeyHandlerExtender, DefaultHandlerExtender,
)
ctx = context.WithValue(ctx,
ContextKeyLogger, DefaultLoggerNull,
)
r := NewRouter(nil)
r.(interface{ Mount(context.Context) }).Mount(ctx)
r.AddHandlerExtend(func(str String) HandlerFunc {
return func(ctx Context) {
ctx.WriteString(str.Data)
}
})
r.Group(" loggerkind=all")
r.Group(" loggerkind=~all")
r.Group(" loggerkind=middleware").AddController(&Test014Controller{})
api := r.Group("/api")
api.AddHandler("TEST", "/*", String{"test"})
api.AddHandler("LOCK", "/*", String{"lock"})
api.AddHandler("UNLOCK", "/*", String{"unlock"})
api.AddHandler("MOVE", "/*", String{"lock"})
api.AnyFunc("/*", String{"any /*"})
api.GetFunc("/", String{"get"})
api.PostFunc("/", String{"post"})
api.PutFunc("/", String{"put"})
api.DeleteFunc("/", String{"delete"})
api.HeadFunc("/", String{"head"})
api.PatchFunc("/", String{"patch"})
api.AnyFunc("/allow", String{"405"})
r.Match(MethodOptions, "/api/allow", &Params{"route", ""})
}
func TestRouterError(t *testing.T) {
var h HandlerFunc
r := NewRouter(nil)
r.Group(" loggerkind=~handler|metadata").GetFunc("/", HandlerEmpty)
r.AddHandlerExtend("/api", TestRouterError)
r.AddController(&Test015Controller{})
r.AddController(NewControllerError(&Test015Controller{}, fmt.Errorf("test controller error")))
r.Group("/api").AddController(NewControllerError(&Test015Controller{}, fmt.Errorf("test controller error")))
r.GetFunc("{/*}", HandlerEmpty)
r.GetFunc("/*path|check", HandlerEmpty)
r.GetFunc("/err", func(*testing.T) {})
r.GetFunc("/nil", h)
}
type Test015Controller struct {
ControllerAutoRoute
}
func (Test015Controller) String() string {
return "015"
}
func TestRouterMiddleware(t *testing.T) {
r := NewRouter(nil)
r.AddMiddleware()
r.AddMiddleware("/api", func(int) {})
r.AddMiddleware(func(int) {})
r.AddHandlerExtend()
r.AddMiddleware("/api/v2", HandlerEmpty)
r.AddMiddleware("/api/v1", HandlerEmpty)
r.AddMiddleware("/api", HandlerEmpty)
r.AnyFunc("/api/v1", HandlerEmpty)
apiv1 := r.Group("/api/v1")
apiv1.AddMiddleware(func(int) {})
apiv1.AnyFunc("/users", HandlerEmpty)
}
func TestRouterCoreHost(t *testing.T) {
echoHandleHost := func(ctx Context) {
ctx.WriteString(ctx.GetParam("route-host"))
}
s := NewServer(nil)
c := NewClient()
r := NewRouter(NewRouterCoreHost(nil))
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()
}))
r.AnyFunc("/* route-host=*.eudore.cn", echoHandleHost)
r.AnyFunc("/* route-host=www.example.*", echoHandleHost)
r.AnyFunc("/* route-host=eudore.cn", echoHandleHost)
r.AnyFunc("/* route-host=eudore.*", echoHandleHost)
r.AnyFunc("/* route-host=eudore.*:8080", echoHandleHost)
r.AddHandler("404", "", HandlerRouter404)
r.(interface{ Metadata() interface{} }).Metadata()
routes := []struct {
path string
host string
status int
body string
}{
{"/", "www.eudore.net", 404, "404"},
{"/", "eudore.cn", 200, "eudore.cn"},
{"/", "eudore.cn2", 200, "eudore.*"},
{"/", "eudore.com", 200, "eudore.*"},
{"/", "eudore.com:80", 200, "eudore.*"},
{"/", "godoc.eudore.cn", 200, "*.eudore.cn"},
{"/", "www.example.cn", 200, "www.example.*"},
}
ctx := context.WithValue(context.Background(),
ContextKeyServer, s,
)
for _, route := range routes {
err := c.NewRequest("GET", route.path, ctx,
NewClientOptionHost(route.host),
NewClientCheckStatus(route.status),
NewClientCheckBody(route.body),
)
if err != nil {
t.Error(err)
}
}
r.(interface{ Mount(context.Context) }).Mount(context.Background())
r.(interface{ Unmount(context.Context) }).Unmount(context.Background())
}