/
router_test.go
88 lines (68 loc) · 1.94 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
// A fast web framework written in Go.
//
// Author: Vincent Composieux <vincent.composieux@gmail.com>
package gofast
import (
"testing"
)
// TestAllAddMethods tests different add methods
func TestAllAddMethods(t *testing.T) {
router := NewRouter()
router.Get("get", "/get", func(c Context) {})
router.Post("post", "/post", func(c Context) {})
router.Patch("patch", "/patch", func(c Context) {})
router.Put("put", "/put", func(c Context) {})
router.Delete("delete", "/delete", func(c Context) {})
router.Options("options", "/options", func(c Context) {})
router.Head("head", "/head", func(c Context) {})
if 7 != len(router.GetRoutes()) {
t.Fail()
}
route := router.GetRoute("get")
if route.method != "GET" || route.pattern.String() != "/get" {
t.Fail()
}
route = router.GetRoute("post")
if route.method != "POST" || route.pattern.String() != "/post" {
t.Fail()
}
route = router.GetRoute("patch")
if route.method != "PATCH" || route.pattern.String() != "/patch" {
t.Fail()
}
route = router.GetRoute("put")
if route.method != "PUT" || route.pattern.String() != "/put" {
t.Fail()
}
route = router.GetRoute("delete")
if route.method != "DELETE" || route.pattern.String() != "/delete" {
t.Fail()
}
route = router.GetRoute("options")
if route.method != "OPTIONS" || route.pattern.String() != "/options" {
t.Fail()
}
route = router.GetRoute("head")
if route.method != "HEAD" || route.pattern.String() != "/head" {
t.Fail()
}
}
// TestFallbackRoute tests adding a fallback route
func TestFallbackRoute(t *testing.T) {
router := NewRouter()
router.SetFallback(func(c Context) {})
fallback := router.GetFallback()
if "fallback" != fallback.name {
t.Fail()
}
}
// TestRouteGetHandler tests route handling getter
func TestRouteGetHandler(t *testing.T) {
handler := func(c Context) {}
router := NewRouter()
router.Get("get", "/get", handler)
route := router.GetRoute("get")
if route.GetHandler() == nil {
t.Fail()
}
}