forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
293 lines (257 loc) · 9.38 KB
/
utils_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 📝 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"fmt"
"testing"
)
// go test -v ./... -run=Test_Utils_toLower -count=3
func Test_Utils_toUpper(t *testing.T) {
t.Parallel()
res := toUpper("/my/name/is/:param/*")
assertEqual(t, "/MY/NAME/IS/:PARAM/*", res)
}
func Test_Utils_toLower(t *testing.T) {
t.Parallel()
res := toLower("/MY/NAME/IS/:PARAM/*")
assertEqual(t, "/my/name/is/:param/*", res)
res = toLower("/MY1/NAME/IS/:PARAM/*")
assertEqual(t, "/my1/name/is/:param/*", res)
res = toLower("/MY2/NAME/IS/:PARAM/*")
assertEqual(t, "/my2/name/is/:param/*", res)
res = toLower("/MY3/NAME/IS/:PARAM/*")
assertEqual(t, "/my3/name/is/:param/*", res)
res = toLower("/MY4/NAME/IS/:PARAM/*")
assertEqual(t, "/my4/name/is/:param/*", res)
}
func Test_Utils_trimRight(t *testing.T) {
t.Parallel()
res := trimRight("/test//////", '/')
assertEqual(t, "/test", res)
}
// func Test_Utils_assertEqual(t *testing.T) {
// // TODO
// }
// func Test_Utils_setETag(t *testing.T) {
// // TODO
// }
func Test_Utils_getGroupPath(t *testing.T) {
t.Parallel()
res := getGroupPath("/v1", "/")
assertEqual(t, "/v1", res)
res = getGroupPath("/v1", "/")
assertEqual(t, "/v1", res)
res = getGroupPath("/", "/")
assertEqual(t, "/", res)
res = getGroupPath("/v1/api/", "/")
assertEqual(t, "/v1/api/", res)
}
func Test_Utils_getMIME(t *testing.T) {
t.Parallel()
res := getMIME(".json")
assertEqual(t, "application/json", res)
res = getMIME(".xml")
assertEqual(t, "application/xml", res)
res = getMIME("xml")
assertEqual(t, "application/xml", res)
res = getMIME("json")
assertEqual(t, "application/json", res)
}
// func Test_Utils_getArgument(t *testing.T) {
// // TODO
// }
// func Test_Utils_parseTokenList(t *testing.T) {
// // TODO
// }
func Test_Utils_getString(t *testing.T) {
t.Parallel()
res := getString([]byte("Hello, World!"))
assertEqual(t, "Hello, World!", res)
}
func Test_Utils_getStringImmutable(t *testing.T) {
t.Parallel()
res := getStringImmutable([]byte("Hello, World!"))
assertEqual(t, "Hello, World!", res)
}
func Test_Utils_getBytes(t *testing.T) {
t.Parallel()
res := getBytes("Hello, World!")
assertEqual(t, []byte("Hello, World!"), res)
}
func Test_Utils_getBytesImmutable(t *testing.T) {
t.Parallel()
res := getBytesImmutable("Hello, World!")
assertEqual(t, []byte("Hello, World!"), res)
}
func Test_Utils_methodINT(t *testing.T) {
t.Parallel()
res := methodINT[MethodGet]
assertEqual(t, 0, res)
res = methodINT[MethodHead]
assertEqual(t, 1, res)
res = methodINT[MethodPost]
assertEqual(t, 2, res)
res = methodINT[MethodPut]
assertEqual(t, 3, res)
res = methodINT[MethodDelete]
assertEqual(t, 4, res)
res = methodINT[MethodConnect]
assertEqual(t, 5, res)
res = methodINT[MethodOptions]
assertEqual(t, 6, res)
res = methodINT[MethodTrace]
assertEqual(t, 7, res)
res = methodINT[MethodPatch]
assertEqual(t, 8, res)
}
func Test_Utils_statusMessage(t *testing.T) {
t.Parallel()
res := statusMessage[102]
assertEqual(t, "Processing", res)
res = statusMessage[303]
assertEqual(t, "See Other", res)
res = statusMessage[404]
assertEqual(t, "Not Found", res)
res = statusMessage[507]
assertEqual(t, "Insufficient Storage", res)
}
func Test_Utils_extensionMIME(t *testing.T) {
t.Parallel()
res := getMIME(".html")
assertEqual(t, "text/html", res)
res = getMIME("html")
assertEqual(t, "text/html", res)
res = getMIME(".msp")
assertEqual(t, "application/octet-stream", res)
res = getMIME("msp")
assertEqual(t, "application/octet-stream", res)
}
// func Test_Utils_getParams(t *testing.T) {
// // TODO
// }
func Test_Utils_matchParams(t *testing.T) {
t.Parallel()
type testparams struct {
url string
params []string
match bool
}
testCase := func(r string, cases []testparams) {
parser := getParams(r)
for _, c := range cases {
params, match := parser.getMatch(c.url, false)
assertEqual(t, c.params, params, fmt.Sprintf("route: '%s', url: '%s'", r, c.url))
assertEqual(t, c.match, match, fmt.Sprintf("route: '%s', url: '%s'", r, c.url))
}
}
testCase("/api/v1/:param/*", []testparams{
{url: "/api/v1/entity", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/", params: []string{"entity", ""}, match: true},
{url: "/api/v1/entity/1", params: []string{"entity", "1"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
})
testCase("/api/v1/:param?", []testparams{
{url: "/api/v1", params: []string{""}, match: true},
{url: "/api/v1/", params: []string{""}, match: true},
{url: "/api/v1/optional", params: []string{"optional"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/xyz", params: nil, match: false},
})
testCase("/api/v1/*", []testparams{
{url: "/api/v1", params: []string{""}, match: true},
{url: "/api/v1/", params: []string{""}, match: true},
{url: "/api/v1/entity", params: []string{"entity"}, match: true},
{url: "/api/v1/entity/1/2", params: []string{"entity/1/2"}, match: true},
{url: "/api/v", params: nil, match: false},
{url: "/api/v2", params: nil, match: false},
{url: "/api/abc", params: nil, match: false},
})
testCase("/api/v1/:param", []testparams{
{url: "/api/v1/entity", params: []string{"entity"}, match: true},
{url: "/api/v1/entity/8728382", params: nil, match: false},
{url: "/api/v1", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
})
testCase("/api/v1/const", []testparams{
{url: "/api/v1/const", params: []string{}, match: true},
{url: "/api/v1", params: nil, match: false},
{url: "/api/v1/", params: nil, match: false},
{url: "/api/v1/something", params: nil, match: false},
})
testCase("/api/v1/:param/abc/*", []testparams{
{url: "/api/v1/well/abc/wildcard", params: []string{"well", "wildcard"}, match: true},
{url: "/api/v1/well/abc/", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/abc", params: []string{"well", ""}, match: true},
{url: "/api/v1/well/ttt", params: nil, match: false},
})
testCase("/api/:day/:month?/:year?", []testparams{
{url: "/api/1", params: []string{"1", "", ""}, match: true},
{url: "/api/1/", params: []string{"1", "", ""}, match: true},
{url: "/api/1/2", params: []string{"1", "2", ""}, match: true},
{url: "/api/1/2/3", params: []string{"1", "2", "3"}, match: true},
{url: "/api/", params: nil, match: false},
})
testCase("/api/*", []testparams{
{url: "/api/", params: []string{""}, match: true},
{url: "/api/joker", params: []string{"joker"}, match: true},
{url: "/api", params: []string{""}, match: true},
{url: "/api/v1/entity", params: []string{"v1/entity"}, match: true},
{url: "/api2/v1/entity", params: nil, match: false},
{url: "/api_ignore/v1/entity", params: nil, match: false},
})
testCase("/api/*/:param?", []testparams{
{url: "/api/", params: []string{"", ""}, match: true},
{url: "/api/joker", params: []string{"joker", ""}, match: true},
{url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
{url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
{url: "/api", params: []string{"", ""}, match: true},
})
testCase("/api/*/:param", []testparams{
{url: "/api/test/abc", params: []string{"test", "abc"}, match: true},
{url: "/api/joker/batman", params: []string{"joker", "batman"}, match: true},
{url: "/api/joker/batman/robin", params: []string{"joker/batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman/robin", "1"}, match: true},
{url: "/api", params: nil, match: false},
})
testCase("/api/*/:param/:param2", []testparams{
{url: "/api/test/abc", params: nil, match: false},
{url: "/api/joker/batman", params: nil, match: false},
{url: "/api/joker/batman/robin", params: []string{"joker", "batman", "robin"}, match: true},
{url: "/api/joker/batman/robin/1", params: []string{"joker/batman", "robin", "1"}, match: true},
{url: "/api/joker/batman/robin/1/2", params: []string{"joker/batman/robin", "1", "2"}, match: true},
{url: "/api", params: nil, match: false},
})
testCase("/", []testparams{
{url: "/api", params: nil, match: false},
{url: "", params: []string{}, match: true},
{url: "/", params: []string{}, match: true},
})
testCase("/config/abc.json", []testparams{
{url: "/config/abc.json", params: []string{}, match: true},
{url: "config/abc.json", params: nil, match: false},
{url: "/config/efg.json", params: nil, match: false},
{url: "/config", params: nil, match: false},
})
testCase("/config/*.json", []testparams{
{url: "/config/abc.json", params: []string{"abc.json"}, match: true},
{url: "/config/efg.json", params: []string{"efg.json"}, match: true},
//{url: "/config/efg.csv", params: nil, match: false},// doesn`t work, current: params: "efg.csv", true
{url: "config/abc.json", params: nil, match: false},
{url: "/config", params: nil, match: false},
})
testCase("/xyz", []testparams{
{url: "xyz", params: nil, match: false},
{url: "xyz/", params: nil, match: false},
})
}
// func Test_Utils_getTrimmedParam(t *testing.T) {
// // TODO
// }
// func Test_Utils_getCharPos(t *testing.T) {
// // TODO
// }