| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
upperStr = "/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS/REPOS/GOFIBER/FIBER/ISSUES/COMMENTS" |
| 13 |
lowerStr = "/repos/gofiber/fiber/issues/187643/comments/repos/gofiber/fiber/issues/comments" |
| 14 |
) |
| 15 |
|
| 16 |
// Shared test cases for TrimSpace benchmarks |
| 17 |
var trimSpaceBenchmarkCases = []struct { |
| 18 |
name string |
| 19 |
input string |
| 20 |
}{ |
| 21 |
{name: "empty", input: ""}, |
| 22 |
{name: "spaces", input: " "}, |
| 23 |
{name: "ascii-word", input: " fiber "}, |
| 24 |
{name: "auth-header-bearer", input: " Bearer abc.def.ghi "}, |
| 25 |
{name: "auth-header-basic", input: "\tBasic QWxhZGRpbjpvcGVuIHNlc2FtZQ== "}, |
| 26 |
{name: "accept-encoding", input: " gzip, deflate, br "}, |
| 27 |
{name: "content-type", input: " application/json "}, |
| 28 |
{name: "x-forwarded-for", input: " 203.0.113.5, 198.51.100.7 "}, |
| 29 |
{name: "query-params", input: " user=alice&id=42 "}, |
| 30 |
{name: "ascii-long", input: " " + strings.Repeat("fiber utils ", 8) + " "}, |
| 31 |
{name: "no-trim", input: "fiber"}, |
| 32 |
{name: "mixed-whitespace", input: "\n\t fiber utils \r\n"}, |
| 33 |
} |
| 34 |
|
| 35 |
func Test_EqualFold(t *testing.T) { |
| 36 |
t.Parallel() |
| 37 |
testCases := []struct { |
| 38 |
Expected bool |
| 39 |
S1 string |
| 40 |
S2 string |
| 41 |
}{ |
| 42 |
{Expected: true, S1: "/MY/NAME/IS/:PARAM/*", S2: "/my/name/is/:param/*"}, |
| 43 |
{Expected: true, S1: "/MY/NAME/IS/:PARAM/*", S2: "/my/name/is/:param/*"}, |
| 44 |
{Expected: true, S1: "/MY1/NAME/IS/:PARAM/*", S2: "/MY1/NAME/IS/:PARAM/*"}, |
| 45 |
{Expected: false, S1: "/my2/name/is/:param/*", S2: "/my2/name"}, |
| 46 |
{Expected: false, S1: "/dddddd", S2: "eeeeee"}, |
| 47 |
{Expected: false, S1: "\na", S2: "*A"}, |
| 48 |
{Expected: true, S1: "/MY3/NAME/IS/:PARAM/*", S2: "/my3/name/is/:param/*"}, |
| 49 |
{Expected: true, S1: "/MY4/NAME/IS/:PARAM/*", S2: "/my4/nAME/IS/:param/*"}, |
| 50 |
} |
| 51 |
|
| 52 |
for _, tc := range testCases { |
| 53 |
res := EqualFold(tc.S1, tc.S2) |
| 54 |
require.Equal(t, tc.Expected, res, "string") |
| 55 |
|
| 56 |
res = EqualFold([]byte(tc.S1), []byte(tc.S2)) |
| 57 |
require.Equal(t, tc.Expected, res, "bytes") |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func Benchmark_EqualFoldBytes(b *testing.B) { |
| 62 |
left := []byte(upperStr) |
| 63 |
right := []byte(lowerStr) |
| 64 |
var res bool |
| 65 |
b.Run("fiber", func(b *testing.B) { |
| 66 |
b.ReportAllocs() |
| 67 |
for i := 0; i < b.N; i++ { |
| 68 |
res = EqualFold(left, right) |
| 69 |
} |
| 70 |
require.True(b, res) |
| 71 |
}) |
| 72 |
b.Run("default", func(b *testing.B) { |
| 73 |
b.ReportAllocs() |
| 74 |
for i := 0; i < b.N; i++ { |
| 75 |
res = bytes.EqualFold(left, right) |
| 76 |
} |
| 77 |
require.True(b, res) |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
// go test -v -run=^$ -bench=Benchmark_EqualFold -benchmem -count=4 ./utils/ |
| 82 |
func Benchmark_EqualFold(b *testing.B) { |
| 83 |
var res bool |
| 84 |
b.Run("fiber", func(b *testing.B) { |
| 85 |
b.ReportAllocs() |
| 86 |
for i := 0; i < b.N; i++ { |
| 87 |
res = EqualFold(upperStr, lowerStr) |
| 88 |
} |
| 89 |
require.True(b, res) |
| 90 |
}) |
| 91 |
b.Run("default", func(b *testing.B) { |
| 92 |
b.ReportAllocs() |
| 93 |
for i := 0; i < b.N; i++ { |
| 94 |
res = strings.EqualFold(upperStr, lowerStr) |
| 95 |
} |
| 96 |
require.True(b, res) |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
func Test_TrimRight(t *testing.T) { |
| 101 |
t.Parallel() |
| 102 |
testCases := []struct { |
| 103 |
S1 string |
| 104 |
S2 string |
| 105 |
Cutset byte |
| 106 |
}{ |
| 107 |
{S1: "/test//////", S2: "/test", Cutset: '/'}, |
| 108 |
{S1: "/test", S2: "/test", Cutset: '/'}, |
| 109 |
{S1: " ", S2: "", Cutset: ' '}, |
| 110 |
{S1: " ", S2: "", Cutset: ' '}, |
| 111 |
{S1: "", S2: "", Cutset: ' '}, |
| 112 |
} |
| 113 |
|
| 114 |
for _, tc := range testCases { |
| 115 |
res := TrimRight(tc.S1, tc.Cutset) |
| 116 |
require.Equal(t, tc.S2, res, "string") |
| 117 |
|
| 118 |
resB := TrimRight([]byte(tc.S1), tc.Cutset) |
| 119 |
require.Equal(t, []byte(tc.S2), resB, "bytes") |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func Benchmark_TrimRight(b *testing.B) { |
| 124 |
var res string |
| 125 |
word := "foobar " |
| 126 |
expected := "foobar" |
| 127 |
|
| 128 |
b.Run("fiber", func(b *testing.B) { |
| 129 |
b.ReportAllocs() |
| 130 |
for i := 0; i < b.N; i++ { |
| 131 |
res = TrimRight(word, ' ') |
| 132 |
} |
| 133 |
require.Equal(b, expected, res) |
| 134 |
}) |
| 135 |
b.Run("default", func(b *testing.B) { |
| 136 |
b.ReportAllocs() |
| 137 |
for i := 0; i < b.N; i++ { |
| 138 |
res = strings.TrimRight(word, " ") |
| 139 |
} |
| 140 |
require.Equal(b, expected, res) |
| 141 |
}) |
| 142 |
} |
| 143 |
|
| 144 |
func Benchmark_TrimRightBytes(b *testing.B) { |
| 145 |
var res []byte |
| 146 |
word := []byte("foobar ") |
| 147 |
expected := []byte("foobar") |
| 148 |
|
| 149 |
b.Run("fiber", func(b *testing.B) { |
| 150 |
b.ReportAllocs() |
| 151 |
for i := 0; i < b.N; i++ { |
| 152 |
res = TrimRight(word, ' ') |
| 153 |
} |
| 154 |
require.Equal(b, expected, res) |
| 155 |
}) |
| 156 |
b.Run("default", func(b *testing.B) { |
| 157 |
b.ReportAllocs() |
| 158 |
for i := 0; i < b.N; i++ { |
| 159 |
res = bytes.TrimRight(word, " ") |
| 160 |
} |
| 161 |
require.Equal(b, expected, res) |
| 162 |
}) |
| 163 |
} |
| 164 |
|
| 165 |
func Test_TrimLeft(t *testing.T) { |
| 166 |
t.Parallel() |
| 167 |
testCases := []struct { |
| 168 |
S1 string |
| 169 |
S2 string |
| 170 |
Cutset byte |
| 171 |
}{ |
| 172 |
{S1: "////test/", S2: "test/", Cutset: '/'}, |
| 173 |
{S1: "test/", S2: "test/", Cutset: '/'}, |
| 174 |
{S1: " ", S2: "", Cutset: ' '}, |
| 175 |
{S1: " ", S2: "", Cutset: ' '}, |
| 176 |
{S1: "", S2: "", Cutset: ' '}, |
| 177 |
} |
| 178 |
|
| 179 |
for _, tc := range testCases { |
| 180 |
res := TrimLeft(tc.S1, tc.Cutset) |
| 181 |
require.Equal(t, tc.S2, res, "string") |
| 182 |
|
| 183 |
resB := TrimLeft([]byte(tc.S1), tc.Cutset) |
| 184 |
require.Equal(t, []byte(tc.S2), resB, "bytes") |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
func Benchmark_TrimLeft(b *testing.B) { |
| 189 |
var res string |
| 190 |
word := " foobar" |
| 191 |
expected := "foobar" |
| 192 |
|
| 193 |
b.Run("fiber", func(b *testing.B) { |
| 194 |
b.ReportAllocs() |
| 195 |
for i := 0; i < b.N; i++ { |
| 196 |
res = TrimLeft(word, ' ') |
| 197 |
} |
| 198 |
require.Equal(b, expected, res) |
| 199 |
}) |
| 200 |
b.Run("default", func(b *testing.B) { |
| 201 |
b.ReportAllocs() |
| 202 |
for i := 0; i < b.N; i++ { |
| 203 |
res = strings.TrimLeft(word, " ") |
| 204 |
} |
| 205 |
require.Equal(b, expected, res) |
| 206 |
}) |
| 207 |
} |
| 208 |
|
| 209 |
func Benchmark_TrimLeftBytes(b *testing.B) { |
| 210 |
var res []byte |
| 211 |
word := []byte(" foobar") |
| 212 |
expected := []byte("foobar") |
| 213 |
|
| 214 |
b.Run("fiber", func(b *testing.B) { |
| 215 |
b.ReportAllocs() |
| 216 |
for i := 0; i < b.N; i++ { |
| 217 |
res = TrimLeft(word, ' ') |
| 218 |
} |
| 219 |
require.Equal(b, expected, res) |
| 220 |
}) |
| 221 |
b.Run("default", func(b *testing.B) { |
| 222 |
b.ReportAllocs() |
| 223 |
for i := 0; i < b.N; i++ { |
| 224 |
res = bytes.TrimLeft(word, " ") |
| 225 |
} |
| 226 |
require.Equal(b, expected, res) |
| 227 |
}) |
| 228 |
} |
| 229 |
|
| 230 |
func Test_Trim(t *testing.T) { |
| 231 |
t.Parallel() |
| 232 |
testCases := []struct { |
| 233 |
S1 string |
| 234 |
S2 string |
| 235 |
Cutset byte |
| 236 |
}{ |
| 237 |
{S1: " test ", S2: "test", Cutset: ' '}, |
| 238 |
{S1: "test", S2: "test", Cutset: ' '}, |
| 239 |
{S1: ".test", S2: "test", Cutset: '.'}, |
| 240 |
{S1: " ", S2: "", Cutset: ' '}, |
| 241 |
{S1: " ", S2: "", Cutset: ' '}, |
| 242 |
{S1: "", S2: "", Cutset: ' '}, |
| 243 |
} |
| 244 |
|
| 245 |
for _, tc := range testCases { |
| 246 |
res := Trim(tc.S1, tc.Cutset) |
| 247 |
require.Equal(t, tc.S2, res, "string") |
| 248 |
|
| 249 |
resB := Trim([]byte(tc.S1), tc.Cutset) |
| 250 |
require.Equal(t, []byte(tc.S2), resB, "bytes") |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
func Benchmark_Trim(b *testing.B) { |
| 255 |
var res string |
| 256 |
word := " foobar " |
| 257 |
expected := "foobar" |
| 258 |
|
| 259 |
b.Run("fiber", func(b *testing.B) { |
| 260 |
b.ReportAllocs() |
| 261 |
for i := 0; i < b.N; i++ { |
| 262 |
res = Trim(word, ' ') |
| 263 |
} |
| 264 |
require.Equal(b, expected, res) |
| 265 |
}) |
| 266 |
b.Run("default", func(b *testing.B) { |
| 267 |
b.ReportAllocs() |
| 268 |
for i := 0; i < b.N; i++ { |
| 269 |
res = strings.Trim(word, " ") |
| 270 |
} |
| 271 |
require.Equal(b, expected, res) |
| 272 |
}) |
| 273 |
b.Run("default.trimspace", func(b *testing.B) { |
| 274 |
b.ReportAllocs() |
| 275 |
for i := 0; i < b.N; i++ { |
| 276 |
res = strings.TrimSpace(word) |
| 277 |
} |
| 278 |
require.Equal(b, expected, res) |
| 279 |
}) |
| 280 |
} |
| 281 |
|
| 282 |
func Benchmark_TrimBytes(b *testing.B) { |
| 283 |
var res []byte |
| 284 |
word := []byte(" foobar ") |
| 285 |
expected := []byte("foobar") |
| 286 |
|
| 287 |
b.Run("fiber", func(b *testing.B) { |
| 288 |
b.ReportAllocs() |
| 289 |
for i := 0; i < b.N; i++ { |
| 290 |
res = Trim(word, ' ') |
| 291 |
} |
| 292 |
require.Equal(b, expected, res) |
| 293 |
}) |
| 294 |
b.Run("default", func(b *testing.B) { |
| 295 |
b.ReportAllocs() |
| 296 |
for i := 0; i < b.N; i++ { |
| 297 |
res = bytes.Trim(word, " ") |
| 298 |
} |
| 299 |
require.Equal(b, expected, res) |
| 300 |
}) |
| 301 |
b.Run("default.trimspace", func(b *testing.B) { |
| 302 |
b.ReportAllocs() |
| 303 |
for i := 0; i < b.N; i++ { |
| 304 |
res = bytes.TrimSpace(word) |
| 305 |
} |
| 306 |
require.Equal(b, expected, res) |
| 307 |
}) |
| 308 |
} |
| 309 |
|
| 310 |
func Test_Trim_Edge(t *testing.T) { |
| 311 |
t.Parallel() |
| 312 |
|
| 313 |
cases := []struct { |
| 314 |
input string |
| 315 |
cut byte |
| 316 |
exp string |
| 317 |
}{ |
| 318 |
{"foobar", 'x', "foobar"}, |
| 319 |
{"", ' ', ""}, |
| 320 |
{"xxfoobarxx", 'x', "foobar"}, |
| 321 |
} |
| 322 |
for _, c := range cases { |
| 323 |
t.Run(c.input, func(t *testing.T) { |
| 324 |
t.Parallel() |
| 325 |
require.Equal(t, c.exp, Trim(c.input, c.cut)) |
| 326 |
require.Equal(t, []byte(c.exp), Trim([]byte(c.input), c.cut)) |
| 327 |
}) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func Test_TrimSpace(t *testing.T) { |
| 332 |
t.Parallel() |
| 333 |
testCases := []struct { |
| 334 |
name string |
| 335 |
input string |
| 336 |
}{ |
| 337 |
{name: "empty", input: ""}, |
| 338 |
{name: "spaces", input: " "}, |
| 339 |
{name: "tabs", input: "\t\t"}, |
| 340 |
{name: "ascii-word", input: " fiber "}, |
| 341 |
{name: "auth-header-bearer", input: " Bearer abc.def.ghi "}, |
| 342 |
{name: "auth-header-basic", input: "\tBasic QWxhZGRpbjpvcGVuIHNlc2FtZQ== "}, |
| 343 |
{name: "accept-encoding", input: " gzip, deflate, br "}, |
| 344 |
{name: "content-type-json", input: " application/json "}, |
| 345 |
{name: "x-forwarded-for", input: " 203.0.113.5, 198.51.100.7 "}, |
| 346 |
{name: "query-params", input: " user=alice&id=42 "}, |
| 347 |
{name: "ascii-long", input: " " + strings.Repeat("fiber utils ", 8) + " "}, |
| 348 |
{name: "mixed-whitespace", input: "\n\t fiber utils \r\n"}, |
| 349 |
{name: "no-trim", input: "fiber"}, |
| 350 |
{name: "utf8-valid", input: " Hello, 世界! "}, |
| 351 |
} |
| 352 |
|
| 353 |
for _, tc := range testCases { |
| 354 |
t.Run(tc.name, func(t *testing.T) { |
| 355 |
t.Parallel() |
| 356 |
// Test string variant - must match strings.TrimSpace |
| 357 |
result := TrimSpace(tc.input) |
| 358 |
stdResult := strings.TrimSpace(tc.input) |
| 359 |
require.Equal(t, stdResult, result, "TrimSpace should match strings.TrimSpace for %s", tc.name) |
| 360 |
|
| 361 |
// Test []byte variant - must match bytes.TrimSpace |
| 362 |
resultBytes := TrimSpace([]byte(tc.input)) |
| 363 |
stdResultBytes := bytes.TrimSpace([]byte(tc.input)) |
| 364 |
// bytes.Equal treats nil and empty slices as equal |
| 365 |
require.True(t, bytes.Equal(stdResultBytes, resultBytes), "TrimSpace should match bytes.TrimSpace for %s", tc.name) |
| 366 |
}) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
func Benchmark_TrimSpace(b *testing.B) { |
| 371 |
for _, tc := range trimSpaceBenchmarkCases { |
| 372 |
b.Run("fiber/"+tc.name, func(b *testing.B) { |
| 373 |
b.ReportAllocs() |
| 374 |
b.SetBytes(int64(len(tc.input))) |
| 375 |
b.ResetTimer() |
| 376 |
var res string |
| 377 |
for i := 0; i < b.N; i++ { |
| 378 |
res = TrimSpace(tc.input) |
| 379 |
} |
| 380 |
_ = res |
| 381 |
}) |
| 382 |
b.Run("default/"+tc.name, func(b *testing.B) { |
| 383 |
b.ReportAllocs() |
| 384 |
b.SetBytes(int64(len(tc.input))) |
| 385 |
b.ResetTimer() |
| 386 |
var res string |
| 387 |
for i := 0; i < b.N; i++ { |
| 388 |
res = strings.TrimSpace(tc.input) |
| 389 |
} |
| 390 |
_ = res |
| 391 |
}) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func Benchmark_TrimSpaceBytes(b *testing.B) { |
| 396 |
for _, tc := range trimSpaceBenchmarkCases { |
| 397 |
input := []byte(tc.input) |
| 398 |
b.Run("fiber/"+tc.name, func(b *testing.B) { |
| 399 |
b.ReportAllocs() |
| 400 |
b.SetBytes(int64(len(input))) |
| 401 |
b.ResetTimer() |
| 402 |
var res []byte |
| 403 |
for i := 0; i < b.N; i++ { |
| 404 |
res = TrimSpace(input) |
| 405 |
} |
| 406 |
_ = res |
| 407 |
}) |
| 408 |
b.Run("default/"+tc.name, func(b *testing.B) { |
| 409 |
b.ReportAllocs() |
| 410 |
b.SetBytes(int64(len(input))) |
| 411 |
b.ResetTimer() |
| 412 |
var res []byte |
| 413 |
for i := 0; i < b.N; i++ { |
| 414 |
res = bytes.TrimSpace(input) |
| 415 |
} |
| 416 |
_ = res |
| 417 |
}) |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
// Benchmark_EqualFold_Short measures token-sized inputs that are equal up to case, so every byte is inspected. |
| 422 |
func Benchmark_EqualFold_Short(b *testing.B) { |
| 423 |
inputs := []struct { |
| 424 |
name string |
| 425 |
x, y string |
| 426 |
}{ |
| 427 |
{"3B", "GET", "get"}, |
| 428 |
{"4B", "POST", "post"}, |
| 429 |
{"5B", "close", "Close"}, |
| 430 |
{"7B", "chunked", "Chunked"}, |
| 431 |
{"8B", "identity", "Identity"}, |
| 432 |
{"10B", "keep-alive", "Keep-Alive"}, |
| 433 |
{"16B", "application/json", "Application/JSON"}, |
| 434 |
} |
| 435 |
var res bool |
| 436 |
for _, input := range inputs { |
| 437 |
b.Run(input.name+"/fiber", func(b *testing.B) { |
| 438 |
b.ReportAllocs() |
| 439 |
for b.Loop() { |
| 440 |
res = EqualFold(input.x, input.y) |
| 441 |
} |
| 442 |
require.True(b, res) |
| 443 |
}) |
| 444 |
b.Run(input.name+"/default", func(b *testing.B) { |
| 445 |
b.ReportAllocs() |
| 446 |
for b.Loop() { |
| 447 |
res = strings.EqualFold(input.x, input.y) |
| 448 |
} |
| 449 |
require.True(b, res) |
| 450 |
}) |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
// Test_EqualFold_Lengths covers every scan shape with a case-flipped twin and a changed byte at every position. |
| 455 |
func Test_EqualFold_Lengths(t *testing.T) { |
| 456 |
t.Parallel() |
| 457 |
base := "Content-Type: Multipart" |
| 458 |
for n := 0; n <= len(base); n++ { |
| 459 |
a := base[:n] |
| 460 |
flipped := make([]byte, n) |
| 461 |
for i := range n { |
| 462 |
c := a[i] |
| 463 |
switch { |
| 464 |
case c >= 'a' && c <= 'z': |
| 465 |
c -= 0x20 |
| 466 |
case c >= 'A' && c <= 'Z': |
| 467 |
c += 0x20 |
| 468 |
} |
| 469 |
flipped[i] = c |
| 470 |
} |
| 471 |
require.True(t, EqualFold(a, string(flipped)), "len %d", n) |
| 472 |
require.True(t, EqualFold([]byte(a), flipped), "len %d", n) |
| 473 |
for i := range n { |
| 474 |
for _, c := range []byte{'!', '[', '{', '@', '`', 0x00, 0x80, 0xFF, a[i] ^ 0x01} { |
| 475 |
if c == a[i] { |
| 476 |
continue |
| 477 |
} |
| 478 |
mutated := bytes.Clone(flipped) |
| 479 |
mutated[i] = c |
| 480 |
want := asciiFoldUpper(a) == asciiFoldUpper(string(mutated)) |
| 481 |
require.Equal(t, want, EqualFold(a, string(mutated)), "len %d pos %d byte %#x", n, i, c) |
| 482 |
require.Equal(t, want, EqualFold([]byte(a), mutated), "len %d pos %d byte %#x", n, i, c) |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
// '[' and '{' differ only in bit 5, like a letter pair, and must not fold. |
| 487 |
for n := 1; n <= 16; n++ { |
| 488 |
require.False(t, EqualFold(strings.Repeat("[", n), strings.Repeat("{", n)), "len %d", n) |
| 489 |
require.False(t, EqualFold(strings.Repeat("@", n), strings.Repeat("`", n)), "len %d", n) |
| 490 |
} |
| 491 |
} |