fiber/middleware/session/store_test.go
M. Efe Çetin a458bd344c
v3 (feature): convert fiber.Ctx type to interface (#1928)
*  v3: convert fiber.Ctx type to interface

* update ctx methods

* add new methods to customize ctx, fix some problems

* update comments.

* fix something
2022-07-13 07:48:29 +02:00

83 lines
1.9 KiB
Go

package session
import (
"fmt"
"testing"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils"
"github.com/valyala/fasthttp"
)
// go test -run TestStore_getSessionID
func TestStore_getSessionID(t *testing.T) {
expectedID := "test-session-id"
// fiber instance
app := fiber.New()
t.Run("from cookie", func(t *testing.T) {
// session store
store := New()
// fiber context
ctx := app.NewCtx(&fasthttp.RequestCtx{})
// set cookie
ctx.Request().Header.SetCookie(store.sessionName, expectedID)
utils.AssertEqual(t, expectedID, store.getSessionID(ctx))
})
t.Run("from header", func(t *testing.T) {
// session store
store := New(Config{
KeyLookup: "header:session_id",
})
// fiber context
ctx := app.NewCtx(&fasthttp.RequestCtx{})
// set header
ctx.Request().Header.Set(store.sessionName, expectedID)
utils.AssertEqual(t, expectedID, store.getSessionID(ctx))
})
t.Run("from url query", func(t *testing.T) {
// session store
store := New(Config{
KeyLookup: "query:session_id",
})
// fiber context
ctx := app.NewCtx(&fasthttp.RequestCtx{})
// set url parameter
ctx.Request().SetRequestURI(fmt.Sprintf("/path?%s=%s", store.sessionName, expectedID))
utils.AssertEqual(t, expectedID, store.getSessionID(ctx))
})
}
// go test -run TestStore_Get
// Regression: https://github.com/gofiber/fiber/issues/1408
func TestStore_Get(t *testing.T) {
unexpectedID := "test-session-id"
// fiber instance
app := fiber.New()
t.Run("session should persisted even session is invalid", func(t *testing.T) {
// session store
store := New()
// fiber context
ctx := app.NewCtx(&fasthttp.RequestCtx{})
// set cookie
ctx.Request().Header.SetCookie(store.sessionName, unexpectedID)
acquiredSession, err := store.Get(ctx)
utils.AssertEqual(t, err, nil)
if acquiredSession.ID() != unexpectedID {
t.Fatal("server should not accept the unexpectedID which is not in the store")
}
})
}