| 1 |
// ⚡️ Fiber is an Express-inspired web framework written in Go with ☕️ |
| 2 |
// 🤖 Github Repository: https://github.com/gofiber/fiber |
| 3 |
// 📌 API Documentation: https://docs.gofiber.io |
| 4 |
|
| 5 |
package utils |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
) |
| 12 |
|
| 13 |
func Test_AddTrailingSlashString(t *testing.T) { |
| 14 |
t.Parallel() |
| 15 |
|
| 16 |
testCases := []struct { |
| 17 |
name string |
| 18 |
in string |
| 19 |
want string |
| 20 |
}{ |
| 21 |
{name: "empty", in: "", want: "/"}, |
| 22 |
{name: "slash-only", in: "/", want: "/"}, |
| 23 |
{name: "short-no-slash", in: "abc", want: "abc/"}, |
| 24 |
{name: "short-with-slash", in: "abc/", want: "abc/"}, |
| 25 |
{name: "path-no-slash", in: "/api/v1/users", want: "/api/v1/users/"}, |
| 26 |
{name: "path-with-slash", in: "/api/v1/users/", want: "/api/v1/users/"}, |
| 27 |
{name: "double-slash", in: "abc//", want: "abc//"}, |
| 28 |
{name: "unicode", in: "/日本語", want: "/日本語/"}, |
| 29 |
} |
| 30 |
|
| 31 |
for _, tc := range testCases { |
| 32 |
t.Run(tc.name, func(t *testing.T) { |
| 33 |
t.Parallel() |
| 34 |
result := AddTrailingSlashString(tc.in) |
| 35 |
require.Equal(t, tc.want, result) |
| 36 |
}) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func Benchmark_AddTrailingSlashString(b *testing.B) { |
| 41 |
cases := []struct { |
| 42 |
name string |
| 43 |
input string |
| 44 |
}{ |
| 45 |
{name: "empty", input: ""}, |
| 46 |
{name: "slash-only", input: "/"}, |
| 47 |
{name: "short-no-slash", input: "abc"}, |
| 48 |
{name: "short-with-slash", input: "abc/"}, |
| 49 |
{name: "path-no-slash", input: "/api/v1/users"}, |
| 50 |
{name: "path-with-slash", input: "/api/v1/users/"}, |
| 51 |
} |
| 52 |
|
| 53 |
for _, tc := range cases { |
| 54 |
b.Run(tc.name, func(b *testing.B) { |
| 55 |
b.ReportAllocs() |
| 56 |
var res string |
| 57 |
for i := 0; i < b.N; i++ { |
| 58 |
res = AddTrailingSlashString(tc.input) |
| 59 |
} |
| 60 |
_ = res |
| 61 |
}) |
| 62 |
} |
| 63 |
} |