From d86c257c89fc320fdb95e4e2d0b9f35842106dda Mon Sep 17 00:00:00 2001 From: KaptinLin Date: Mon, 2 Oct 2023 21:19:18 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20Delete=20method=20to?= =?UTF-8?q?=20Store=20struct=20in=20session=20middleware=20(#2655)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ feat: add Delete method to Store struct in session middleware * ♻ refactor: enhance Delete method and test cases in session middleware --- docs/api/middleware/session.md | 1 + middleware/session/store.go | 12 ++++++++++++ middleware/session/store_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/docs/api/middleware/session.md b/docs/api/middleware/session.md index 65d23681e7..08b115cb75 100644 --- a/docs/api/middleware/session.md +++ b/docs/api/middleware/session.md @@ -16,6 +16,7 @@ This middleware uses our [Storage](https://github.com/gofiber/storage) package t func New(config ...Config) *Store func (s *Store) RegisterType(i interface{}) func (s *Store) Get(c *fiber.Ctx) (*Session, error) +func (s *Store) Delete(id string) error func (s *Store) Reset() error func (s *Session) Get(key string) interface{} diff --git a/middleware/session/store.go b/middleware/session/store.go index bda5db7f45..f0c84f0653 100644 --- a/middleware/session/store.go +++ b/middleware/session/store.go @@ -2,6 +2,7 @@ package session import ( "encoding/gob" + "errors" "fmt" "sync" @@ -12,6 +13,9 @@ import ( "github.com/valyala/fasthttp" ) +// ErrEmptySessionID is an error that occurs when the session ID is empty. +var ErrEmptySessionID = errors.New("session id cannot be empty") + type Store struct { Config } @@ -140,3 +144,11 @@ func (s *Store) responseCookies(c *fiber.Ctx) (string, error) { func (s *Store) Reset() error { return s.Storage.Reset() } + +// Delete deletes a session by its id. +func (s *Store) Delete(id string) error { + if id == "" { + return ErrEmptySessionID + } + return s.Storage.Delete(id) +} diff --git a/middleware/session/store_test.go b/middleware/session/store_test.go index 06c45f9b10..fbc6e846b0 100644 --- a/middleware/session/store_test.go +++ b/middleware/session/store_test.go @@ -85,3 +85,34 @@ func TestStore_Get(t *testing.T) { utils.AssertEqual(t, unexpectedID, acquiredSession.ID()) }) } + +// go test -run TestStore_DeleteSession +func TestStore_DeleteSession(t *testing.T) { + t.Parallel() + // fiber instance + app := fiber.New() + // session store + store := New() + + // fiber context + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(ctx) + + // Create a new session + session, err := store.Get(ctx) + utils.AssertEqual(t, err, nil) + + // Save the session ID + sessionID := session.ID() + + // Delete the session + err = store.Delete(sessionID) + utils.AssertEqual(t, err, nil) + + // Try to get the session again + session, err = store.Get(ctx) + utils.AssertEqual(t, err, nil) + + // The session ID should be different now, because the old session was deleted + utils.AssertEqual(t, session.ID() == sessionID, false) +}