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

Function Test_Session_ChainedExtractors

middleware/session/session_test.go:571–704  ·  view source on GitHub ↗

Test chained extractors to ensure both cookie and header are set when both are present

(t *testing.T)

Source from the content-addressed store, hash-verified

569
570// Test chained extractors to ensure both cookie and header are set when both are present
571func Test_Session_ChainedExtractors(t *testing.T) {
572 t.Parallel()
573
574 t.Run("cookie and header chain", func(t *testing.T) {
575 t.Parallel()
576 // session store with chained extractors
577 store := NewStore(Config{
578 Extractor: extractors.Chain(extractors.FromCookie("session_id"), extractors.FromHeader("x-session-id")),
579 })
580 // fiber instance
581 app := fiber.New()
582 // fiber context
583 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
584 defer app.ReleaseCtx(ctx)
585
586 // get session
587 sess, err := store.Get(ctx)
588 require.NoError(t, err)
589 // set value
590 sess.Set("name", "john")
591
592 // save session
593 err = sess.Save()
594 require.NoError(t, err)
595
596 // verify both cookie and header are set
597 cookie := ctx.Response().Header.PeekCookie("session_id")
598 require.NotNil(t, cookie)
599 require.Contains(t, string(cookie), sess.ID())
600
601 header := string(ctx.Response().Header.Peek("x-session-id"))
602 require.Equal(t, sess.ID(), header)
603
604 sess.Release()
605 })
606
607 t.Run("header and cookie chain", func(t *testing.T) {
608 t.Parallel()
609 // session store with chained extractors (different order)
610 store := NewStore(Config{
611 Extractor: extractors.Chain(extractors.FromHeader("x-session-id"), extractors.FromCookie("session_id")),
612 })
613 // fiber instance
614 app := fiber.New()
615 // fiber context
616 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
617 defer app.ReleaseCtx(ctx)
618
619 // get session
620 sess, err := store.Get(ctx)
621 require.NoError(t, err)
622 // set value
623 sess.Set("name", "john")
624
625 // save session
626 err = sess.Save()
627 require.NoError(t, err)
628

Callers

nothing calls this directly

Calls 15

GetMethod · 0.95
ChainFunction · 0.92
FromCookieFunction · 0.92
FromHeaderFunction · 0.92
FromQueryFunction · 0.92
FromFormFunction · 0.92
NewStoreFunction · 0.85
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
ContainsMethod · 0.80
NewMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected