Skip to content

Commit

Permalink
Merge pull request #44 from tinh-tinh/enhance/ren/add-unit-test
Browse files Browse the repository at this point in the history
fix: add unit test session
  • Loading branch information
Ren0503 authored Oct 4, 2024
2 parents 1de56a8 + e5624a9 commit 17238ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions middleware/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type Options struct {

func New(opt Options) *Config {
session := &Config{
Secret: opt.Secret,
store: memory.New(opt.StoreOptions),
Secret: opt.Secret,
store: memory.New(opt.StoreOptions),
ExpiresIn: opt.ExpiresIn,
}
if session.ExpiresIn == 0 {
session.ExpiresIn = time.Hour
Expand Down
30 changes: 30 additions & 0 deletions middleware/session/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package session

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

func Test_Session(t *testing.T) {
s := New(Options{
GeneratorID: func() string {
return time.Now().String()
},
})

cookie := s.Set("abc", "mno")
require.Equal(t, "mno", s.Get(cookie.Value))
}

func Test_Expiration(t *testing.T) {
s := New(Options{
ExpiresIn: 3 * time.Second,
})

cookie := s.Set("abc", "mno")
require.Equal(t, "mno", s.Get(cookie.Value))
time.Sleep(3 * time.Second)
require.Nil(t, s.Get(cookie.Value))
}

0 comments on commit 17238ae

Please sign in to comment.