-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tree_test.go
133 lines (120 loc) · 2.94 KB
/
tree_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
package godzilla
import "testing"
func catchPanic(f func()) (recv interface{}) {
defer func() {
recv = recover()
}()
f()
return
}
type testRoute struct {
path string
conflict bool
}
func TestAddRoute(t *testing.T) {
tree := createRootNode()
routes := []testRoute{
{"/cmd/:tool/:sub", false},
{"/cmd/vet", true},
{"/src/*", false},
{"/src/*", true},
{"/src/test", true},
{"/src/:test", true},
{"/src/", false},
{"/src1/", false},
{"/src1/*", false},
{"/search/:query", false},
{"/search/invalid", true},
{"/user_:name", false},
{"/user_x", false},
{"/id:id", false},
{"/id/:id", false},
{"/id/:value", true},
{"/id/:id/settings", false},
{"/id/:id/:type", true},
{"/*", true},
{"books/*/get", true},
{"/file/test", false},
{"/file/test", true},
{"/file/:test", true},
{"/orders/:id/settings/:id", true},
{"/accounts/*/settings", true},
{"/results/*", false},
{"/results/*/view", true},
}
for _, route := range routes {
recv := catchPanic(func() {
tree.addRoute(route.path, emptyHandlersChain)
})
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)
}
}
}
type testRequests []struct {
path string
match bool
params map[string]string
}
func TestMatchRoute(t *testing.T) {
tree := createRootNode()
routes := [...]string{
"/hi",
"/contact",
"/users/:id/",
"/books/*",
"/search/:item1/settings/:item2",
"/co",
"/c",
"/a",
"/ab",
"/doc/",
"/doc/go_faq.html",
"/doc/go1.html",
"/α",
"/β",
}
for _, route := range routes {
tree.addRoute(route, emptyHandlersChain)
}
requests := testRequests{
{"/a", true, nil},
{"/", false, nil},
{"/hi", true, nil},
{"/contact", true, nil},
{"/co", true, nil},
{"/con", false, nil}, // key mismatch
{"/cona", false, nil}, // key mismatch
{"/no", false, nil}, // no matching child
{"/ab", true, nil},
{"/α", true, nil},
{"/β", true, nil},
{"/users/test", true, map[string]string{"id": "test"}},
{"/books/title", true, nil},
{"/search/test1/settings/test2", true, map[string]string{"item1": "test1", "item2": "test2"}},
{"/search/test1", false, nil},
{"test", false, nil},
}
for _, request := range requests {
ctx := &context{paramValues: make(map[string]string)}
handler := tree.matchRoute(request.path, ctx)
if handler == nil {
if request.match {
t.Errorf("handle mismatch for route '%s': Expected non-nil handle", request.path)
}
} else if !request.match {
t.Errorf("handle mismatch for route '%s': Expected nil handle", request.path)
}
for expectedKey, expectedValue := range request.params {
actualValue := ctx.Param(expectedKey)
if actualValue != expectedValue {
t.Errorf(" mismatch for route '%s' parameter '%s' actual '%s', expected '%s'",
request.path, expectedKey, actualValue, expectedValue)
}
}
}
}