-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindroutes_test.go
155 lines (125 loc) · 3.3 KB
/
bindroutes_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
package bindroutes
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
type (
testRouter map[string]bool
testHandler struct {
BasePath `handle:"/users"`
Post http.HandlerFunc `handle:"POST /"`
Get http.HandlerFunc `handle:"GET /{id}"`
Put http.HandlerFunc `handle:"PUT /{id}"`
Delete http.HandlerFunc `handle:"DELETE /{id}"`
Ignored string
}
testGroupHandler struct {
BasePath `handle:"/users"`
Post http.HandlerFunc `handle:"POST /" using-router:"router_a"`
Get http.HandlerFunc `handle:"GET /{id}" using-router:"router_b"`
Put http.HandlerFunc `handle:"PUT /{id}" using-router:"router_a"`
Delete http.HandlerFunc `handle:"DELETE /{id}" using-router:"router_c"`
}
failingHandler struct {
Post http.HandlerFunc `handle:"Get,/users"`
}
)
func TestSplitTag(t *testing.T) {
assert.Panics(
t,
func() {
splitHandleTag("GET")
},
"should panic with incomplete string",
)
assert.Panics(
t,
func() {
splitHandleTag("NINJA /")
},
"should panic with wrong method",
)
method, pattern := splitHandleTag("POST /")
assert.Equal(t, "POST", method)
assert.Equal(t, "/", pattern)
}
func TestUsingRouter(t *testing.T) {
h := testHandler{
Post: dummyHandler,
Get: dummyHandler,
Put: dummyHandler,
Delete: dummyHandler,
}
r := make(testRouter)
UsingRouter(r, &h)
assert.True(t, r["POST /users"])
assert.True(t, r["GET /users/{id}"])
assert.True(t, r["PUT /users/{id}"])
assert.True(t, r["DELETE /users/{id}"])
}
func TestUsingRouters(t *testing.T) {
h := testGroupHandler{
Post: dummyHandler,
Get: dummyHandler,
Put: dummyHandler,
Delete: dummyHandler,
}
rs := map[string]Router{
"router_a": make(testRouter),
"router_b": make(testRouter),
"router_c": make(testRouter),
}
UsingRouters(rs, &h)
ra := rs["router_a"].(testRouter)
rb := rs["router_b"].(testRouter)
rc := rs["router_c"].(testRouter)
assert.True(t, ra["POST /users"])
assert.True(t, rb["GET /users/{id}"])
assert.True(t, ra["PUT /users/{id}"])
assert.True(t, rc["DELETE /users/{id}"])
}
func TestFailRegister(t *testing.T) {
h := failingHandler{Post: dummyHandler}
r := make(testRouter)
assert.Panics(t, func() {
UsingRouter(r, h)
})
}
func TestGroupHandlerFuncs(t *testing.T) {
h := testGroupHandler{
Post: dummyHandler,
Get: dummyHandler,
Put: dummyHandler,
Delete: dummyHandler,
}
hg := groupHandlerFuncs([]any{&h})
assert.Equal(t, 2, len(hg["router_a"]))
assert.Equal(t, 1, len(hg["router_b"]))
assert.Equal(t, 1, len(hg["router_c"]))
assert.Equal(t, 0, len(hg["router_d"]))
}
func (r testRouter) Delete(pattern string, h http.HandlerFunc) {
r["DELETE "+pattern] = true
}
func (r testRouter) Get(pattern string, h http.HandlerFunc) {
r["GET "+pattern] = true
}
func (r testRouter) Head(pattern string, h http.HandlerFunc) {
r["HEAD "+pattern] = true
}
func (r testRouter) Options(pattern string, h http.HandlerFunc) {
r["OPTIONS "+pattern] = true
}
func (r testRouter) Patch(pattern string, h http.HandlerFunc) {
r["PATCH "+pattern] = true
}
func (r testRouter) Post(pattern string, h http.HandlerFunc) {
r["POST "+pattern] = true
}
func (r testRouter) Put(pattern string, h http.HandlerFunc) {
r["PUT "+pattern] = true
}
func dummyHandler(w http.ResponseWriter, r *http.Request) {
// nothing ...
}