-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnano_test.go
99 lines (77 loc) · 2.15 KB
/
nano_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
package nano
import (
"log"
"net/http"
"net/http/httptest"
"testing"
)
func TestUseMiddleware(t *testing.T) {
app := New()
emptyHandler := func(c *Context) {}
app.Use(emptyHandler, emptyHandler, emptyHandler)
if mlen := len(app.middlewares); mlen != 3 {
t.Errorf("expect num of middlewares to be 3; got %d", mlen)
}
}
func TestGroup(t *testing.T) {
app := New()
api := app.Group("/api")
if api.prefix != "/api" {
t.Errorf("expected group prefix to be /api; got %s", api.prefix)
}
finance := api.Group("/finance")
if finance.prefix != "/api/finance" {
t.Errorf("expected group prefix to be /api/finance; got %s", api.prefix)
}
}
func TestRouteRegistration(t *testing.T) {
app := New()
emptyHandler := func(c *Context) {}
app.GET("/", emptyHandler)
app.POST("/", emptyHandler)
app.PUT("/", emptyHandler)
app.DELETE("/", emptyHandler)
if hlen := len(app.router.handlers); hlen != 4 {
t.Errorf("expected num of registered routes to be 4; got %d", hlen)
}
}
func TestDefaultHandler(t *testing.T) {
app := New()
if app.router.defaultHandler != nil {
t.Fatalf("expected initial value of default handler to be nil")
}
t.Run("set default handler", func(st *testing.T) {
app.Default(func(c *Context) {
c.String(http.StatusOK, "ok")
})
if app.router.defaultHandler == nil {
st.Errorf("expected default handler to be setted; got %v", app.router.defaultHandler)
}
})
t.Run("set default handler when it already set", func(st *testing.T) {
err := app.Default(func(c *Context) {
c.String(http.StatusOK, "ok")
})
if err != ErrDefaultHandler {
st.Errorf("expected result to be ErrDefaultHandler; got %v", err)
}
})
}
func TestServeHTTP(t *testing.T) {
app := New()
app.GET("/", func(c *Context) {
c.String(http.StatusOK, "ok")
})
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
log.Fatalf("could not make http request: %v", err)
}
rec := httptest.NewRecorder()
app.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected response code to be 200; got %d", rec.Code)
}
if body := rec.Body.String(); body != "ok" {
t.Errorf("expected response text to be ok; got %s", body)
}
}