-
Notifications
You must be signed in to change notification settings - Fork 0
/
routetrie_test.go
87 lines (77 loc) · 2.63 KB
/
routetrie_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
// Copyright 2013 Steven Le. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package routetrie
import (
"testing"
)
func TestRouteTrie(t *testing.T) {
trie := NewRouteTrie()
// Verify a direct path.
trie.Add("example.com/foo", 1)
value, _ := trie.Get("example.com/foo")
if value != 1 {
t.Errorf("Expected value to be 1, but was: %v", value)
}
// Verify another direct path.
trie.Add("example.com/foo/bar", 2)
value, _ = trie.Get("example.com/foo/bar")
if value != 2 {
t.Errorf("Expected value to be 2, but was: %v", value)
}
// Verify a :param path.
trie.Add("example.com/foo/:bar/baz", 3)
value, params := trie.Get("example.com/foo/bar/baz")
if value != 3 {
t.Errorf("Expected value to be 3, but was: %v", value)
}
if params["bar"] != "bar" {
t.Errorf("Expected value of :bar to be \"bar\", but was: %v", params["bar"])
}
// Verify an unnamed *wildcard path.
trie.Add("example.com/*", 4)
value, params = trie.Get("example.com/abc/def")
if value != 4 {
t.Errorf("Expected value to be 4, but was: %v", value)
}
if len(params) != 0 {
t.Errorf("Expected empty map, but was: %v", params)
}
// Verify a named *wildcard path.
trie.Add("example.com/foo/*wildcard", 5)
value, params = trie.Get("example.com/foo/blahblahblah")
if params["wildcard"] != "blahblahblah" {
t.Errorf("Expected *wildcard to be \"blahblahblah\" but was %v", params["wildcard"])
}
// Verify multiple matching routes.
trie.Add("example.com/one/:two", 6)
trie.Add("example.com/:one/two", 7)
trie.Add("example.com/one/*two", 8)
value, params = trie.Get("example.com/one/two")
if value != 6 {
t.Errorf("Expected value to be 6, but was: %v", value)
}
if len(params) != 1 || params["two"] != "two" {
t.Errorf("Unexpected :param values: %v", params)
}
// Verify routes that walk up and back down a trie.
trie.Add("example.com/test/one/:two", 9)
trie.Add("example.com/test/:one/two/three", 10)
value, params = trie.Get("example.com/test/one/two/three")
if value != 10 {
t.Errorf("Expected value to be 10, but was: %v", value)
}
if len(params) != 1 || params["one"] != "one" {
t.Errorf("Unexpected :param values: %v", params)
}
}