2020-05-13 20:21:49 +02:00
|
|
|
// ⚡️ 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 (
|
|
|
|
"testing"
|
2020-05-23 09:30:28 +02:00
|
|
|
|
|
|
|
utils "github.com/gofiber/utils"
|
|
|
|
fasthttp "github.com/valyala/fasthttp"
|
2020-05-13 20:21:49 +02:00
|
|
|
)
|
|
|
|
|
2020-05-23 09:30:28 +02:00
|
|
|
// go test -v -run=Test_Utils_ -count=3
|
2020-05-16 05:12:29 +02:00
|
|
|
|
2020-05-23 09:30:28 +02:00
|
|
|
func Test_Utils_ETag(t *testing.T) {
|
|
|
|
app := New()
|
2020-07-14 15:24:42 +08:00
|
|
|
t.Run("Not Status OK", func(t *testing.T) {
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
c.Status(201)
|
|
|
|
setETag(c, false)
|
|
|
|
utils.AssertEqual(t, "", string(c.Fasthttp.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.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Has HeaderIfNoneMatch", func(t *testing.T) {
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
c.Fasthttp.Request.Header.Set(HeaderIfNoneMatch, `"13-1831710635"`)
|
|
|
|
setETag(c, false)
|
|
|
|
utils.AssertEqual(t, 304, c.Fasthttp.Response.StatusCode())
|
|
|
|
utils.AssertEqual(t, "", string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
utils.AssertEqual(t, "", string(c.Fasthttp.Response.Body()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("No HeaderIfNoneMatch", func(t *testing.T) {
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
setETag(c, false)
|
|
|
|
utils.AssertEqual(t, `"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
})
|
2020-05-16 05:12:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:30:28 +02:00
|
|
|
// 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.Send("Hello, World!")
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
setETag(c, false)
|
|
|
|
}
|
|
|
|
utils.AssertEqual(b, `"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
2020-05-16 05:12:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:30:28 +02:00
|
|
|
func Test_Utils_ETag_Weak(t *testing.T) {
|
|
|
|
app := New()
|
2020-07-14 15:24:42 +08:00
|
|
|
t.Run("Set Weak", func(t *testing.T) {
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
setETag(c, true)
|
|
|
|
utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Match Weak ETag", func(t *testing.T) {
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
c.Fasthttp.Request.Header.Set(HeaderIfNoneMatch, `W/"13-1831710635"`)
|
|
|
|
setETag(c, true)
|
|
|
|
utils.AssertEqual(t, 304, c.Fasthttp.Response.StatusCode())
|
|
|
|
utils.AssertEqual(t, "", string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
utils.AssertEqual(t, "", string(c.Fasthttp.Response.Body()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Not Match Weak ETag", func(t *testing.T) {
|
|
|
|
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
|
|
|
defer app.ReleaseCtx(c)
|
|
|
|
c.Send("Hello, World!")
|
|
|
|
c.Fasthttp.Request.Header.Set(HeaderIfNoneMatch, `W/"13-1831710635xx"`)
|
|
|
|
setETag(c, true)
|
|
|
|
utils.AssertEqual(t, `W/"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
})
|
2020-05-16 05:12:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:30:28 +02:00
|
|
|
// 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.Send("Hello, World!")
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
setETag(c, true)
|
|
|
|
}
|
|
|
|
utils.AssertEqual(b, `W/"13-1831710635"`, string(c.Fasthttp.Response.Header.Peek(HeaderETag)))
|
|
|
|
}
|
2020-05-13 20:21:49 +02:00
|
|
|
|
|
|
|
func Test_Utils_getGroupPath(t *testing.T) {
|
2020-05-16 05:12:29 +02:00
|
|
|
t.Parallel()
|
2020-05-13 20:21:49 +02:00
|
|
|
res := getGroupPath("/v1", "/")
|
2020-05-23 09:30:28 +02:00
|
|
|
utils.AssertEqual(t, "/v1", res)
|
2020-05-13 20:21:49 +02:00
|
|
|
|
2020-05-24 10:02:21 -04:00
|
|
|
res = getGroupPath("/v1/", "/")
|
|
|
|
utils.AssertEqual(t, "/v1/", res)
|
|
|
|
|
2020-05-13 20:21:49 +02:00
|
|
|
res = getGroupPath("/v1", "/")
|
2020-05-23 09:30:28 +02:00
|
|
|
utils.AssertEqual(t, "/v1", res)
|
2020-05-13 20:21:49 +02:00
|
|
|
|
|
|
|
res = getGroupPath("/", "/")
|
2020-05-23 09:30:28 +02:00
|
|
|
utils.AssertEqual(t, "/", res)
|
2020-05-13 20:21:49 +02:00
|
|
|
|
|
|
|
res = getGroupPath("/v1/api/", "/")
|
2020-05-23 09:30:28 +02:00
|
|
|
utils.AssertEqual(t, "/v1/api/", res)
|
2020-05-13 20:21:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:30:28 +02:00
|
|
|
// 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++ {
|
2020-06-08 02:55:19 +02:00
|
|
|
_ = getGroupPath("/v1/long/path/john/doe", "/why/this/name/is/so/awesome")
|
|
|
|
_ = getGroupPath("/v1", "/")
|
|
|
|
_ = getGroupPath("/v1", "/api")
|
2020-05-23 09:30:28 +02:00
|
|
|
res = getGroupPath("/v1", "/api/register/:project")
|
|
|
|
}
|
|
|
|
utils.AssertEqual(b, "/v1/api/register/:project", res)
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:51:08 +02:00
|
|
|
func Benchmark_Utils_Unescape(b *testing.B) {
|
|
|
|
unescaped := ""
|
|
|
|
dst := make([]byte, 0)
|
|
|
|
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
source := "/cr%C3%A9er"
|
|
|
|
pathBytes := getBytes(source)
|
|
|
|
pathBytes = fasthttp.AppendUnquotedArg(dst[:0], pathBytes)
|
|
|
|
unescaped = getString(pathBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.AssertEqual(b, "/créer", unescaped)
|
|
|
|
}
|