1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-06 11:02:01 +00:00
fiber/helpers_fuzz_test.go
nickajacks1 8ca562c92e
♻️Refactor: Simplify content negotiation code (#2865)
* refactor: Update to use fasthttp.VisitHeaderParams

The implementation of forEachParameter was upstreamed to fasthttp, so
use that version instead of maintaining our own.

* refactor: use map for header params

The previous implementation of content negotiation used some difficult
to understand techniques in order to reduce allocations, potentially
hurting maintainability. The more natural approach for storing and
comparing unordered media-type parameters is to utilize maps. While the
resulting code still isn't as simple as it could be, it's a step closer.

To reduce allocations, we use a sync.Pool. This actually reduces in fewer
allocations than before at 3 or more parameters. The net result is nearly
identical performance for zero parameters, almost-as-good performance for
1-2 parameters, and better performance for 3+ parameters.

---------

Co-authored-by: Jason McNeil <sixcolors@mac.com>
2024-02-19 15:28:06 +01:00

24 lines
617 B
Go

//go:build go1.18
package fiber
import (
"testing"
)
// go test -v -run=^$ -fuzz=FuzzUtilsGetOffer
func FuzzUtilsGetOffer(f *testing.F) {
inputs := []string{
`application/json; v=1; foo=bar; q=0.938; extra=param, text/plain;param="big fox"; q=0.43`,
`text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8`,
`*/*`,
`text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c`,
}
for _, input := range inputs {
f.Add(input)
}
f.Fuzz(func(_ *testing.T, spec string) {
getOffer([]byte(spec), acceptsOfferType, `application/json;version=1;v=1;foo=bar`, `text/plain;param="big fox"`)
})
}