mirror of
https://github.com/zap-proto/fiber.git
synced 2026-10-30 15:06:23 +00:00
281 lines
6.1 KiB
Go
281 lines
6.1 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 (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func Test_ToUpper(t *testing.T) {
|
|
t.Parallel()
|
|
res := ToUpper("/my/name/is/:param/*")
|
|
AssertEqual(t, "/MY/NAME/IS/:PARAM/*", res)
|
|
}
|
|
|
|
const (
|
|
largeStr = "/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts/RePos/GoFiBer/FibEr/iSsues/CoMmEnts"
|
|
upperStr = "/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS/REPOS/GOFIBER/FIBER/ISSUES/COMMENTS"
|
|
lowerStr = "/repos/gofiber/fiber/issues/187643/comments/repos/gofiber/fiber/issues/comments"
|
|
)
|
|
|
|
func Benchmark_ToUpper(b *testing.B) {
|
|
var res string
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = ToUpper(largeStr)
|
|
}
|
|
AssertEqual(b, upperStr, res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.ToUpper(largeStr)
|
|
}
|
|
AssertEqual(b, upperStr, res)
|
|
})
|
|
}
|
|
|
|
func Test_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 res string
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = ToLower(largeStr)
|
|
}
|
|
AssertEqual(b, lowerStr, res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.ToLower(largeStr)
|
|
}
|
|
AssertEqual(b, lowerStr, res)
|
|
})
|
|
}
|
|
|
|
func Test_TrimRight(t *testing.T) {
|
|
t.Parallel()
|
|
res := TrimRight("/test//////", '/')
|
|
AssertEqual(t, "/test", res)
|
|
|
|
res = TrimRight("/test", '/')
|
|
AssertEqual(t, "/test", res)
|
|
|
|
res = TrimRight(" ", ' ')
|
|
AssertEqual(t, "", res)
|
|
|
|
res = TrimRight(" ", ' ')
|
|
AssertEqual(t, "", res)
|
|
|
|
res = TrimRight("", ' ')
|
|
AssertEqual(t, "", 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_TrimLeft(t *testing.T) {
|
|
t.Parallel()
|
|
res := TrimLeft("////test/", '/')
|
|
AssertEqual(t, "test/", res)
|
|
|
|
res = TrimLeft("test/", '/')
|
|
AssertEqual(t, "test/", res)
|
|
|
|
res = TrimLeft(" ", ' ')
|
|
AssertEqual(t, "", res)
|
|
|
|
res = TrimLeft(" ", ' ')
|
|
AssertEqual(t, "", res)
|
|
|
|
res = TrimLeft("", ' ')
|
|
AssertEqual(t, "", 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_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)
|
|
|
|
res = Trim(" ", ' ')
|
|
AssertEqual(t, "", res)
|
|
|
|
res = Trim(" ", ' ')
|
|
AssertEqual(t, "", res)
|
|
|
|
res = Trim("", ' ')
|
|
AssertEqual(t, "", 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)
|
|
})
|
|
}
|
|
|
|
// go test -v -run=^$ -bench=Benchmark_EqualFold -benchmem -count=4
|
|
func Benchmark_EqualFold(b *testing.B) {
|
|
var res bool
|
|
b.Run("fiber", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = EqualFold(upperStr, lowerStr)
|
|
}
|
|
AssertEqual(b, true, res)
|
|
})
|
|
b.Run("default", func(b *testing.B) {
|
|
for n := 0; n < b.N; n++ {
|
|
res = strings.EqualFold(upperStr, lowerStr)
|
|
}
|
|
AssertEqual(b, true, res)
|
|
})
|
|
}
|
|
|
|
func Test_EqualFold(t *testing.T) {
|
|
t.Parallel()
|
|
res := EqualFold("/MY/NAME/IS/:PARAM/*", "/my/name/is/:param/*")
|
|
AssertEqual(t, true, res)
|
|
res = EqualFold("/MY1/NAME/IS/:PARAM/*", "/MY1/NAME/IS/:PARAM/*")
|
|
AssertEqual(t, true, res)
|
|
res = EqualFold("/my2/name/is/:param/*", "/my2/name")
|
|
AssertEqual(t, false, res)
|
|
res = EqualFold("/dddddd", "eeeeee")
|
|
AssertEqual(t, false, res)
|
|
res = EqualFold("\na", "*A")
|
|
AssertEqual(t, false, res)
|
|
res = EqualFold("/MY3/NAME/IS/:PARAM/*", "/my3/name/is/:param/*")
|
|
AssertEqual(t, true, res)
|
|
res = EqualFold("/MY4/NAME/IS/:PARAM/*", "/my4/nAME/IS/:param/*")
|
|
AssertEqual(t, true, res)
|
|
}
|
|
|
|
func Test_AddTrailingSlash(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{
|
|
name: "already has trailing slash",
|
|
in: "path/",
|
|
want: "path/",
|
|
},
|
|
{
|
|
name: "no trailing slash",
|
|
in: "path",
|
|
want: "path/",
|
|
},
|
|
{
|
|
name: "empty string",
|
|
in: "",
|
|
want: "/",
|
|
},
|
|
{
|
|
name: "root slash",
|
|
in: "/",
|
|
want: "/",
|
|
},
|
|
{
|
|
name: "multi-level path",
|
|
in: "a/b/c",
|
|
want: "a/b/c/",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
got := AddTrailingSlash(tt.in)
|
|
if got != tt.want {
|
|
t.Fatalf("AddTrailingSlash(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Benchmark_AddTrailingSlash(b *testing.B) {
|
|
cases := map[string]string{
|
|
"AlreadyHasSlash": "example/path/",
|
|
"NoSlash": "example/path",
|
|
"Empty": "",
|
|
"LongString": strings.Repeat("a", 10_000),
|
|
"LongStringWithSlash": strings.Repeat("a", 10_000) + "/",
|
|
}
|
|
|
|
for name, input := range cases {
|
|
b.Run(name, func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
_ = AddTrailingSlash(input)
|
|
}
|
|
})
|
|
}
|
|
}
|