From 1022c2d36c39ca316c721b3e9b1bc8aafaf8f401 Mon Sep 17 00:00:00 2001 From: Darko Luketic <2694548+dalu@users.noreply.github.com> Date: Thu, 2 Apr 2020 21:32:02 +0200 Subject: [PATCH] fixed new logic --- go.mod | 1 + store.go | 31 +++++++++++-------------------- store_test.go | 40 ++-------------------------------------- 3 files changed, 14 insertions(+), 58 deletions(-) diff --git a/go.mod b/go.mod index a9aabbb..3879a9c 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/dalu/mongostore go 1.14 require ( + github.com/davecgh/go-spew v1.1.1 github.com/gorilla/securecookie v1.1.1 github.com/gorilla/sessions v1.2.0 go.mongodb.org/mongo-driver v1.3.1 diff --git a/store.go b/store.go index c383475..efa4df7 100644 --- a/store.go +++ b/store.go @@ -50,27 +50,18 @@ func (s *MongoStore) MaxAge(age int) { func (s *MongoStore) New(r *http.Request, name string) (*sessions.Session, error) { session := sessions.NewSession(s, name) - session.Options = &sessions.Options{ - Path: s.Options.Path, - MaxAge: s.Options.MaxAge, - Domain: s.Options.Domain, - Secure: s.Options.Secure, - HttpOnly: s.Options.HttpOnly, - } + opts := *s.Options + session.Options = &opts session.IsNew = true cookie, e := r.Cookie(name) - if e != nil { - return session, e - } - e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...) - if e != nil { - return session, e - } - e = s.load(session) - if e != nil { - return session, e - } else { - session.IsNew = false + if e == nil { + e = securecookie.DecodeMulti(name, cookie.Value, &session.ID, s.Codecs...) + if e == nil { + e = s.load(session) + if e == nil { + session.IsNew = false + } + } } return session, nil } @@ -80,7 +71,7 @@ func (s *MongoStore) Get(r *http.Request, name string) (*sessions.Session, error } func (s *MongoStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error { - if session.Options.MaxAge < 0 { + if session.Options.MaxAge <= 0 { if e := s.remove(session); e != nil { return e } diff --git a/store_test.go b/store_test.go index 8deb776..eaa1674 100644 --- a/store_test.go +++ b/store_test.go @@ -44,14 +44,14 @@ func TestFlashes(t *testing.T) { var session *sessions.Session var flashes []interface{} - ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) + ctx := context.Background() client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) err = client.Ping(ctx, readpref.Primary()) if err != nil { panic(err) } - store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second) + store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second, []byte("hellol-worlol")) store.Options.Path = "/" store.Options.MaxAge = 86400 * 30 * 365 defer store.Close() @@ -199,42 +199,6 @@ func TestFlashes(t *testing.T) { } } -func TestCookieStoreMapPanic(t *testing.T) { - defer func() { - err := recover() - if err != nil { - t.Fatal(err) - } - }() - - ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) - client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) - err = client.Ping(ctx, readpref.Primary()) - if err != nil { - panic(err) - } - - store := mongostore.NewMongoStore(client, "test", "sessions", 3*time.Second, []byte("aaa0defe5d2839cbc46fc4f080cd7adc")) - store.Options.Path = "/" - store.Options.MaxAge = 86400 * 30 * 365 - defer store.Close() - - req, err := http.NewRequest("GET", "http://www.example.com", nil) - if err != nil { - t.Fatal("failed to create request", err) - } - w := httptest.NewRecorder() - - session := sessions.NewSession(store, "hello") - - session.Values["data"] = "hello-world" - - err = session.Save(req, w) - if err != nil { - t.Fatal("failed to save session", err) - } -} - func init() { gob.Register(FlashMessage{}) }