-
Notifications
You must be signed in to change notification settings - Fork 10
/
router_test.go
175 lines (161 loc) · 4.38 KB
/
router_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
164
165
166
167
168
169
170
171
172
173
174
175
package ziggurat
import (
"context"
"reflect"
"testing"
)
func shouldPanic(t *testing.T, f func()) {
defer func() { recover() }()
f()
t.Errorf("should have panicked")
}
func Test_match(t *testing.T) {
tests := []struct {
want string
name string
paths []string
input []routerEntry
}{
{
name: "should match the longest prefix without regex",
want: "foo.id",
paths: []string{"foo.id/foo_consumer"},
input: []routerEntry{{pattern: "foo.id", handler: nil}},
},
{
name: "should match when topic regex is provided",
want: "foo.id/foo_consumer/.*-log",
paths: []string{"foo.id/foo_consumer/message-log/0", "foo.id/foo_consumer/app-log/0"},
input: []routerEntry{{pattern: "foo.id/foo_consumer/.*-log", handler: nil}},
},
{
name: "should match exact partition when a certain partition is specified",
want: "",
paths: []string{"foo.id/foo_consumer/message-log/10"},
input: []routerEntry{{pattern: "foo.id/foo_consumer/message-log/1$", handler: nil}},
},
{
name: "should match the longest prefix with regex",
want: "foo.id/foo_consumer/.*-log/\\d+",
paths: []string{"foo.id/foo_consumer/message-log/5"},
input: []routerEntry{
{pattern: "foo.id/foo_consumer/.*-log/\\d+", handler: nil},
{pattern: "foo.id/foo_consumer/.*-log", handler: nil},
},
},
{
name: "should not match similar consumer group names",
want: "",
paths: []string{"foo.id/foo_consumer/"},
input: []routerEntry{
{pattern: "foo.id/foo/"},
},
},
{
name: "should not match similar consumer group names",
want: "bar.id/.*",
paths: []string{"bar.id/GO_BIRD_COMBO-booking-log/11"},
input: []routerEntry{
{pattern: "bar.id/.*"},
},
},
{
name: "regex test: should match paths ending with even numbers",
want: "bar.id/message-log/(2|4|6|8|10)$",
input: []routerEntry{
{pattern: "bar.id/message-log/(1|3|5|7|11)$"},
{pattern: "bar.id/message-log/(2|4|6|8|10)$"},
},
paths: []string{"bar.id/message-log/10"},
},
}
esToMap := func(es []routerEntry) map[string]routerEntry {
m := make(map[string]routerEntry, len(es))
for _, e := range es {
m[e.pattern] = routerEntry{pattern: e.pattern}
}
return m
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var r Router
for _, es := range test.input {
r.register(es.pattern, es.handler)
}
r.handlerEntry = esToMap(test.input)
for _, path := range test.paths {
t.Logf("matching path:%s\n", path)
_, m := r.match(path)
t.Logf("match:%s\n", m)
if m != test.want {
t.Errorf("%s test failed, expected %q got %q", test.name, test.want, m)
return
}
}
})
}
}
func Test_sortAndAppend(t *testing.T) {
var es []routerEntry
want := []routerEntry{
{pattern: "foo/bar/baz/2"},
{pattern: "foo/bar/0"},
{pattern: "bar/baz"},
}
patterns := []string{"foo/bar/0", "bar/baz", "foo/bar/baz/2"}
for _, p := range patterns {
es = sortAndAppend(es, routerEntry{pattern: p})
}
if !reflect.DeepEqual(want, es) {
t.Errorf("expected %v but got %v", want, es)
}
}
func Test_register(t *testing.T) {
var router Router
var h HandlerFunc = func(ctx context.Context, event *Event) {
}
cases := map[string]func(t *testing.T){
"should panic on empty pattern": func(t *testing.T) {
shouldPanic(t, func() {
router.HandlerFunc("", h)
})
},
"should panic on nil handler": func(t *testing.T) {
shouldPanic(t, func() {
router.HandlerFunc("/bar", nil)
})
},
"should panic on / as the pattern": func(t *testing.T) {
shouldPanic(t, func() {
router.HandlerFunc("/", h)
})
},
"should not allow multiple registrations of the same pattern": func(t *testing.T) {
shouldPanic(t, func() {
router.HandlerFunc("/bar", h)
router.HandlerFunc("/bar", h)
})
},
"sort and append should append the patterns in increasing order of len(s)": func(t *testing.T) {
var router Router
router.HandlerFunc("/bar/baz", h)
router.HandlerFunc("/bar", h)
router.HandlerFunc("/foo/bar/0", h)
want := []routerEntry{
{pattern: "/foo/bar/0", handler: h},
{pattern: "/bar/baz", handler: h},
{pattern: "/bar", handler: h},
}
if !reflect.DeepEqual(want, router.es) {
for i, w := range want {
if w.pattern != router.es[i].pattern {
t.Errorf("expected: %s, got %s", w.pattern, router.es[i].pattern)
}
}
}
},
}
for c, f := range cases {
t.Run(c, f)
}
}