-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.go
163 lines (144 loc) · 3.83 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cotton
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func printTree(tree *tree) {
fmt.Println("=============")
tree.root.print(0)
fmt.Println("=============")
}
func TestAddConflicts(t *testing.T) {
assert.PanicsWithError(t, "path [test] must start with /", func() {
tree := newTree()
tree.add("test", nil)
})
assert.PanicsWithError(t, "[:method] in path [/a/:method] conflicts with [/a/:test]", func() {
tree := newTree()
tree.add("/a/:test", nil)
tree.add("/a/:method", nil)
})
assert.PanicsWithError(t, "[:method] in path [/a/:method] conflicts with [/a/*file]", func() {
tree := newTree()
tree.add("/a/*file", nil)
tree.add("/a/:method", nil)
})
assert.PanicsWithError(t, "[*file] in path [/a/*file] conflicts with [/a/:method]", func() {
tree := newTree()
tree.add("/a/:method", nil)
tree.add("/a/*file", nil)
})
assert.PanicsWithError(t, "action [*file] must end of path [/a/*file/test]", func() {
tree := newTree()
tree.add("/a/*file/test", nil)
})
assert.PanicsWithError(t, "path [/:test/12] conflicts with [/:test/:id]", func() {
tree := newTree()
tree.add("/:test/:id", nil)
tree.add("/:test/12", nil)
})
assert.NotPanics(t, func() {
tree := newTree()
tree.add("/:test/:id", nil)
tree.add("/:test/12/abc", nil)
})
assert.PanicsWithError(t, "path [/:test/:id] conflicts with [/:test/12]", func() {
tree := newTree()
tree.add("/:test/12", nil)
tree.add("/:test/:id", nil)
})
assert.PanicsWithError(t, "path [/:test/12] conflicts with [/:test/*file]", func() {
tree := newTree()
tree.add("/:test/*file", nil)
tree.add("/:test/12", nil)
})
assert.PanicsWithError(t, "path [/:test/*file] conflicts with [/:test/12]", func() {
tree := newTree()
tree.add("/:test/12", nil)
tree.add("/:test/*file", nil)
})
assert.PanicsWithError(t, "path [/s/*file] conflicts with [/*file]", func() {
tree := newTree()
tree.add("/*file", nil)
tree.add("/s/*file", nil)
})
}
func TestTreeMultipleEOP(t *testing.T) {
assert.PanicsWithError(t, "path [//a] has multiple '/'", func() {
tree := newTree()
tree.add("//a", nil)
})
}
func TestRouterHasSpace(t *testing.T) {
assert.PanicsWithError(t, "path [/ /a] has space", func() {
tree := newTree()
tree.add("/ /a", nil)
})
}
func TestTree(t *testing.T) {
tree := newTree()
arrRouter := []string{
"/",
"/a",
"/a/",
"/a/:method",
"/a/:method/:name",
"/a/:method/:name/test",
"/b/",
"/b/*file",
}
var tmpValue string
for _, path := range arrRouter {
tree.add(path, func(p string) HandlerFunc {
return func(c *Context) {
tmpValue = "for " + p
}
}(path))
}
tmpValue += ""
for _, path := range arrRouter {
tmpValue = ""
pathFind := strings.ReplaceAll(path, ":", "v")
result := tree.root.find(pathFind)
assert.NotNil(t, result)
assert.NotNil(t, result.node.handler)
result.node.handler(nil)
assert.Equal(t, "for "+path, tmpValue)
c := strings.Count(path, ":")
if c > 0 {
assert.Equal(t, c, len(result.params), path)
}
}
}
func TestEmptyParam(t *testing.T) {
tree := newTree()
tree.add("/test/:conf", nil)
tree.add("/test1/:conf/:test", nil)
result := tree.root.find("/test/abc")
assert.Equal(t, map[string]string{
"conf": "abc",
}, result.params)
// not match anything
result = tree.root.find("/test/")
assert.Equal(t, map[string]string{}, result.params)
// not match anything
result = tree.root.find("/")
assert.Equal(t, map[string]string{}, result.params)
// not match anything
result = tree.root.find("/test1/")
assert.Equal(t, map[string]string{}, result.params)
}
func TestTreeNotFound(t *testing.T) {
tree := newTree()
tree.add("/v1/a", nil)
result := tree.root.find("/v1/v1/a")
assert.Nil(t, result.node)
}
func TestTree2(t *testing.T) {
tree := newTree()
tree.add("/v1/a/", nil)
result := tree.root.find("/v1///a/")
assert.NotNil(t, result.node)
}