mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-23 12:24:02 +00:00
503 lines
13 KiB
Go
503 lines
13 KiB
Go
// ⚡️ 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 utils
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"fmt"
|
|
"mime"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func Test_Utils_FunctionName(t *testing.T) {
|
|
t.Parallel()
|
|
AssertEqual(t, "github.com/gofiber/fiber/v2/utils.Test_Utils_UUID", FunctionName(Test_Utils_UUID))
|
|
|
|
AssertEqual(t, "github.com/gofiber/fiber/v2/utils.Test_Utils_FunctionName.func1", FunctionName(func() {}))
|
|
|
|
var dummyint = 20
|
|
AssertEqual(t, "int", FunctionName(dummyint))
|
|
}
|
|
|
|
func Test_Utils_UUID(t *testing.T) {
|
|
t.Parallel()
|
|
res := UUID()
|
|
AssertEqual(t, 36, len(res))
|
|
}
|
|
|
|
// go test -v -run=^$ -bench=Benchmark_UUID -benchmem -count=2
|
|
|
|
func Benchmark_UUID(b *testing.B) {
|
|
var res string
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = UUID()
|
|
}
|
|
AssertEqual(b, 36, len(res))
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
rnd := make([]byte, 16)
|
|
_, _ = rand.Read(rnd)
|
|
for n := 0; n < b.N; n++ {
|
|
res = fmt.Sprintf("%x-%x-%x-%x-%x", rnd[0:4], rnd[4:6], rnd[6:8], rnd[8:10], rnd[10:])
|
|
}
|
|
AssertEqual(b, 36, len(res))
|
|
})
|
|
}
|
|
|
|
func Test_Utils_ToUpper(t *testing.T) {
|
|
t.Parallel()
|
|
res := ToUpper("/my/name/is/:param/*")
|
|
AssertEqual(t, "/MY/NAME/IS/:PARAM/*", res)
|
|
}
|
|
|
|
func Benchmark_ToUpper(b *testing.B) {
|
|
var path = "/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts"
|
|
var res string
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = ToUpper(path)
|
|
}
|
|
AssertEqual(b, "/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS", res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.ToUpper(path)
|
|
}
|
|
AssertEqual(b, "/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS", 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 Benchmark_ToLower(b *testing.B) {
|
|
var path = "/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts"
|
|
var res string
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = ToLower(path)
|
|
}
|
|
AssertEqual(b, "/repos/gofiber/fiber/issues/187643/comments", res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.ToLower(path)
|
|
}
|
|
AssertEqual(b, "/repos/gofiber/fiber/issues/187643/comments", res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_ToLowerBytes(t *testing.T) {
|
|
t.Parallel()
|
|
res := ToLowerBytes([]byte("/MY/NAME/IS/:PARAM/*"))
|
|
AssertEqual(t, true, bytes.Equal([]byte("/my/name/is/:param/*"), res))
|
|
res = ToLowerBytes([]byte("/MY1/NAME/IS/:PARAM/*"))
|
|
AssertEqual(t, true, bytes.Equal([]byte("/my1/name/is/:param/*"), res))
|
|
res = ToLowerBytes([]byte("/MY2/NAME/IS/:PARAM/*"))
|
|
AssertEqual(t, true, bytes.Equal([]byte("/my2/name/is/:param/*"), res))
|
|
res = ToLowerBytes([]byte("/MY3/NAME/IS/:PARAM/*"))
|
|
AssertEqual(t, true, bytes.Equal([]byte("/my3/name/is/:param/*"), res))
|
|
res = ToLowerBytes([]byte("/MY4/NAME/IS/:PARAM/*"))
|
|
AssertEqual(t, true, bytes.Equal([]byte("/my4/name/is/:param/*"), res))
|
|
}
|
|
|
|
func Benchmark_ToLowerBytes(b *testing.B) {
|
|
var path = []byte("/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts")
|
|
var res []byte
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = ToLowerBytes(path)
|
|
}
|
|
AssertEqual(b, bytes.EqualFold(GetBytes("/repos/gofiber/fiber/issues/187643/comments"), res), true)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = bytes.ToLower(path)
|
|
}
|
|
AssertEqual(b, bytes.EqualFold(GetBytes("/repos/gofiber/fiber/issues/187643/comments"), res), true)
|
|
})
|
|
}
|
|
|
|
func Benchmark_EqualFolds(b *testing.B) {
|
|
var left = []byte("/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts")
|
|
var right = []byte("/RePos/goFiber/Fiber/issues/187643/COMMENTS")
|
|
var res bool
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = EqualsFold(left, right)
|
|
}
|
|
AssertEqual(b, true, res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = bytes.EqualFold(left, right)
|
|
}
|
|
AssertEqual(b, true, res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_EqualsFold(t *testing.T) {
|
|
t.Parallel()
|
|
res := EqualsFold([]byte("/MY/NAME/IS/:PARAM/*"), []byte("/my/name/is/:param/*"))
|
|
AssertEqual(t, true, res)
|
|
res = EqualsFold([]byte("/MY1/NAME/IS/:PARAM/*"), []byte("/MY1/NAME/IS/:PARAM/*"))
|
|
AssertEqual(t, true, res)
|
|
res = EqualsFold([]byte("/my2/name/is/:param/*"), []byte("/my2/name"))
|
|
AssertEqual(t, false, res)
|
|
res = EqualsFold([]byte("/dddddd"), []byte("eeeeee"))
|
|
AssertEqual(t, false, res)
|
|
res = EqualsFold([]byte("/MY3/NAME/IS/:PARAM/*"), []byte("/my3/name/is/:param/*"))
|
|
AssertEqual(t, true, res)
|
|
res = EqualsFold([]byte("/MY4/NAME/IS/:PARAM/*"), []byte("/my4/nAME/IS/:param/*"))
|
|
AssertEqual(t, true, res)
|
|
}
|
|
|
|
func Test_Utils_TrimRight(t *testing.T) {
|
|
t.Parallel()
|
|
res := TrimRight("/test//////", '/')
|
|
AssertEqual(t, "/test", res)
|
|
|
|
res = TrimRight("/test", '/')
|
|
AssertEqual(t, "/test", res)
|
|
}
|
|
func Benchmark_TrimRight(b *testing.B) {
|
|
var res string
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = TrimRight("foobar ", ' ')
|
|
}
|
|
AssertEqual(b, "foobar", res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.TrimRight("foobar ", " ")
|
|
}
|
|
AssertEqual(b, "foobar", res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_TrimLeft(t *testing.T) {
|
|
t.Parallel()
|
|
res := TrimLeft("////test/", '/')
|
|
AssertEqual(t, "test/", res)
|
|
|
|
res = TrimLeft("test/", '/')
|
|
AssertEqual(t, "test/", res)
|
|
}
|
|
func Benchmark_TrimLeft(b *testing.B) {
|
|
var res string
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = TrimLeft(" foobar", ' ')
|
|
}
|
|
AssertEqual(b, "foobar", res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.TrimLeft(" foobar", " ")
|
|
}
|
|
AssertEqual(b, "foobar", res)
|
|
})
|
|
}
|
|
func Test_Utils_Trim(t *testing.T) {
|
|
t.Parallel()
|
|
res := Trim(" test ", ' ')
|
|
AssertEqual(t, "test", res)
|
|
|
|
res = Trim("test", ' ')
|
|
AssertEqual(t, "test", res)
|
|
|
|
res = Trim(".test", '.')
|
|
AssertEqual(t, "test", res)
|
|
}
|
|
|
|
func Benchmark_Trim(b *testing.B) {
|
|
var res string
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = Trim(" foobar ", ' ')
|
|
}
|
|
AssertEqual(b, "foobar", res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.Trim(" foobar ", " ")
|
|
}
|
|
AssertEqual(b, "foobar", res)
|
|
})
|
|
b.Run("default.trimspace", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.TrimSpace(" foobar ")
|
|
}
|
|
AssertEqual(b, "foobar", res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_TrimRightBytes(t *testing.T) {
|
|
t.Parallel()
|
|
res := TrimRightBytes([]byte("/test//////"), '/')
|
|
AssertEqual(t, []byte("/test"), res)
|
|
|
|
res = TrimRightBytes([]byte("/test"), '/')
|
|
AssertEqual(t, []byte("/test"), res)
|
|
}
|
|
|
|
func Benchmark_TrimRightBytes(b *testing.B) {
|
|
var res []byte
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = TrimRightBytes([]byte("foobar "), ' ')
|
|
}
|
|
AssertEqual(b, []byte("foobar"), res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = bytes.TrimRight([]byte("foobar "), " ")
|
|
}
|
|
AssertEqual(b, []byte("foobar"), res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_TrimLeftBytes(t *testing.T) {
|
|
t.Parallel()
|
|
res := TrimLeftBytes([]byte("////test/"), '/')
|
|
AssertEqual(t, []byte("test/"), res)
|
|
|
|
res = TrimLeftBytes([]byte("test/"), '/')
|
|
AssertEqual(t, []byte("test/"), res)
|
|
}
|
|
func Benchmark_TrimLeftBytes(b *testing.B) {
|
|
var res []byte
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = TrimLeftBytes([]byte(" foobar"), ' ')
|
|
}
|
|
AssertEqual(b, []byte("foobar"), res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = bytes.TrimLeft([]byte(" foobar"), " ")
|
|
}
|
|
AssertEqual(b, []byte("foobar"), res)
|
|
})
|
|
}
|
|
func Test_Utils_TrimBytes(t *testing.T) {
|
|
t.Parallel()
|
|
res := TrimBytes([]byte(" test "), ' ')
|
|
AssertEqual(t, []byte("test"), res)
|
|
|
|
res = TrimBytes([]byte("test"), ' ')
|
|
AssertEqual(t, []byte("test"), res)
|
|
|
|
res = TrimBytes([]byte(".test"), '.')
|
|
AssertEqual(t, []byte("test"), res)
|
|
}
|
|
func Benchmark_TrimBytes(b *testing.B) {
|
|
var res []byte
|
|
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = TrimBytes([]byte(" foobar "), ' ')
|
|
}
|
|
AssertEqual(b, []byte("foobar"), res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = bytes.Trim([]byte(" foobar "), " ")
|
|
}
|
|
AssertEqual(b, []byte("foobar"), res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_GetCharPos(t *testing.T) {
|
|
t.Parallel()
|
|
res := GetCharPos("/foo/bar/foobar/test", '/', 3)
|
|
AssertEqual(t, 8, res)
|
|
res = GetCharPos("foo/bar/foobar/test", '/', 3)
|
|
AssertEqual(t, 14, res)
|
|
res = GetCharPos("foo/bar/foobar/test", '/', 1)
|
|
AssertEqual(t, 3, res)
|
|
res = GetCharPos("foo/bar/foobar/test", 'f', 2)
|
|
AssertEqual(t, 8, res)
|
|
res = GetCharPos("foo/bar/foobar/test", 'f', 0)
|
|
AssertEqual(t, 0, res)
|
|
}
|
|
|
|
func Test_Utils_GetTrimmedParam(t *testing.T) {
|
|
t.Parallel()
|
|
res := GetTrimmedParam("*")
|
|
AssertEqual(t, "*", res)
|
|
res = GetTrimmedParam(":param")
|
|
AssertEqual(t, "param", res)
|
|
res = GetTrimmedParam(":param1?")
|
|
AssertEqual(t, "param1", res)
|
|
res = GetTrimmedParam("noParam")
|
|
AssertEqual(t, "noParam", res)
|
|
}
|
|
|
|
func Test_Utils_GetString(t *testing.T) {
|
|
t.Parallel()
|
|
res := GetString([]byte("Hello, World!"))
|
|
AssertEqual(t, "Hello, World!", res)
|
|
}
|
|
|
|
// go test -v -run=^$ -bench=GetString -benchmem -count=2
|
|
|
|
func Benchmark_GetString(b *testing.B) {
|
|
var hello = []byte("Hello, World!")
|
|
var res string
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = GetString(hello)
|
|
}
|
|
AssertEqual(b, "Hello, World!", res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = string(hello)
|
|
}
|
|
AssertEqual(b, "Hello, World!", res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_GetBytes(t *testing.T) {
|
|
t.Parallel()
|
|
res := GetBytes("Hello, World!")
|
|
AssertEqual(t, []byte("Hello, World!"), res)
|
|
}
|
|
|
|
// go test -v -run=^$ -bench=GetBytes -benchmem -count=4
|
|
|
|
func Benchmark_GetBytes(b *testing.B) {
|
|
var hello = "Hello, World!"
|
|
var res []byte
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = GetBytes(hello)
|
|
}
|
|
AssertEqual(b, []byte("Hello, World!"), res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = []byte(hello)
|
|
}
|
|
AssertEqual(b, []byte("Hello, World!"), res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_ImmutableString(t *testing.T) {
|
|
t.Parallel()
|
|
res := ImmutableString("Hello, World!")
|
|
AssertEqual(t, "Hello, World!", 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("unknown")
|
|
AssertEqual(t, MIMEOctetStream, res)
|
|
// empty case
|
|
res = GetMIME("")
|
|
AssertEqual(t, "", res)
|
|
}
|
|
|
|
// go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2
|
|
func Benchmark_GetMIME(b *testing.B) {
|
|
var res string
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = GetMIME(".xml")
|
|
res = GetMIME(".txt")
|
|
res = GetMIME(".png")
|
|
res = GetMIME(".exe")
|
|
res = GetMIME(".json")
|
|
}
|
|
AssertEqual(b, "application/json", res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = mime.TypeByExtension(".xml")
|
|
res = mime.TypeByExtension(".txt")
|
|
res = mime.TypeByExtension(".png")
|
|
res = mime.TypeByExtension(".exe")
|
|
res = mime.TypeByExtension(".json")
|
|
}
|
|
AssertEqual(b, "application/json", res)
|
|
})
|
|
}
|
|
|
|
func Test_Utils_StatusMessage(t *testing.T) {
|
|
t.Parallel()
|
|
res := StatusMessage(204)
|
|
AssertEqual(t, "No Content", res)
|
|
|
|
res = StatusMessage(404)
|
|
AssertEqual(t, "Not Found", res)
|
|
|
|
res = StatusMessage(426)
|
|
AssertEqual(t, "Upgrade Required", res)
|
|
|
|
res = StatusMessage(511)
|
|
AssertEqual(t, "Network Authentication Required", res)
|
|
|
|
res = StatusMessage(1337)
|
|
AssertEqual(t, "", res)
|
|
|
|
res = StatusMessage(-1)
|
|
AssertEqual(t, "", res)
|
|
|
|
res = StatusMessage(0)
|
|
AssertEqual(t, "", res)
|
|
|
|
res = StatusMessage(600)
|
|
AssertEqual(t, "", res)
|
|
}
|
|
|
|
// go test -v -run=^$ -bench=Benchmark_StatusMessage -benchmem -count=4
|
|
func Benchmark_StatusMessage(b *testing.B) {
|
|
var res string
|
|
for n := 0; n < b.N; n++ {
|
|
res = StatusMessage(http.StatusNotExtended)
|
|
}
|
|
AssertEqual(b, "Not Extended", res)
|
|
}
|
|
|
|
func Test_Utils_AssertEqual(t *testing.T) {
|
|
t.Parallel()
|
|
AssertEqual(nil, []string{}, []string{})
|
|
AssertEqual(t, []string{}, []string{})
|
|
}
|