File: bytes_test.go

package info (click to toggle)
golang-github-gofiber-utils 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 432 kB
  • sloc: makefile: 40
file content (84 lines) | stat: -rw-r--r-- 2,338 bytes parent folder | download
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
// ⚡️ 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")
}

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
		})
	}
}