mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-19 13:07:52 +00:00
Basic auth alloc (#2333)
* basic_auth: extend benchmark for uppercase Basic * basic_auth: check space after basic (and avoid alloc if Basic) * fixup! basic_auth: check space after basic (and avoid alloc if Basic)
This commit is contained in:
parent
c3b151a1fe
commit
497eb02b48
@ -24,7 +24,7 @@ func New(config Config) fiber.Handler {
|
||||
auth := c.Get(fiber.HeaderAuthorization)
|
||||
|
||||
// Check if the header contains content besides "basic".
|
||||
if len(auth) <= 6 || strings.ToLower(auth[:5]) != "basic" {
|
||||
if len(auth) <= 6 || !utils.EqualFold(auth[:6], "basic ") {
|
||||
return cfg.Unauthorized(c)
|
||||
}
|
||||
|
||||
|
@ -122,3 +122,33 @@ func Benchmark_Middleware_BasicAuth(b *testing.B) {
|
||||
|
||||
utils.AssertEqual(b, fiber.StatusTeapot, fctx.Response.Header.StatusCode())
|
||||
}
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_Middleware_BasicAuth -benchmem -count=4
|
||||
func Benchmark_Middleware_BasicAuth_Upper(b *testing.B) {
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Users: map[string]string{
|
||||
"john": "doe",
|
||||
},
|
||||
}))
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.SendStatus(fiber.StatusTeapot)
|
||||
})
|
||||
|
||||
h := app.Handler()
|
||||
|
||||
fctx := &fasthttp.RequestCtx{}
|
||||
fctx.Request.Header.SetMethod(fiber.MethodGet)
|
||||
fctx.Request.SetRequestURI("/")
|
||||
fctx.Request.Header.Set(fiber.HeaderAuthorization, "Basic am9objpkb2U=") // john:doe
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
h(fctx)
|
||||
}
|
||||
|
||||
utils.AssertEqual(b, fiber.StatusTeapot, fctx.Response.Header.StatusCode())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user