-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathTree_test.go
103 lines (78 loc) · 3.07 KB
/
pathTree_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
package rero
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var tree pathTree
var _ = Describe("pathTree", func() {
Context("createPathTree", func() {
BeforeEach(func() {
tree = createPathTree()
})
It("returns an instance", func() {
Expect(tree).NotTo(BeNil())
})
})
Context("getPathContext", func() {
handler := func(any RequestContext) {}
BeforeEach(func() {
tree = createPathTree()
})
It("returns list of existing handlers", func() {
tree.addPathHandler("GET", "/a", handler).addPathHandler("GET", "/a", handler)
Expect(len(tree.getPathContext("GET", "/a").handlers)).To(Equal(2))
})
It("ignores trailing slash", func() {
tree.addPathHandler("GET", "/", handler).addPathHandler("GET", "", handler)
Expect(len(tree.getPathContext("GET", "/").handlers)).To(Equal(2))
})
It("compares complete path", func() {
tree.addPathHandler("GET", "/a/b/c", handler).addPathHandler("GET", "/z/b/c", handler)
Expect(len(tree.getPathContext("GET", "/a/b/c").handlers)).To(Equal(1))
Expect(len(tree.getPathContext("GET", "/z/b/c").handlers)).To(Equal(1))
})
It("skips intermediate path segments", func() {
tree.addPathHandler("GET", "/a/b/c", handler)
Expect(len(tree.getPathContext("GET", "/").handlers)).To(Equal(0))
Expect(len(tree.getPathContext("GET", "/a").handlers)).To(Equal(0))
Expect(len(tree.getPathContext("GET", "/a/b").handlers)).To(Equal(0))
})
It("respects method selector", func() {
tree.addPathHandler("PUT", "/a/b/c", handler)
Expect(len(tree.getPathContext("GET", "/a/b/c").handlers)).To(Equal(0))
Expect(len(tree.getPathContext("PUT", "/a/b/c").handlers)).To(Equal(1))
})
It("matches longest path only", func() {
tree.addPathHandler("GET", "/a", handler).
addPathHandler("GET", "/a/b", handler)
Expect(len(tree.getPathContext("GET", "/a/b").handlers)).To(Equal(1))
})
It("returns an empty array for unregistered paths", func() {
Expect(len(tree.getPathContext("GET", "").handlers)).To(Equal(0))
Expect(len(tree.getPathContext("GET", "/a").handlers)).To(Equal(0))
Expect(len(tree.getPathContext("GET", "/z/yy").handlers)).To(Equal(0))
})
It("replaces path variables", func() {
tree.addPathHandler("GET", "/a/:var1:/c", handler)
tree.addPathHandler("GET", "/a/:var1:/a/d/:var2:/f", handler)
Expect(tree.getPathContext("GET", "/a/alice/c").pathVariables).
To(Equal(map[string]string{"var1": "alice"}))
Expect(tree.getPathContext("GET", "/a/john/a/d/murdoch/f").pathVariables).
To(Equal(map[string]string{"var1": "john", "var2": "murdoch"}))
})
It("panic if non-var segment clashes with var segment", func() {
tree.addPathHandler("GET", "/a/:var1:/c", handler)
addNonUniformPath := func() {
tree.addPathHandler("GET", "/a/b/c", handler)
}
Expect(addNonUniformPath).To(Panic())
})
It("panic if var segment clashes with non-var segment", func() {
tree.addPathHandler("GET", "/a/b/c", handler)
addNonUniformPath := func() {
tree.addPathHandler("GET", "/a/:var1:/c", handler)
}
Expect(addNonUniformPath).To(Panic())
})
})
})