mirror of
https://github.com/zap-proto/fiber.git
synced 2026-10-30 15:06:23 +00:00
* Revert "Revert "🐛 requestid.Config.ContextKey is interface{} (#2369)" (#2742)"
This reverts commit 21ea005b07.
* fix: request ContextKey default value condition
Should check for `nil` since it is `any`.
* fix: don't constrain middlewares' context-keys to strings
`context` recommends using "unexported type" as context keys to avoid
collisions https://pkg.go.dev/github.com/gofiber/fiber/v2#Ctx.Locals.
The official go blog also recommends this https://go.dev/blog/context.
`fiber.Ctx.Locals(key any, value any)` correctly allows consumers to
use unexported types or e.g. strings.
But some fiber middlewares constrain their context-keys to `string` in
their "default config structs", making it impossible to use unexported
types.
This PR removes the `string` _constraint_ from all middlewares, allowing
to now use unexported types as per the official guidelines. However
the default value is still a string, so it's not a breaking change, and
anyone still using strings as context keys is not affected.
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
package requestid
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/utils"
|
|
)
|
|
|
|
// go test -run Test_RequestID
|
|
func Test_RequestID(t *testing.T) {
|
|
t.Parallel()
|
|
app := fiber.New()
|
|
|
|
app.Use(New())
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
return c.SendString("Hello, World 👋!")
|
|
})
|
|
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
|
utils.AssertEqual(t, nil, err)
|
|
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
|
|
|
reqid := resp.Header.Get(fiber.HeaderXRequestID)
|
|
utils.AssertEqual(t, 36, len(reqid))
|
|
|
|
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
|
|
req.Header.Add(fiber.HeaderXRequestID, reqid)
|
|
|
|
resp, err = app.Test(req)
|
|
utils.AssertEqual(t, nil, err)
|
|
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
|
utils.AssertEqual(t, reqid, resp.Header.Get(fiber.HeaderXRequestID))
|
|
}
|
|
|
|
// go test -run Test_RequestID_Next
|
|
func Test_RequestID_Next(t *testing.T) {
|
|
t.Parallel()
|
|
app := fiber.New()
|
|
app.Use(New(Config{
|
|
Next: func(_ *fiber.Ctx) bool {
|
|
return true
|
|
},
|
|
}))
|
|
|
|
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
|
utils.AssertEqual(t, nil, err)
|
|
utils.AssertEqual(t, resp.Header.Get(fiber.HeaderXRequestID), "")
|
|
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
|
}
|
|
|
|
// go test -run Test_RequestID_Locals
|
|
func Test_RequestID_Locals(t *testing.T) {
|
|
t.Parallel()
|
|
reqID := "ThisIsARequestId"
|
|
type ContextKey int
|
|
const requestContextKey ContextKey = iota
|
|
|
|
app := fiber.New()
|
|
app.Use(New(Config{
|
|
Generator: func() string {
|
|
return reqID
|
|
},
|
|
ContextKey: requestContextKey,
|
|
}))
|
|
|
|
var ctxVal string
|
|
|
|
app.Use(func(c *fiber.Ctx) error {
|
|
ctxVal = c.Locals(requestContextKey).(string) //nolint:forcetypeassert,errcheck // We always store a string in here
|
|
return c.Next()
|
|
})
|
|
|
|
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
|
utils.AssertEqual(t, nil, err)
|
|
utils.AssertEqual(t, reqID, ctxVal)
|
|
}
|
|
|
|
// go test -run Test_RequestID_DefaultKey
|
|
func Test_RequestID_DefaultKey(t *testing.T) {
|
|
t.Parallel()
|
|
reqID := "ThisIsARequestId"
|
|
|
|
app := fiber.New()
|
|
app.Use(New(Config{
|
|
Generator: func() string {
|
|
return reqID
|
|
},
|
|
}))
|
|
|
|
var ctxVal string
|
|
|
|
app.Use(func(c *fiber.Ctx) error {
|
|
ctxVal = c.Locals("requestid").(string) //nolint:forcetypeassert,errcheck // We always store a string in here
|
|
return c.Next()
|
|
})
|
|
|
|
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
|
|
utils.AssertEqual(t, nil, err)
|
|
utils.AssertEqual(t, reqID, ctxVal)
|
|
}
|