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

Function Test_Session_Save_AbsoluteTimeout

middleware/session/session_test.go:914–996  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

912}
913
914func Test_Session_Save_AbsoluteTimeout(t *testing.T) {
915 t.Parallel()
916
917 t.Run("save to cookie", func(t *testing.T) {
918 t.Parallel()
919
920 const absoluteTimeout = 2 * time.Second // extra headroom to avoid flakiness under -race
921 // session store
922 store := NewStore(Config{
923 IdleTimeout: absoluteTimeout,
924 AbsoluteTimeout: absoluteTimeout,
925 })
926
927 // force change to IdleTimeout
928 store.IdleTimeout = 10 * time.Second
929
930 // fiber instance
931 app := fiber.New()
932 // fiber context
933 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
934 defer app.ReleaseCtx(ctx)
935
936 // get session
937 sess, err := store.Get(ctx)
938 require.NoError(t, err)
939
940 // set value
941 sess.Set("name", "john")
942
943 token := sess.ID()
944
945 // save session
946 err = sess.Save()
947 require.NoError(t, err)
948
949 sess.Release()
950 app.ReleaseCtx(ctx)
951 ctx = app.AcquireCtx(&fasthttp.RequestCtx{})
952
953 // here you need to get the old session yet
954 ctx.Request().Header.SetCookie("session_id", token)
955 sess, err = store.Get(ctx)
956 require.NoError(t, err)
957 require.Equal(t, "john", sess.Get("name"))
958
959 // just to make sure the session has been expired
960 time.Sleep(absoluteTimeout + (200 * time.Millisecond))
961
962 sess.Release()
963
964 app.ReleaseCtx(ctx)
965 ctx = app.AcquireCtx(&fasthttp.RequestCtx{})
966
967 // here you should get a new session
968 ctx.Request().Header.SetCookie("session_id", token)
969 sess, err = store.Get(ctx)
970 require.NoError(t, err)
971 require.Nil(t, sess.Get("name"))

Callers

nothing calls this directly

Calls 15

GetMethod · 0.95
GetByIDMethod · 0.95
NewStoreFunction · 0.85
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
NewMethod · 0.65
SetMethod · 0.65
RequestMethod · 0.65
GetMethod · 0.65
FreshMethod · 0.65
ErrorMethod · 0.65
IDMethod · 0.45

Tested by

no test coverage detected