Add tests
This commit is contained in:
parent
4c38047cf7
commit
98c90d0b33
10 changed files with 31 additions and 18 deletions
1
reaper/options_test.go
Normal file
1
reaper/options_test.go
Normal file
|
@ -0,0 +1 @@
|
|||
package reaper
|
1
reaper/reaper_test.go
Normal file
1
reaper/reaper_test.go
Normal file
|
@ -0,0 +1 @@
|
|||
package reaper
|
|
@ -2,9 +2,14 @@ package shared
|
|||
|
||||
import "time"
|
||||
|
||||
// Defaults for sessions.Options
|
||||
const (
|
||||
DefaultPath = "/"
|
||||
DefaultMaxAge = 60 * 60 * 24 * 30 // 30days
|
||||
)
|
||||
|
||||
// Defaults for store.Options
|
||||
const (
|
||||
DefaultPath = "/"
|
||||
DefaultBucketName = "sessions"
|
||||
)
|
||||
|
||||
|
|
|
@ -18,3 +18,9 @@ func Session(data []byte) (protobuf.Session, error) {
|
|||
func Expired(session protobuf.Session) bool {
|
||||
return *session.ExpiresAt > 0 && *session.ExpiresAt <= time.Now().Unix()
|
||||
}
|
||||
|
||||
// NewSession creates and returns a session data.
|
||||
func NewSession(values []byte, maxAge int) *protobuf.Session {
|
||||
expiresAt := time.Now().Unix() + int64(maxAge)
|
||||
return &protobuf.Session{Values: values, ExpiresAt: &expiresAt}
|
||||
}
|
||||
|
|
|
@ -48,3 +48,14 @@ func TestExpired(t *testing.T) {
|
|||
t.Error("Expired() should return false (actual: true)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSession(t *testing.T) {
|
||||
values := []byte("test")
|
||||
maxAge := 10
|
||||
preExpiresAt := time.Now().Unix() + int64(maxAge)
|
||||
session := NewSession(values, maxAge)
|
||||
postExpiresAt := time.Now().Unix() + int64(maxAge)
|
||||
if string(session.Values) != string(values) || *session.ExpiresAt < preExpiresAt || postExpiresAt < *session.ExpiresAt {
|
||||
t.Errorf("NewSession() returned an invalid value (actual: %+v)", session)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@ func (c *Config) setDefault() {
|
|||
if c.SessionOptions.Path == "" {
|
||||
c.SessionOptions.Path = shared.DefaultPath
|
||||
}
|
||||
if c.SessionOptions.MaxAge == 0 {
|
||||
c.SessionOptions.MaxAge = shared.DefaultMaxAge
|
||||
}
|
||||
if c.DBOptions.BucketName == nil {
|
||||
c.DBOptions.BucketName = []byte(shared.DefaultBucketName)
|
||||
}
|
||||
|
|
1
store/config_test.go
Normal file
1
store/config_test.go
Normal file
|
@ -0,0 +1 @@
|
|||
package store
|
|
@ -1,16 +0,0 @@
|
|||
package store
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/yosssi/boltstore/shared/protobuf"
|
||||
)
|
||||
|
||||
// NewSession creates and returns a session data.
|
||||
func NewSession(values []byte, maxAge int) *protobuf.Session {
|
||||
var expiresAt int64
|
||||
if maxAge > 0 {
|
||||
expiresAt = time.Now().Unix() + int64(maxAge)
|
||||
}
|
||||
return &protobuf.Session{Values: values, ExpiresAt: &expiresAt}
|
||||
}
|
|
@ -118,7 +118,7 @@ func (s *Store) save(session *sessions.Session) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := proto.Marshal(NewSession(buf.Bytes(), session.Options.MaxAge))
|
||||
data, err := proto.Marshal(shared.NewSession(buf.Bytes(), session.Options.MaxAge))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
|
1
store/store_test.go
Normal file
1
store/store_test.go
Normal file
|
@ -0,0 +1 @@
|
|||
package store
|
Loading…
Reference in a new issue