-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathrouter_test.go
203 lines (182 loc) · 4.9 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
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
package easytcp
import (
"github.com/stretchr/testify/assert"
"reflect"
"runtime"
"testing"
)
func TestRouter_register(t *testing.T) {
rt := newRouter()
var id = 1
rt.register(id, nil)
_, ok := rt.handlerMapper[id]
assert.False(t, ok)
_, ok = rt.middlewaresMapper[id]
assert.False(t, ok)
h := nilHandler
m1 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
m2 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
rt.register(id, h, m1, nil, m2)
v, ok := rt.handlerMapper[id]
assert.True(t, ok)
expect := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
actual := runtime.FuncForPC(reflect.ValueOf(v).Pointer()).Name()
assert.Equal(t, expect, actual)
mhs, ok := rt.middlewaresMapper[id]
assert.True(t, ok)
expects := []MiddlewareFunc{m1, m2}
for i, mh := range mhs {
expect := runtime.FuncForPC(reflect.ValueOf(expects[i]).Pointer()).Name()
actual := runtime.FuncForPC(reflect.ValueOf(mh).Pointer()).Name()
assert.Equal(t, expect, actual)
}
}
func TestRouter_registerMiddleware(t *testing.T) {
rt := newRouter()
rt.registerMiddleware()
assert.Len(t, rt.globalMiddlewares, 0)
rt.registerMiddleware(nil, nil)
assert.Len(t, rt.globalMiddlewares, 0)
m1 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
m2 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
m3 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
rt.registerMiddleware(m1, m2)
assert.Len(t, rt.globalMiddlewares, 2)
rt.registerMiddleware(m3)
assert.Len(t, rt.globalMiddlewares, 3)
expects := []MiddlewareFunc{m1, m2, m3}
for i, m := range rt.globalMiddlewares {
expect := runtime.FuncForPC(reflect.ValueOf(expects[i]).Pointer()).Name()
actual := runtime.FuncForPC(reflect.ValueOf(m).Pointer()).Name()
assert.Equal(t, expect, actual)
}
}
func TestRouter_handleReq(t *testing.T) {
t.Run("when request message is nil", func(t *testing.T) {
rt := newRouter()
ctx := &routeContext{}
rt.handleRequest(ctx)
})
t.Run("when handler and middlewares not found", func(t *testing.T) {
rt := newRouter()
reqMsg := NewMessage(1, []byte("test"))
ctx := &routeContext{reqMsg: reqMsg}
rt.handleRequest(ctx)
assert.Nil(t, ctx.respMsg)
})
t.Run("when handler and middlewares found", func(t *testing.T) {
rt := newRouter()
var id = 1
rt.register(id, nilHandler, func(next HandlerFunc) HandlerFunc {
return func(ctx Context) { next(ctx) }
})
reqMsg := NewMessage(1, []byte("test"))
ctx := &routeContext{reqMsg: reqMsg}
rt.handleRequest(ctx)
assert.Nil(t, ctx.respMsg)
})
}
func TestRouter_wrapHandlers(t *testing.T) {
rt := newRouter()
t.Run("it works when there's no handler nor middleware", func(t *testing.T) {
wrap := rt.wrapHandlers(nil, nil)
ctx := &routeContext{}
wrap(ctx)
assert.Nil(t, ctx.respMsg)
})
t.Run("it should invoke handlers in the right order", func(t *testing.T) {
result := make([]string, 0)
middles := []MiddlewareFunc{
func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
result = append(result, "m1-before")
next(ctx)
}
},
func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
result = append(result, "m2-before")
next(ctx)
result = append(result, "m2-after")
}
},
func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
result = append(result, "m3-after")
}
},
}
var handler HandlerFunc = func(ctx Context) {
result = append(result, "done")
ctx.SetResponseMessage(NewMessage(2, []byte("done")))
}
wrap := rt.wrapHandlers(handler, middles)
ctx := &routeContext{}
wrap(ctx)
assert.EqualValues(t, ctx.respMsg.Data(), "done")
assert.Equal(t, result, []string{"m1-before", "m2-before", "done", "m3-after", "m2-after"})
})
}
func TestRouter_printHandlers(t *testing.T) {
t.Run("when there's no route registered", func(t *testing.T) {
rt := newRouter()
rt.printHandlers("localhost")
})
t.Run("when there are routes registered", func(t *testing.T) {
rt := newRouter()
m1 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
m2 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
m3 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
m4 := func(next HandlerFunc) HandlerFunc {
return func(ctx Context) {
next(ctx)
}
}
rt.registerMiddleware(m1)
rt.registerMiddleware(m2)
rt.register(1234, nilHandler, m3, m4)
rt.register(12345678, nilHandler)
rt.register(12345, nilHandler)
rt.register(123456, nilHandler)
rt.printHandlers("localhost")
})
}
func TestRouter_setNotFoundHandler(t *testing.T) {
rt := newRouter()
assert.Nil(t, rt.notFoundHandler)
rt.setNotFoundHandler(func(ctx Context) {})
assert.NotNil(t, rt.notFoundHandler)
}