forked from go-humble/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
202 lines (196 loc) · 5.48 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2015 Alex Browne and Soroush Pour.
// Allrights reserved. Use of this source code is
// governed by the MIT license, which can be found
// in the LICENSE file.
package router
import (
"reflect"
"testing"
)
// routeTestCases is format for test case. Each test case takes possible route paths, the URL path provided
// and the expected route path and query parameters.
var routeTestCases = []struct {
paths []string
path string
expectedPath string
expectedParams map[string]string
expectedQueryParams map[string][]string
}{
// Test route to /
{
paths: []string{"/home", "/about", "/"},
path: "/",
expectedPath: "/",
expectedParams: nil,
},
// Test basic literal
{
paths: []string{"/home", "/about"},
path: "/home",
expectedPath: "/home",
expectedParams: nil,
},
// Test trailing slash in path
{
paths: []string{"/home", "/about"},
path: "/about/",
expectedPath: "/about",
expectedParams: nil,
},
// Test query param
{
paths: []string{"/home", "/home/{homeId}", "/about"},
path: "/home/55",
expectedPath: "/home/{homeId}",
expectedParams: map[string]string{
"homeId": "55",
},
},
// Test trailing slash after query param in path
{
paths: []string{"/home", "/about", "/about/{aboutId}"},
path: "/about/55/",
expectedPath: "/about/{aboutId}",
expectedParams: map[string]string{
"aboutId": "55",
},
},
// Test multiple query params
{
paths: []string{"/home", "/home/{homeId}", "/about", "/home/{homeId}/image/{imageSize}/{imageType}/jpg"},
path: "/home/55/image/800x600/panoramic/jpg",
expectedPath: "/home/{homeId}/image/{imageSize}/{imageType}/jpg",
expectedParams: map[string]string{
"homeId": "55",
"imageSize": "800x600",
"imageType": "panoramic",
},
},
// Test tie breaker
{
paths: []string{"/home/all", "/home/{homeId}", "/about"},
path: "/home/all",
expectedPath: "/home/all",
expectedParams: nil,
},
// Test query param with weird characters
{
paths: []string{"/home", "/home/{homeId}", "/about"},
path: "/home/@1.b$",
expectedPath: "/home/{homeId}",
expectedParams: map[string]string{
"homeId": "@1.b$",
},
},
// Test trailing slash after query param in path
{
paths: []string{"/home", "/about", "/about/{aboutId}"},
path: "/about/@1.b$/",
expectedPath: "/about/{aboutId}",
expectedParams: map[string]string{
"aboutId": "@1.b$",
},
},
// Test multiple query params
{
paths: []string{"/home", "/home/{homeId}", "/about", "/home/{homeId}/image/{imageSize}/{imageType}/jpg"},
path: "/home/@1.b$/image/800^600/%20a+/jpg",
expectedPath: "/home/{homeId}/image/{imageSize}/{imageType}/jpg",
expectedParams: map[string]string{
"homeId": "@1.b$",
"imageSize": "800^600",
"imageType": "%20a+",
},
},
// Test real query param
{
paths: []string{"/home", "/home/{homeId}", "/about"},
path: "/home/55?foo=bar",
expectedPath: "/home/{homeId}",
expectedParams: map[string]string{
"homeId": "55",
},
expectedQueryParams: map[string][]string{
"foo": []string{"bar"},
},
},
// Test multiple real query params
{
paths: []string{"/home", "/home/{homeId}", "/about"},
path: "/home/55?foo=bar&foo=baz&qux=quux",
expectedPath: "/home/{homeId}",
expectedParams: map[string]string{
"homeId": "55",
},
expectedQueryParams: map[string][]string{
"foo": []string{"bar", "baz"},
"qux": []string{"quux"},
},
},
}
func TestRouter(t *testing.T) {
for _, tc := range routeTestCases {
gotPath := ""
gotParams := map[string]string{}
gotQueryParams := map[string][]string{}
r := New()
for _, path := range tc.paths {
handler := generateTestHandler(path, &gotPath, &gotParams, &gotQueryParams)
r.HandleFunc(path, handler)
}
r.pathChanged(tc.path, false)
if gotPath != tc.expectedPath {
t.Errorf("Failed for path=%s. Expected path: %s, Got path: %s", tc.path, tc.expectedPath, gotPath)
}
if tc.expectedParams != nil {
if !reflect.DeepEqual(gotParams, tc.expectedParams) {
t.Errorf("Failed for path=%s. Expected params: %s, Got params: %s", tc.path, tc.expectedParams, gotParams)
}
}
if tc.expectedQueryParams != nil {
if !reflect.DeepEqual(gotQueryParams, tc.expectedQueryParams) {
t.Errorf("Failed for path=%s. Expected query params: %s, Got query params: %s", tc.path, tc.expectedQueryParams, gotQueryParams)
}
}
}
}
func generateTestHandler(path string, gotPath *string, gotParams *map[string]string, gotQueryParams *map[string][]string) Handler {
return func(context *Context) {
*gotPath = path
*gotParams = context.Params
*gotQueryParams = context.QueryParams
}
}
func TestRemoveEmptyStrings(t *testing.T) {
testCases := []struct {
input []string
expected []string
}{
{
input: []string{"a", "b", "c"},
expected: []string{"a", "b", "c"},
},
{
input: []string{"a", "b", ""},
expected: []string{"a", "b"},
},
{
input: []string{"a", "", "c"},
expected: []string{"a", "c"},
},
{
input: []string{"", "b", "c"},
expected: []string{"b", "c"},
},
{
input: []string{"", "", ""},
expected: []string{},
},
}
for i, tc := range testCases {
got := removeEmptyStrings(tc.input)
if !reflect.DeepEqual(got, tc.expected) {
t.Errorf("removeEmptyStrings failed for test case %d\nExpected: %v\nBut got %v", i, tc.expected, got)
}
}
}