1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 10:03:45 +00:00
This commit is contained in:
Fenny 2020-11-25 13:10:01 +01:00
parent 92eb9f5d39
commit 711911aeea
2 changed files with 43 additions and 2 deletions

View File

@ -136,8 +136,10 @@ func (s *Session) Save() error {
return err
}
// Create cookie with the session ID
s.setCookie()
// Create cookie with the session ID if fresh
if s.fresh {
s.setCookie()
}
// Release session
// TODO: It's not safe to use the Session after called Save()

View File

@ -67,6 +67,45 @@ func Test_Session(t *testing.T) {
utils.AssertEqual(t, 36, len(id))
}
// go test -run Test_Session_CustomType
func Test_Session_CustomType(t *testing.T) {
t.Parallel()
// session store
store := New()
// fiber instance
app := fiber.New()
// fiber context
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(ctx)
// set cookie
ctx.Request().Header.SetCookie(store.CookieName, "123")
// get session
sess, err := store.Get(ctx)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, sess.Fresh())
type User struct {
Name string
}
val := User{
Name: "john",
}
// set value
sess.Set("user", val)
// get value
user, ok := sess.Get("user").(User)
utils.AssertEqual(t, true, ok)
utils.AssertEqual(t, val, user)
}
// go test -run Test_Session_Store_Reset
func Test_Session_Store_Reset(t *testing.T) {
t.Parallel()