forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
349 lines (299 loc) · 8.89 KB
/
helpers_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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// ⚡️ 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 (
"crypto/tls"
"fmt"
"net"
"strings"
"testing"
"time"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
)
// go test -v -run=Test_Utils_ -count=3
func Test_Utils_ETag(t *testing.T) {
app := New()
t.Run("Not Status OK", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Status(201)
setETag(c, false)
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
})
t.Run("No Body", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
setETag(c, false)
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
})
t.Run("Has HeaderIfNoneMatch", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Request().Header.Set(HeaderIfNoneMatch, `"13-1831710635"`)
setETag(c, false)
utils.AssertEqual(t, 304, c.Response().StatusCode())
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
utils.AssertEqual(t, "", string(c.Response().Body()))
})
t.Run("No HeaderIfNoneMatch", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
setETag(c, false)
utils.AssertEqual(t, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
})
}
// go test -v -run=^$ -bench=Benchmark_App_ETag -benchmem -count=4
func Benchmark_Utils_ETag(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
for n := 0; n < b.N; n++ {
setETag(c, false)
}
utils.AssertEqual(b, `"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
}
// go test -v -run=Test_Utils_ETag_Weak -count=1
func Test_Utils_ETag_Weak(t *testing.T) {
app := New()
t.Run("Set Weak", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
setETag(c, true)
utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
})
t.Run("Match Weak ETag", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Request().Header.Set(HeaderIfNoneMatch, `W/"13-1831710635"`)
setETag(c, true)
utils.AssertEqual(t, 304, c.Response().StatusCode())
utils.AssertEqual(t, "", string(c.Response().Header.Peek(HeaderETag)))
utils.AssertEqual(t, "", string(c.Response().Body()))
})
t.Run("Not Match Weak ETag", func(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
c.Request().Header.Set(HeaderIfNoneMatch, `W/"13-1831710635xx"`)
setETag(c, true)
utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
})
}
func Test_Utils_UniqueRouteStack(t *testing.T) {
route1 := &Route{}
route2 := &Route{}
route3 := &Route{}
utils.AssertEqual(
t,
[]*Route{
route1,
route2,
route3,
},
uniqueRouteStack([]*Route{
route1,
route1,
route1,
route2,
route2,
route2,
route3,
route3,
route3,
route1,
route2,
route3,
}),
)
}
// go test -v -run=^$ -bench=Benchmark_App_ETag_Weak -benchmem -count=4
func Benchmark_Utils_ETag_Weak(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.SendString("Hello, World!")
for n := 0; n < b.N; n++ {
setETag(c, true)
}
utils.AssertEqual(b, `W/"13-1831710635"`, string(c.Response().Header.Peek(HeaderETag)))
}
func Test_Utils_getGroupPath(t *testing.T) {
t.Parallel()
res := getGroupPath("/v1", "/")
utils.AssertEqual(t, "/v1", res)
res = getGroupPath("/v1/", "/")
utils.AssertEqual(t, "/v1/", res)
res = getGroupPath("/v1", "/")
utils.AssertEqual(t, "/v1", res)
res = getGroupPath("/", "/")
utils.AssertEqual(t, "/", res)
res = getGroupPath("/v1/api/", "/")
utils.AssertEqual(t, "/v1/api/", res)
res = getGroupPath("/v1/api", "group")
utils.AssertEqual(t, "/v1/api/group", res)
res = getGroupPath("/v1/api", "")
utils.AssertEqual(t, "/v1/api", res)
}
// go test -v -run=^$ -bench=Benchmark_Utils_ -benchmem -count=3
func Benchmark_Utils_getGroupPath(b *testing.B) {
var res string
for n := 0; n < b.N; n++ {
_ = getGroupPath("/v1/long/path/john/doe", "/why/this/name/is/so/awesome")
_ = getGroupPath("/v1", "/")
_ = getGroupPath("/v1", "/api")
res = getGroupPath("/v1", "/api/register/:project")
}
utils.AssertEqual(b, "/v1/api/register/:project", res)
}
func Benchmark_Utils_Unescape(b *testing.B) {
unescaped := ""
dst := make([]byte, 0)
for n := 0; n < b.N; n++ {
source := "/cr%C3%A9er"
pathBytes := utils.UnsafeBytes(source)
pathBytes = fasthttp.AppendUnquotedArg(dst[:0], pathBytes)
unescaped = utils.UnsafeString(pathBytes)
}
utils.AssertEqual(b, "/créer", unescaped)
}
func Test_Utils_Parse_Address(t *testing.T) {
testCases := []struct {
addr, host, port string
}{
{"[::]:3000", "[::]", "3000"},
{"127.0.0.1:3000", "127.0.0.1", "3000"},
{"/path/to/unix/socket", "/path/to/unix/socket", ""},
}
for _, c := range testCases {
host, port := parseAddr(c.addr)
utils.AssertEqual(t, c.host, host, "addr host")
utils.AssertEqual(t, c.port, port, "addr port")
}
}
func Test_Utils_GetOffset(t *testing.T) {
utils.AssertEqual(t, "", getOffer("hello"))
utils.AssertEqual(t, "1", getOffer("", "1"))
utils.AssertEqual(t, "", getOffer("2", "1"))
}
func Test_Utils_TestAddr_Network(t *testing.T) {
var addr testAddr = "addr"
utils.AssertEqual(t, "addr", addr.Network())
}
func Test_Utils_TestConn_Deadline(t *testing.T) {
conn := &testConn{}
utils.AssertEqual(t, nil, conn.SetDeadline(time.Time{}))
utils.AssertEqual(t, nil, conn.SetReadDeadline(time.Time{}))
utils.AssertEqual(t, nil, conn.SetWriteDeadline(time.Time{}))
}
func Test_Utils_IsNoCache(t *testing.T) {
testCases := []struct {
string
bool
}{
{"public", false},
{"no-cache", true},
{"public, no-cache, max-age=30", true},
{"public,no-cache", true},
{"public,no-cacheX", false},
{"no-cache, public", true},
{"Xno-cache, public", false},
{"max-age=30, no-cache,public", true},
}
for _, c := range testCases {
ok := isNoCache(c.string)
utils.AssertEqual(t, c.bool, ok,
fmt.Sprintf("want %t, got isNoCache(%s)=%t", c.bool, c.string, ok))
}
}
// go test -v -run=^$ -bench=Benchmark_Utils_IsNoCache -benchmem -count=4
func Benchmark_Utils_IsNoCache(b *testing.B) {
var ok bool
for i := 0; i < b.N; i++ {
ok = isNoCache("public")
ok = isNoCache("no-cache")
ok = isNoCache("public, no-cache, max-age=30")
ok = isNoCache("public,no-cache")
ok = isNoCache("no-cache, public")
ok = isNoCache("max-age=30, no-cache,public")
}
utils.AssertEqual(b, true, ok)
}
func Test_Utils_lnMetadata(t *testing.T) {
t.Run("closed listen", func(t *testing.T) {
ln, err := net.Listen(NetworkTCP, ":0")
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, nil, ln.Close())
addr, config := lnMetadata(NetworkTCP, ln)
utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config == nil)
})
t.Run("non tls", func(t *testing.T) {
ln, err := net.Listen(NetworkTCP, ":0")
utils.AssertEqual(t, nil, err)
addr, config := lnMetadata(NetworkTCP4, ln)
utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config == nil)
})
t.Run("tls", func(t *testing.T) {
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
utils.AssertEqual(t, nil, err)
config := &tls.Config{Certificates: []tls.Certificate{cer}}
ln, err := net.Listen(NetworkTCP4, ":0")
utils.AssertEqual(t, nil, err)
ln = tls.NewListener(ln, config)
addr, config := lnMetadata(NetworkTCP4, ln)
utils.AssertEqual(t, ln.Addr().String(), addr)
utils.AssertEqual(t, true, config != nil)
})
}
// go test -v -run=^$ -bench=Benchmark_SlashRecognition -benchmem -count=4
func Benchmark_SlashRecognition(b *testing.B) {
search := "wtf/1234"
var result bool
b.Run("indexBytes", func(b *testing.B) {
result = false
for i := 0; i < b.N; i++ {
if strings.IndexByte(search, slashDelimiter) != -1 {
result = true
}
}
utils.AssertEqual(b, true, result)
})
b.Run("forEach", func(b *testing.B) {
result = false
c := int32(slashDelimiter)
for i := 0; i < b.N; i++ {
for _, b := range search {
if b == c {
result = true
break
}
}
}
utils.AssertEqual(b, true, result)
})
b.Run("IndexRune", func(b *testing.B) {
result = false
c := int32(slashDelimiter)
for i := 0; i < b.N; i++ {
result = IndexRune(search, c)
}
utils.AssertEqual(b, true, result)
})
}
func IndexRune(str string, needle int32) bool {
for _, b := range str {
if b == needle {
return true
}
}
return false
}