Skip to content

Commit

Permalink
fix: implement missing session.Create method
Browse files Browse the repository at this point in the history
  • Loading branch information
vividvilla committed Feb 24, 2019
1 parent b133309 commit 39fbfba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ func (s *Session) clearCookie() error {
return s.manager.setCookieCb(s.cookie, s.writer)
}

// Create a new session. This is implicit when option `DisableAutoSet` is false
// else session has to be manually created before setting or getting values.
func (s *Session) Create() error {
// Create new cookie in store and write to front.
cv, err := s.manager.store.Create(s)
if err != nil {
return err
}

// Write cookie
if err := s.WriteCookie(cv); err != nil {
return err
}

// Set isSet flag
s.isSet = true

return nil
}

// LoadValues loads the session values in memory.
// Get session field tries to get value from memory before hitting store.
func (s *Session) LoadValues() error {
Expand Down
28 changes: 28 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,34 @@ func TestSessionClearCookie(t *testing.T) {
assert.True(receivedCookie.Expires.UnixNano() < time.Now().UnixNano())
}

func TestSessionCreate(t *testing.T) {
assert := assert.New(t)
mockStore := newMockStore()
mockStore.isValid = true
mockStore.val = "test"
mockManager := newMockManager(mockStore)
mockManager.opts.DisableAutoSet = true
mockManager.RegisterGetCookie(func(name string, r interface{}) (*http.Cookie, error) {
return nil, http.ErrNoCookie
})

var isCallbackTriggered bool
mockManager.RegisterSetCookie(func(cookie *http.Cookie, w interface{}) error {
isCallbackTriggered = true
return nil
})

sess, err := NewSession(mockManager, nil, nil)
assert.NoError(err)
assert.False(sess.isSet)
assert.False(isCallbackTriggered)

err = sess.Create()
assert.NoError(err)
assert.True(isCallbackTriggered)
assert.True(sess.isSet)
}

func TestSessionLoadValues(t *testing.T) {
mockStore := newMockStore()
mockStore.isValid = true
Expand Down

0 comments on commit 39fbfba

Please sign in to comment.