boltstore/shared/utils_test.go

62 lines
1.5 KiB
Go
Raw Permalink Normal View History

2014-06-16 13:23:48 +04:00
package shared
import (
2016-02-18 12:08:51 +03:00
"github.com/admpub/boltstore/shared/protobuf"
2014-06-16 13:23:48 +04:00
"testing"
"time"
"github.com/gogo/protobuf/proto"
2014-06-16 13:23:48 +04:00
)
func TestSession(t *testing.T) {
expiresAt := time.Now().Unix()
sessionOrig := &protobuf.Session{
Values: []byte("test"),
ExpiresAt: &expiresAt,
}
data, err := proto.Marshal(sessionOrig)
if err != nil {
t.Error(err.Error())
}
session, err := Session(data)
if err != nil {
t.Error(err.Error())
}
if string(session.Values) != "test" || *session.ExpiresAt != expiresAt {
t.Errorf("Session() should return %+v (actual: %+v)", sessionOrig, session)
}
}
func TestExpired(t *testing.T) {
// When Expired() should return true.
expiresAt := time.Now().Unix()
session := protobuf.Session{
Values: []byte("test"),
ExpiresAt: &expiresAt,
}
if Expired(session) != true {
t.Error("Expired() should return true (actual: false)")
}
// When Expired() should return false.
expiresAt = time.Now().Add(time.Hour).Unix()
session = protobuf.Session{
Values: []byte("test"),
ExpiresAt: &expiresAt,
}
if Expired(session) != false {
t.Error("Expired() should return false (actual: true)")
}
}
2014-06-17 12:53:24 +04:00
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)
}
}