Debian Codegraph @

rest/trie/impl_test.go

raw
1 package trie
2
3 import (
4 "testing"
5 )
6
7 func TestPathInsert(t *testing.T) {
8
9 trie := New()
10 if trie.root == nil {
11 t.Error("Expected to not be nil")
12 }
13
14 trie.AddRoute("GET", "/", "1")
15 if trie.root.Children["/"] == nil {
16 t.Error("Expected to not be nil")
17 }
18
19 trie.AddRoute("GET", "/r", "2")
20 if trie.root.Children["/"].Children["r"] == nil {
21 t.Error("Expected to not be nil")
22 }
23
24 trie.AddRoute("GET", "/r/", "3")
25 if trie.root.Children["/"].Children["r"].Children["/"] == nil {
26 t.Error("Expected to not be nil")
27 }
28 }
29
30 func TestTrieCompression(t *testing.T) {
31
32 trie := New()
33 trie.AddRoute("GET", "/abc", "3")
34 trie.AddRoute("GET", "/adc", "3")
35
36 // before compression
37 if trie.root.Children["/"].Children["a"].Children["b"].Children["c"] == nil {
38 t.Error("Expected to not be nil")
39 }
40 if trie.root.Children["/"].Children["a"].Children["d"].Children["c"] == nil {
41 t.Error("Expected to not be nil")
42 }
43
44 trie.Compress()
45
46 // after compression
47 if trie.root.Children["/abc"] == nil {
48 t.Errorf("%+v", trie.root)
49 }
50 if trie.root.Children["/adc"] == nil {
51 t.Errorf("%+v", trie.root)
52 }
53 }
54
55 func TestParamInsert(t *testing.T) {
56 trie := New()
57
58 trie.AddRoute("GET", "/:id/", "")
59 if trie.root.Children["/"].ParamChild.Children["/"] == nil {
60 t.Error("Expected to not be nil")
61 }
62 if trie.root.Children["/"].ParamName != "id" {
63 t.Error("Expected ParamName to be id")
64 }
65
66 trie.AddRoute("GET", "/:id/:property.:format", "")
67 if trie.root.Children["/"].ParamChild.Children["/"].ParamChild.Children["."].ParamChild == nil {
68 t.Error("Expected to not be nil")
69 }
70 if trie.root.Children["/"].ParamName != "id" {
71 t.Error("Expected ParamName to be id")
72 }
73 if trie.root.Children["/"].ParamChild.Children["/"].ParamName != "property" {
74 t.Error("Expected ParamName to be property")
75 }
76 if trie.root.Children["/"].ParamChild.Children["/"].ParamChild.Children["."].ParamName != "format" {
77 t.Error("Expected ParamName to be format")
78 }
79 }
80
81 func TestRelaxedInsert(t *testing.T) {
82 trie := New()
83
84 trie.AddRoute("GET", "/#id/", "")
85 if trie.root.Children["/"].RelaxedChild.Children["/"] == nil {
86 t.Error("Expected to not be nil")
87 }
88 if trie.root.Children["/"].RelaxedName != "id" {
89 t.Error("Expected RelaxedName to be id")
90 }
91 }
92
93 func TestSplatInsert(t *testing.T) {
94 trie := New()
95 trie.AddRoute("GET", "/*splat", "")
96 if trie.root.Children["/"].SplatChild == nil {
97 t.Error("Expected to not be nil")
98 }
99 }
100
101 func TestDupeInsert(t *testing.T) {
102 trie := New()
103 trie.AddRoute("GET", "/", "1")
104 err := trie.AddRoute("GET", "/", "2")
105 if err == nil {
106 t.Error("Expected to not be nil")
107 }
108 if trie.root.Children["/"].HttpMethodToRoute["GET"] != "1" {
109 t.Error("Expected to be 1")
110 }
111 }
112
113 func isInMatches(test string, matches []*Match) bool {
114 for _, match := range matches {
115 if match.Route.(string) == test {
116 return true
117 }
118 }
119 return false
120 }
121
122 func TestFindRoute(t *testing.T) {
123
124 trie := New()
125
126 trie.AddRoute("GET", "/", "root")
127 trie.AddRoute("GET", "/r/:id", "resource")
128 trie.AddRoute("GET", "/r/:id/property", "property")
129 trie.AddRoute("GET", "/r/:id/property.*format", "property_format")
130 trie.AddRoute("GET", "/user/#username/property", "user_property")
131
132 trie.Compress()
133
134 matches := trie.FindRoutes("GET", "/")
135 if len(matches) != 1 {
136 t.Errorf("expected one route, got %d", len(matches))
137 }
138 if !isInMatches("root", matches) {
139 t.Error("expected 'root'")
140 }
141
142 matches = trie.FindRoutes("GET", "/notfound")
143 if len(matches) != 0 {
144 t.Errorf("expected zero route, got %d", len(matches))
145 }
146
147 matches = trie.FindRoutes("GET", "/r/1")
148 if len(matches) != 1 {
149 t.Errorf("expected one route, got %d", len(matches))
150 }
151 if !isInMatches("resource", matches) {
152 t.Errorf("expected 'resource', got %+v", matches)
153 }
154 if matches[0].Params["id"] != "1" {
155 t.Error("Expected Params id to be 1")
156 }
157
158 matches = trie.FindRoutes("GET", "/r/1/property")
159 if len(matches) != 1 {
160 t.Errorf("expected one route, got %d", len(matches))
161 }
162 if !isInMatches("property", matches) {
163 t.Error("expected 'property'")
164 }
165 if matches[0].Params["id"] != "1" {
166 t.Error("Expected Params id to be 1")
167 }
168
169 matches = trie.FindRoutes("GET", "/r/1/property.json")
170 if len(matches) != 1 {
171 t.Errorf("expected one route, got %d", len(matches))
172 }
173 if !isInMatches("property_format", matches) {
174 t.Error("expected 'property_format'")
175 }
176 if matches[0].Params["id"] != "1" {
177 t.Error("Expected Params id to be 1")
178 }
179 if matches[0].Params["format"] != "json" {
180 t.Error("Expected Params format to be json")
181 }
182
183 matches = trie.FindRoutes("GET", "/user/antoine.imbert/property")
184 if len(matches) != 1 {
185 t.Errorf("expected one route, got %d", len(matches))
186 }
187 if !isInMatches("user_property", matches) {
188 t.Error("expected 'user_property'")
189 }
190 if matches[0].Params["username"] != "antoine.imbert" {
191 t.Error("Expected Params username to be antoine.imbert")
192 }
193 }
194
195 func TestFindRouteMultipleMatches(t *testing.T) {
196
197 trie := New()
198
199 trie.AddRoute("GET", "/r/1", "resource1")
200 trie.AddRoute("GET", "/r/2", "resource2")
201 trie.AddRoute("GET", "/r/:id", "resource_generic")
202 trie.AddRoute("GET", "/s/*rest", "special_all")
203 trie.AddRoute("GET", "/s/:param", "special_generic")
204 trie.AddRoute("GET", "/s/#param", "special_relaxed")
205 trie.AddRoute("GET", "/", "root")
206
207 trie.Compress()
208
209 matches := trie.FindRoutes("GET", "/r/1")
210 if len(matches) != 2 {
211 t.Errorf("expected two matches, got %d", len(matches))
212 }
213 if !isInMatches("resource_generic", matches) {
214 t.Error("Expected resource_generic to match")
215 }
216 if !isInMatches("resource1", matches) {
217 t.Error("Expected resource1 to match")
218 }
219
220 matches = trie.FindRoutes("GET", "/s/1")
221 if len(matches) != 3 {
222 t.Errorf("expected two matches, got %d", len(matches))
223 }
224 if !isInMatches("special_all", matches) {
225 t.Error("Expected special_all to match")
226 }
227 if !isInMatches("special_generic", matches) {
228 t.Error("Expected special_generic to match")
229 }
230 if !isInMatches("special_relaxed", matches) {
231 t.Error("Expected special_relaxed to match")
232 }
233 }
234
235 func TestConsistentPlaceholderName(t *testing.T) {
236
237 trie := New()
238
239 trie.AddRoute("GET", "/r/:id", "oneph")
240 err := trie.AddRoute("GET", "/r/:rid/other", "twoph")
241 if err == nil {
242 t.Error("Should have died on inconsistent placeholder name")
243 }
244
245 trie.AddRoute("GET", "/r/#id", "oneph")
246 err = trie.AddRoute("GET", "/r/#rid/other", "twoph")
247 if err == nil {
248 t.Error("Should have died on inconsistent placeholder name")
249 }
250
251 trie.AddRoute("GET", "/r/*id", "oneph")
252 err = trie.AddRoute("GET", "/r/*rid", "twoph")
253 if err == nil {
254 t.Error("Should have died on duplicated route")
255 }
256 }
257
258 func TestDuplicateName(t *testing.T) {
259
260 trie := New()
261
262 err := trie.AddRoute("GET", "/r/:id/o/:id", "two")
263 if err == nil {
264 t.Error("Should have died, this route has two placeholder named `id`")
265 }
266
267 err = trie.AddRoute("GET", "/r/:id/o/*id", "two")
268 if err == nil {
269 t.Error("Should have died, this route has two placeholder named `id`")
270 }
271
272 err = trie.AddRoute("GET", "/r/:id/o/#id", "two")
273 if err == nil {
274 t.Error("Should have died, this route has two placeholder named `id`")
275 }
276 }