mirror of https://github.com/gofiber/fiber.git
added some tests
parent
9733eabf63
commit
7376a707ef
2
ctx.go
2
ctx.go
|
@ -451,7 +451,7 @@ func (c *DefaultCtx) Cookie(cookie *Cookie) {
|
|||
// Due to current limitations in how fasthttp works, Deadline operates as a nop.
|
||||
// See: https://github.com/valyala/fasthttp/issues/965#issuecomment-777268945
|
||||
func (*DefaultCtx) Deadline() (deadline time.Time, ok bool) {
|
||||
return time.Time{}, false
|
||||
return deadline, false
|
||||
}
|
||||
|
||||
// Done returns a channel that's closed when work done on behalf of this
|
||||
|
|
50
ctx_test.go
50
ctx_test.go
|
@ -2187,6 +2187,56 @@ func Test_Ctx_Locals(t *testing.T) {
|
|||
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_Deadline
|
||||
func Test_Ctx_Deadline(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := New()
|
||||
app.Use(func(c Ctx) error {
|
||||
return c.Next()
|
||||
})
|
||||
app.Get("/test", func(c Ctx) error {
|
||||
deadline, ok := c.Deadline()
|
||||
require.Equal(t, time.Time{}, deadline)
|
||||
require.Equal(t, false, ok)
|
||||
return nil
|
||||
})
|
||||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
|
||||
require.NoError(t, err, "app.Test(req)")
|
||||
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_Done
|
||||
func Test_Ctx_Done(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := New()
|
||||
app.Use(func(c Ctx) error {
|
||||
return c.Next()
|
||||
})
|
||||
app.Get("/test", func(c Ctx) error {
|
||||
require.Equal(t, (<-chan struct {})(nil), c.Done())
|
||||
return nil
|
||||
})
|
||||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
|
||||
require.NoError(t, err, "app.Test(req)")
|
||||
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_Err
|
||||
func Test_Ctx_Err(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := New()
|
||||
app.Use(func(c Ctx) error {
|
||||
return c.Next()
|
||||
})
|
||||
app.Get("/test", func(c Ctx) error {
|
||||
require.Equal(t, nil, c.Err())
|
||||
return nil
|
||||
})
|
||||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test", nil))
|
||||
require.NoError(t, err, "app.Test(req)")
|
||||
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_Value
|
||||
func Test_Ctx_Value(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
|
|
@ -72,14 +72,14 @@ func New(config ...Config) fiber.Handler {
|
|||
// returns an empty string if the token does not exist
|
||||
func TokenFromContext(c any) string {
|
||||
switch ctx := c.(type) {
|
||||
case context.Context:
|
||||
if token, ok := ctx.Value(tokenKey).(string); ok {
|
||||
return token
|
||||
}
|
||||
case fiber.Ctx:
|
||||
if token, ok := ctx.Locals(tokenKey).(string); ok {
|
||||
return token
|
||||
}
|
||||
case context.Context:
|
||||
if token, ok := ctx.Value(tokenKey).(string); ok {
|
||||
return token
|
||||
}
|
||||
default:
|
||||
panic("unsupported context type, expected fiber.Ctx or context.Context")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue