diff --git a/app.go b/app.go index 810839c6fc..6fc214a35e 100644 --- a/app.go +++ b/app.go @@ -31,7 +31,7 @@ import ( ) // Version of current fiber package -const Version = "2.2.2" +const Version = "2.2.3" // Handler defines a function to serve HTTP requests. type Handler = func(*Ctx) error diff --git a/internal/storage/memory/memory.go b/internal/storage/memory/memory.go index 054007092d..0612074a56 100644 --- a/internal/storage/memory/memory.go +++ b/internal/storage/memory/memory.go @@ -1,7 +1,6 @@ package memory import ( - "errors" "sync" "time" ) @@ -14,9 +13,6 @@ type Storage struct { done chan struct{} } -// Common storage errors -var ErrNotExist = errors.New("key does not exist") - type entry struct { data []byte expiry int64 @@ -40,13 +36,13 @@ func New() *Storage { // Get value by key func (s *Storage) Get(key string) ([]byte, error) { if len(key) <= 0 { - return nil, ErrNotExist + return nil, nil } s.mux.RLock() v, ok := s.db[key] s.mux.RUnlock() if !ok || v.expiry != 0 && v.expiry <= time.Now().Unix() { - return nil, ErrNotExist + return nil, nil } return v.data, nil diff --git a/middleware/session/session_test.go b/middleware/session/session_test.go index 018f66d1c8..289612302d 100644 --- a/middleware/session/session_test.go +++ b/middleware/session/session_test.go @@ -270,8 +270,8 @@ func Test_Session_Cookie(t *testing.T) { sess, _ := store.Get(ctx) sess.Save() - // cookie should not be set if empty data - utils.AssertEqual(t, 0, len(ctx.Response().Header.PeekCookie(store.CookieName))) + // cookie should be set on Save ( even if empty data ) + utils.AssertEqual(t, 84, len(ctx.Response().Header.PeekCookie(store.CookieName))) } // go test -v -run=^$ -bench=Benchmark_Session -benchmem -count=4 diff --git a/middleware/session/store.go b/middleware/session/store.go index 17f2473223..1f6c438ec1 100644 --- a/middleware/session/store.go +++ b/middleware/session/store.go @@ -53,12 +53,12 @@ func (s *Store) Get(c *fiber.Ctx) (*Session, error) { if !fresh { raw, err := s.Storage.Get(id) // Unmashal if we found data - if err == nil { + if raw != nil && err == nil { mux.Lock() gotiny.Unmarshal(raw, &sess.data) mux.Unlock() sess.fresh = false - } else if raw != nil && err.Error() != "key does not exist" { + } else if err != nil { return nil, err } else { sess.fresh = true