-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
84 lines (58 loc) · 1.67 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
package husky
import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAddingRoute(t *testing.T) {
h := New()
h.GET("/path", func(c *CTX) error {
return c.JSON(200, "This is a test")
})
found := h.Router.GetRoutes("GET")
assert.True(t, reflect.TypeOf(found["GET/path"]).String() == "husky.Route")
}
func TestAddingRouteWithMiddleware(t *testing.T) {
h := New()
h.GET("/path", func(c *CTX) error {
return c.JSON(200, "This is a test")
}, middlware)
found := h.Router.GetRoutes("GET")
assert.Equal(t, 1, len(found["GET/path"].Middleware))
}
func TestHandlerReturnsHandler(t *testing.T) {
h := New()
h.GET("/path", func(ctx *CTX) error {
return ctx.JSON(200, "This is a test")
})
found := h.Router.GetRoutes("GET")
route := found["GET/path"]
assert.True(t, reflect.TypeOf(route.Handler).String() == "husky.Handler")
}
func TestGetRoutesReturnsRoutes(t *testing.T) {
h := New()
h.GET("/path", func(c *CTX) error {
return c.JSON(200, "This is a test")
})
found := h.Router.GetRoutes("GET")
assert.True(t, reflect.TypeOf(found["GET/path"]).String() == "husky.Route")
}
func TestGetRoutesReturnsEmptyIfRouteNotFound(t *testing.T) {
h := New()
h.GET("/path", func(c *CTX) error {
return c.JSON(200, "This is a test")
})
found := h.Router.GetRoutes("POST")
assert.Empty(t, found)
}
func TestRootPathReturnsEmptyMap(t *testing.T) {
h := New()
r, _ := http.NewRequest("GET", "/", strings.NewReader(JSON))
w := httptest.NewRecorder()
h.Context = h.NewContext(w, r)
assert.Empty(t, h.Context.GetParams())
assert.True(t, reflect.TypeOf(h.Context.GetParams()).String() == "map[string]string")
}