MCPcopy
hub / github.com/gofiber/fiber / Test_Session_Next

Function Test_Session_Next

middleware/session/middleware_test.go:585–636  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

583}
584
585func Test_Session_Next(t *testing.T) {
586 t.Parallel()
587
588 var (
589 doNext bool
590 muNext sync.RWMutex
591 )
592
593 app := fiber.New()
594
595 app.Use(New(Config{
596 Next: func(_ fiber.Ctx) bool {
597 muNext.RLock()
598 defer muNext.RUnlock()
599 return doNext
600 },
601 }))
602
603 app.Get("/", func(c fiber.Ctx) error {
604 sess := FromContext(c)
605 if sess == nil {
606 return c.SendStatus(fiber.StatusInternalServerError)
607 }
608 id := sess.ID()
609 return c.SendString("value=" + id)
610 })
611
612 h := app.Handler()
613
614 // Test with Next returning false
615 ctx := &fasthttp.RequestCtx{}
616 ctx.Request.Header.SetMethod(fiber.MethodGet)
617 h(ctx)
618 require.Equal(t, fiber.StatusOK, ctx.Response.StatusCode())
619 // Get session cookie
620 token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie))
621 require.NotEmpty(t, token, "Expected Set-Cookie header to be present")
622 tokenParts := strings.SplitN(strings.SplitN(token, ";", 2)[0], "=", 2)
623 require.Len(t, tokenParts, 2, "Expected Set-Cookie header to contain a token")
624 token = tokenParts[1]
625 require.Equal(t, "value="+token, string(ctx.Response.Body()))
626
627 // Test with Next returning true
628 muNext.Lock()
629 doNext = true
630 muNext.Unlock()
631
632 ctx = &fasthttp.RequestCtx{}
633 ctx.Request.Header.SetMethod(fiber.MethodGet)
634 h(ctx)
635 require.Equal(t, fiber.StatusInternalServerError, ctx.Response.StatusCode())
636}
637
638func Test_Session_Middleware_Store(t *testing.T) {
639 t.Parallel()

Callers

nothing calls this directly

Calls 15

HandlerMethod · 0.80
SetMethodMethod · 0.80
NewFunction · 0.70
FromContextFunction · 0.70
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
SendStatusMethod · 0.65
SendStringMethod · 0.65
LenMethod · 0.65
BodyMethod · 0.65
LockMethod · 0.65

Tested by

no test coverage detected