Repository navigation
Expand file tree
/
Copy pathbytes_test.go
More file actions
107 lines (90 loc) · 3.13 KB
/
Copy pathbytes_test.go
File metadata and controls
107 lines (90 loc) · 3.13 KB
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
// ⚡️ 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 (
"testing"
"github.com/stretchr/testify/require"
)
func Test_AddTrailingSlashBytes(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
in []byte
want []byte
}{
{name: "empty", in: []byte(""), want: []byte("/")},
{name: "slash-only", in: []byte("/"), want: []byte("/")},
{name: "short-no-slash", in: []byte("abc"), want: []byte("abc/")},
{name: "short-with-slash", in: []byte("abc/"), want: []byte("abc/")},
{name: "path-no-slash", in: []byte("/api/v1/users"), want: []byte("/api/v1/users/")},
{name: "path-with-slash", in: []byte("/api/v1/users/"), want: []byte("/api/v1/users/")},
{name: "double-slash", in: []byte("abc//"), want: []byte("abc//")},
{name: "unicode", in: []byte("/日本語"), want: []byte("/日本語/")},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
result := AddTrailingSlashBytes(tc.in)
require.Equal(t, tc.want, result)
})
}
}
func Test_AddTrailingSlashBytes_NoMutation(t *testing.T) {
t.Parallel()
original := []byte("test")
originalCopy := make([]byte, len(original))
copy(originalCopy, original)
_ = AddTrailingSlashBytes(original)
require.Equal(t, originalCopy, original, "original slice should not be mutated")
}
func Test_AddTrailingSlashBytes_ReturnsSame(t *testing.T) {
t.Parallel()
input := []byte("test/")
result := AddTrailingSlashBytes(input)
require.Equal(t, input, result)
require.Same(t, &input[0], &result[0], "should return same slice instance")
}
// Test_AddTrailingSlashBytes_ResultIsOwned: an empty input once got a view of the constant "/".
func Test_AddTrailingSlashBytes_ResultIsOwned(t *testing.T) {
t.Parallel()
spare := make([]byte, 0, 8)
inputs := map[string][]byte{
"nil": nil,
"empty": {},
"empty with spare capacity": spare,
"no slash": []byte("ab"),
}
for name, in := range inputs {
want := append(append([]byte{}, in...), '/')
first, second := AddTrailingSlashBytes(in), AddTrailingSlashBytes(in)
require.Equal(t, want, first, name)
require.NotSame(t, &first[0], &second[0], "%s: two results share storage", name)
first[len(first)-1] = 'x'
require.Equal(t, want, second, "%s: writing to one result changed another", name)
require.Zero(t, spare[:1][0], "%s: the input's spare capacity was written", name)
}
}
func Benchmark_AddTrailingSlashBytes(b *testing.B) {
cases := []struct {
name string
input []byte
}{
{name: "empty", input: []byte("")},
{name: "slash-only", input: []byte("/")},
{name: "short-no-slash", input: []byte("abc")},
{name: "short-with-slash", input: []byte("abc/")},
{name: "path-no-slash", input: []byte("/api/v1/users")},
{name: "path-with-slash", input: []byte("/api/v1/users/")},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
var res []byte
for i := 0; i < b.N; i++ {
res = AddTrailingSlashBytes(tc.input)
}
_ = res
})
}
}