-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouter_test.go
236 lines (196 loc) · 6.66 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
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
package nano
import (
"log"
"net/http"
"net/http/httptest"
"testing"
)
func TestCreateURLParts(t *testing.T) {
tt := []struct {
name string
url string
slices []string
}{
{"root url", "/", []string{}},
{"one url part", "/home", []string{"home"}},
{"one url part without backslash prefix", "home", []string{"home"}},
{"one url part with backslash suffix", "home/", []string{"home"}},
{"two url parts", "/home/services", []string{"home", "services"}},
{"two url parts without backslash prefix", "home/services", []string{"home", "services"}},
{"two url parts with backslash suffix", "home/services/", []string{"home", "services"}},
{"three url parts with star wildcard", "/downloads/*file", []string{"downloads", "*file"}},
}
for _, tc := range tt {
t.Run(tc.name, func(st *testing.T) {
rs := createURLParts(tc.url)
if ln := len(rs); ln != len(tc.slices) {
st.Errorf("expected result length to be %d; got %d", len(tc.slices), ln)
}
if len(tc.slices) > 0 {
for i, urlPart := range rs {
if urlPart != tc.slices[i] {
st.Errorf("expected url part at index %d to be %s; got %s", i, tc.slices[i], urlPart)
}
}
}
})
}
}
func TestCreateRoute(t *testing.T) {
router := newRouter()
if handlersLen := len(router.handlers); handlersLen != 0 {
t.Fatalf("expected num of handlers to be 0; got %d", handlersLen)
}
if nodesLen := len(router.nodes); nodesLen != 0 {
t.Fatalf("expected num of nodes to be 0; got %d", nodesLen)
}
}
func TestAddRoute(t *testing.T) {
r := newRouter()
t.Run("existance route", func(st *testing.T) {
emptyHandler := func(c *Context) {}
tt := []struct {
method string
path string
key string
}{
{http.MethodGet, "/", "GET-/"},
{http.MethodGet, "/about", "GET-/about"},
{http.MethodGet, "/downloads/*", "GET-/downloads/*"},
{http.MethodPost, "/articles/:id", "POST-/articles/:id"},
}
for _, tc := range tt {
r.addRoute(tc.method, tc.path, emptyHandler)
if _, ok := r.handlers[tc.key]; !ok {
st.Errorf("expected key %s to be exists in handlers", tc.key)
}
}
})
t.Run("handler count", func(st *testing.T) {
firstHandler := func(c *Context) {}
secondHandler := func(c *Context) {}
r.addRoute(http.MethodGet, "/", firstHandler, secondHandler)
route, ok := r.handlers["GET-/"]
if !ok {
st.Fatalf("expected route GET-/ to found; got not found")
}
if handlerCount := len(route); handlerCount != 2 {
st.Errorf("expected handler count to be 2; got %d", handlerCount)
}
})
}
func TestFindRoute(t *testing.T) {
r := newRouter()
emptyHandler := func(c *Context) {}
tt := []struct {
name string
method string
urlPattern string
requestedURL string
params map[string]string
}{
{"root url", http.MethodGet, "/", "/", map[string]string{}},
{"one parameter", http.MethodGet, "/users/:id", "users/1", map[string]string{"id": "1"}},
{"one parameter with static path on last url", http.MethodGet, "/users/:id/about", "users/1/about", map[string]string{"id": "1"}},
{"two parameter", http.MethodGet, "/users/:id/about/:section", "users/1/about/jobs", map[string]string{"id": "1", "section": "jobs"}},
}
for _, tc := range tt {
t.Run(tc.name, func(st *testing.T) {
r.addRoute(tc.method, tc.urlPattern, emptyHandler)
node, params := r.findRoute(tc.method, tc.requestedURL)
if node == nil {
st.Errorf("expected route to be found; got not found")
}
if node.urlPattern != tc.urlPattern {
st.Errorf("expected found url to be %s; got %s", tc.urlPattern, node.urlPattern)
}
if paramsLen := len(params); paramsLen != len(tc.params) {
st.Errorf("expected params length to be %d; got %d", len(tc.params), paramsLen)
}
for key, param := range params {
if param != tc.params[key] {
st.Errorf("expected param %s to be %s; got %s", key, tc.params[key], param)
}
}
})
}
}
func TestDefaultRouteHandler(t *testing.T) {
r := newRouter()
if r.defaultHandler != nil {
t.Fatalf("expected default handler to be nil; got %T", r.defaultHandler)
}
tt := []struct {
name string
method string
url string
responseCode int
responseText string
useCustomDefault bool // if it's true, this will modify default route handler using defaultCode & defaultText.
defaultCode int
defaultText string
}{
{"nano default route handler", http.MethodGet, "/", http.StatusNotFound, "nano/1.0 not found", false, 0, ""},
{"set custom default route handler", http.MethodGet, "/", http.StatusOK, "it's works", true, http.StatusOK, "it's works"},
}
for _, tc := range tt {
t.Run(tc.name, func(st *testing.T) {
rec := httptest.NewRecorder()
req, err := http.NewRequest(tc.method, tc.url, nil)
if err != nil {
log.Fatalf("could not create http request: %v", err)
}
if tc.useCustomDefault {
r.defaultHandler = func(c *Context) {
c.String(tc.defaultCode, tc.defaultText)
}
}
ctx := newContext(rec, req)
r.serveDefaultHandler(ctx)
if code := rec.Code; code != tc.responseCode {
st.Fatalf("expected response code to be %d; got %d", tc.responseCode, code)
}
if body := rec.Body.String(); body != tc.responseText {
st.Errorf("expected default handle response to be %s got %s", tc.responseText, body)
}
})
}
}
func TestHandle(t *testing.T) {
r := newRouter()
r.addRoute(http.MethodGet, "/hello/:name", func(c *Context) {
c.String(http.StatusOK, "hello %s", c.Param("name"))
})
r.addRoute(http.MethodGet, "/d/*path", func(c *Context) {
c.String(http.StatusOK, "downloading %s", c.Param("path"))
})
tt := []struct {
name string
method string
url string
responseCode int
responseText string
}{
{"not found handler", http.MethodGet, "/unregistered/path", http.StatusNotFound, "nano/1.0 not found"},
{"not found on exist path but wrong method", http.MethodPost, "/hello/foo", http.StatusNotFound, "nano/1.0 not found"},
{"echo parameter", http.MethodGet, "/hello/foo", http.StatusOK, "hello foo"},
{"echo asterisk wildcard parameter", http.MethodGet, "/d/static/app.js", http.StatusOK, "downloading static/app.js"},
}
for _, tc := range tt {
t.Run(tc.name, func(st *testing.T) {
req, err := http.NewRequest(tc.method, tc.url, nil)
if err != nil {
log.Fatalf("could not create http request: %v", err)
}
rec := httptest.NewRecorder()
ctx := newContext(rec, req)
r.handle(ctx)
if code := rec.Code; code != tc.responseCode {
st.Fatalf("expected response code to be %d; got %d", tc.responseCode, code)
}
if body := rec.Body.String(); body != tc.responseText {
st.Errorf("expected %s as response text; got %v", tc.responseText, body)
}
})
}
}