-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
router_test.go
74 lines (63 loc) · 1.75 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
package godzilla
import (
"sync"
"testing"
)
func TestHandle(t *testing.T) {
routes := []struct {
method string
path string
conflict bool
handlers handlersChain
}{
{method: MethodGet, path: "/articles/search", conflict: false, handlers: fakeHandlersChain},
{method: MethodGet, path: "/articles/test", conflict: false, handlers: fakeHandlersChain},
{method: MethodGet, path: "", conflict: true, handlers: fakeHandlersChain},
{method: "", path: "/articles/test", conflict: true, handlers: fakeHandlersChain},
{method: MethodGet, path: "orders/test", conflict: true, handlers: fakeHandlersChain},
{method: MethodGet, path: "/books/test", conflict: true, handlers: emptyHandlersChain},
}
router := &router{
settings: &Settings{},
cache: make(map[string]*matchResult),
pool: sync.Pool{
New: func() interface{} {
return new(context)
},
},
}
for _, route := range routes {
recv := catchPanic(func() {
router.handle(route.method, route.path, route.handlers)
})
if route.conflict {
if recv == nil {
t.Errorf("no panic for conflicting route '%s'", route.path)
}
} else if recv != nil {
t.Errorf("unexpected panic for route '%s': %v", route.path, recv)
}
}
}
func TestHandler(t *testing.T) {
routes := []struct {
method string
path string
handlers handlersChain
}{
{method: MethodGet, path: "/articles/search", handlers: fakeHandlersChain},
{method: MethodGet, path: "/articles/test", handlers: fakeHandlersChain},
}
router := &router{
settings: &Settings{},
cache: make(map[string]*matchResult),
pool: sync.Pool{
New: func() interface{} {
return new(context)
},
},
}
for _, route := range routes {
router.handle(route.method, route.path, route.handlers)
}
}