-
Notifications
You must be signed in to change notification settings - Fork 0
/
cherry_test.go
386 lines (344 loc) · 9.29 KB
/
cherry_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package cherry
import (
"bytes"
"errors"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"golang.org/x/net/context"
)
var noopHandler = func(ctx *Context) error { return nil }
func TestHandle(t *testing.T) {
c := New()
handler := http.HandlerFunc(func(c http.ResponseWriter, r *http.Request) {})
for _, method := range []string{"GET", "PUT", "POST", "DELETE"} {
c.Handle(method, "/", handler)
code, _ := doRequest(t, method, "/", nil, c)
isHTTPStatusOK(t, code)
}
}
func TestMethodGet(t *testing.T) {
c := New()
c.Get("/", noopHandler)
code, _ := doRequest(t, "GET", "/", nil, c)
isHTTPStatusOK(t, code)
}
func TestMethodPost(t *testing.T) {
c := New()
c.Post("/", noopHandler)
code, _ := doRequest(t, "POST", "/", nil, c)
isHTTPStatusOK(t, code)
}
func TestMethodPut(t *testing.T) {
c := New()
c.Put("/", noopHandler)
code, _ := doRequest(t, "PUT", "/", nil, c)
isHTTPStatusOK(t, code)
}
func TestMethodDelete(t *testing.T) {
c := New()
c.Delete("/", noopHandler)
code, _ := doRequest(t, "DELETE", "/", nil, c)
isHTTPStatusOK(t, code)
}
func TestMethodHead(t *testing.T) {
c := New()
c.Head("/", noopHandler)
code, _ := doRequest(t, "HEAD", "/", nil, c)
isHTTPStatusOK(t, code)
}
func TestMethodOptions(t *testing.T) {
c := New()
c.Options("/", noopHandler)
code, _ := doRequest(t, "OPTIONS", "/", nil, c)
isHTTPStatusOK(t, code)
}
func TestBox(t *testing.T) {
c := New()
sr := c.Group("/foo")
sr.Get("/bar", noopHandler)
code, _ := doRequest(t, "GET", "/foo/bar", nil, c)
isHTTPStatusOK(t, code)
}
func TestStatic(t *testing.T) {
c := New()
c.Static("/public", "./")
code, body := doRequest(t, "GET", "/public/README.md", nil, c)
isHTTPStatusOK(t, code)
if len(body) == 0 {
t.Error("body cannot be empty")
}
if !strings.Contains(body, "cherry") {
t.Error("expecting body containing string (cherry)")
}
code, _ = doRequest(t, "GET", "/public/nofile", nil, c)
if code != http.StatusNotFound {
t.Errorf("expecting status 404 got %d", code)
}
}
func TestContext(t *testing.T) {
c := New()
c.Get("/", checkContext(t, "m1", "m1"))
c.Use(func(ctx *Context) error {
ctx.Context = context.WithValue(ctx.Context, "m1", "m1")
return nil
})
code, _ := doRequest(t, "GET", "/", nil, c)
isHTTPStatusOK(t, code)
c.Get("/some", checkContext(t, "m1", "m2"))
c.Use(func(ctx *Context) error {
ctx.Context = context.WithValue(ctx.Context, "m1", "m2")
ctx.Response().WriteHeader(http.StatusBadRequest)
return nil
})
code, _ = doRequest(t, "GET", "/some", nil, c)
if code != http.StatusBadRequest {
t.Errorf("expecting %d, got %d", http.StatusBadRequest, code)
}
}
func TestContextWithSubrouter(t *testing.T) {
c := New()
sub := c.Group("/test")
sub.Get("/", checkContext(t, "a", "b"))
sub.Use(func(ctx *Context) error {
ctx.Context = context.WithValue(ctx.Context, "a", "b")
return nil
})
code, _ := doRequest(t, "GET", "/test", nil, c)
if code != http.StatusOK {
t.Errorf("expected status code 200 got %d", code)
}
}
func TestBindContext(t *testing.T) {
c := New()
c.BindContext(context.WithValue(context.Background(), "a", "b"))
c.Get("/", checkContext(t, "a", "b"))
sub := c.Group("/foo")
sub.Get("/", checkContext(t, "a", "b"))
code, _ := doRequest(t, "GET", "/", nil, c)
isHTTPStatusOK(t, code)
code, _ = doRequest(t, "GET", "/foo", nil, c)
isHTTPStatusOK(t, code)
}
func TestBindContextSubrouter(t *testing.T) {
c := New()
sub := c.Group("/foo")
sub.Get("/", checkContext(t, "foo", "bar"))
sub.BindContext(context.WithValue(context.Background(), "foo", "bar"))
code, _ := doRequest(t, "GET", "/foo", nil, c)
isHTTPStatusOK(t, code)
}
func checkContext(t *testing.T, key, expect string) Handler {
return func(ctx *Context) error {
value := ctx.Context.Value(key).(string)
if value != expect {
t.Errorf("expected %s got %s", expect, value)
}
return nil
}
}
func TestMiddleware(t *testing.T) {
buf := &bytes.Buffer{}
c := New()
c.Use(func(ctx *Context) error {
buf.WriteString("a")
return nil
})
c.Use(func(ctx *Context) error {
buf.WriteString("b")
return nil
})
c.Use(func(ctx *Context) error {
buf.WriteString("c")
return nil
})
c.Use(func(ctx *Context) error {
buf.WriteString("d")
return nil
})
c.Get("/", noopHandler)
code, _ := doRequest(t, "GET", "/", nil, c)
isHTTPStatusOK(t, code)
if buf.String() != "abcd" {
t.Errorf("expecting abcd got %s", buf.String())
}
}
func TestBoxMiddlewareReset(t *testing.T) {
buf := &bytes.Buffer{}
c := New()
c.Use(func(ctx *Context) error {
buf.WriteString("a")
return nil
})
c.Use(func(ctx *Context) error {
buf.WriteString("b")
return nil
})
sub := c.Group("/sub").Reset()
sub.Get("/", noopHandler)
code, _ := doRequest(t, "GET", "/sub", nil, c)
isHTTPStatusOK(t, code)
if buf.String() != "" {
t.Errorf("expecting empty buffer got %s", buf.String())
}
}
func TestBoxMiddlewareInheritsParent(t *testing.T) {
buf := &bytes.Buffer{}
c := New()
c.Use(func(ctx *Context) error {
buf.WriteString("a")
return nil
})
c.Use(func(ctx *Context) error {
buf.WriteString("b")
return nil
})
sub := c.Group("/sub")
sub.Get("/", noopHandler)
code, _ := doRequest(t, "GET", "/sub", nil, c)
isHTTPStatusOK(t, code)
if buf.String() != "ab" {
t.Errorf("expecting ab got %s", buf.String())
}
}
func TestErrorHandler(t *testing.T) {
c := New()
errorMsg := "oops! something went wrong"
c.SetErrorHandler(func(ctx *Context, err error) {
ctx.Response().WriteHeader(http.StatusInternalServerError)
if err.Error() != errorMsg {
t.Errorf("expecting %s, got %s", errorMsg, err.Error())
}
})
c.Use(func(ctx *Context) error {
return errors.New(errorMsg)
})
c.Get("/", noopHandler)
code, _ := doRequest(t, "GET", "/", nil, c)
if code != http.StatusInternalServerError {
t.Errorf("expecting code 500 got %d", code)
}
}
func TestWeaveboxHandler(t *testing.T) {
c := New()
handle := func(respStr string) Handler {
return func(ctx *Context) error {
return ctx.Text(http.StatusOK, respStr)
}
}
c.Get("/a", handle("a"))
c.Get("/b", handle("b"))
c.Get("/c", handle("c"))
for _, r := range []string{"a", "b", "c"} {
code, body := doRequest(t, "GET", "/"+r, nil, c)
isHTTPStatusOK(t, code)
if body != r {
t.Errorf("expecting %s got %s", r, body)
}
}
}
func TestNotFoundHandler(t *testing.T) {
c := New()
code, body := doRequest(t, "GET", "/", nil, c)
if code != http.StatusNotFound {
t.Errorf("expecting code 404 got %d", code)
}
if !strings.Contains(body, "404 page not found") {
t.Errorf("expecting body: 404 page not found got %s", body)
}
}
func TestSetNotFound(t *testing.T) {
c := New()
notFoundMsg := "hey! not found"
h := http.HandlerFunc(func(c http.ResponseWriter, r *http.Request) {
c.WriteHeader(http.StatusNotFound)
c.Write([]byte(notFoundMsg))
})
c.SetNotFound(h)
code, body := doRequest(t, "GET", "/", nil, c)
if code != http.StatusNotFound {
t.Errorf("expecting code 404 got %d", code)
}
if !strings.Contains(body, notFoundMsg) {
t.Errorf("expecting body: %s got %s", notFoundMsg, body)
}
}
func TestMethodNotAllowed(t *testing.T) {
c := New()
c.Get("/", noopHandler)
code, body := doRequest(t, "POST", "/", nil, c)
if code != http.StatusMethodNotAllowed {
t.Errorf("expecting code 405 got %d", code)
}
if !strings.Contains(body, "Method Not Allowed") {
t.Errorf("expecting body: Method Not Allowed got %s", body)
}
}
func TestSetMethodNotAllowed(t *testing.T) {
c := New()
handler := http.HandlerFunc(func(c http.ResponseWriter, r *http.Request) {
c.WriteHeader(http.StatusMethodNotAllowed)
c.Write([]byte("foo"))
})
c.SetMethodNotAllowed(handler)
c.Get("/", noopHandler)
code, body := doRequest(t, "POST", "/", nil, c)
if code != http.StatusMethodNotAllowed {
t.Errorf("expecting code 405 got %d", code)
}
if !strings.Contains(body, "foo") {
t.Errorf("expecting body: foo got %s", body)
}
}
func TestContextURLQuery(t *testing.T) {
req, _ := http.NewRequest("GET", "/?name=anthony", nil)
ctx := &Context{request: req}
if ctx.Query("name") != "anthony" {
t.Errorf("expected anthony got %s", ctx.Query("name"))
}
}
func TestContextForm(t *testing.T) {
values := url.Values{}
values.Set("email", "john@gmail.com")
req, _ := http.NewRequest("POST", "/", strings.NewReader(values.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
ctx := &Context{request: req}
if ctx.Form("email") != "john@gmail.com" {
t.Errorf("expected john@gmail.com got %s", ctx.Form("email"))
}
}
func TestContextHeader(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Add("x-test", "test")
ctx := &Context{request: req}
if ctx.Header("x-test") != "test" {
t.Errorf("expected header to be (test) got %s", ctx.Header("x-test"))
}
}
func TestTerminal(t *testing.T) {
c := New()
handler := http.HandlerFunc(func(c http.ResponseWriter, r *http.Request) {
c.WriteHeader(http.StatusMethodNotAllowed)
c.Write([]byte("foo"))
})
c.SetMethodNotAllowed(handler)
c.Get("/", noopHandler)
c.Serve(3000)
}
func isHTTPStatusOK(t *testing.T, code int) {
if code != http.StatusOK {
t.Errorf("Expecting status 200 got %d", code)
}
}
func doRequest(t *testing.T, method, route string, body io.Reader, c *Cherry) (int, string) {
r, err := http.NewRequest(method, route, body)
if err != nil {
t.Fatal(err)
}
rw := httptest.NewRecorder()
c.ServeHTTP(rw, r)
return rw.Code, rw.Body.String()
}