forked from bytewayio/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_test.go
121 lines (102 loc) · 2.53 KB
/
session_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cypress
import (
"encoding/gob"
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/go-redis/redis"
)
type TestObj struct {
ID int32
Name string
}
func TestInMemorySessionStore(t *testing.T) {
sessionStore := NewInMemorySessionStore()
defer sessionStore.Close()
testSessionStore(sessionStore, t)
}
func TestFileSessionStore(t *testing.T) {
// setup the container
container := NewSessionID()
testDir := path.Join(os.TempDir(), container)
gob.Register(TestObj{})
err := os.Mkdir(testDir, os.ModePerm)
if err != nil {
t.Error("failed to create test folder", err)
return
}
sessionStore, err := NewFileSessionStore(testDir)
if err != nil {
t.Error("failed to create file session store", err)
return
}
testSessionStore(sessionStore, t)
// clean up
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Error("failed to list test folder")
return
}
for _, file := range files {
os.Remove(path.Join(testDir, file.Name()))
}
os.Remove(testDir)
}
// TestRedisSessionStore test redis store, enable this by change first character to upper case
// however, please make sure redis server is started without any password and default port before
// you run the test case
func testRedisSessionStore(t *testing.T) {
gob.Register(TestObj{})
redisdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
store := NewRedisSessionStore(redisdb)
testSessionStore(store, t)
}
func testSessionStore(sessionStore SessionStore, t *testing.T) {
session := NewSession(NewSessionID())
session.SetValue("key1", "value1")
session.SetValue("key2", &TestObj{123, "abc"})
if !session.NeedSave() {
t.Error("dirty flag is not set")
return
}
err := sessionStore.Save(session, time.Millisecond*50)
if err != nil {
t.Error("failed to save session", err)
return
}
session1, err := sessionStore.Get(session.ID)
if err != nil {
t.Error("session must exist")
return
}
if session1.NeedSave() {
t.Error("dirty flag is set")
return
}
_, ok := session1.GetValue("key1")
if !ok {
t.Error("key1 must exist")
return
}
time.Sleep(time.Millisecond * 51)
_, err = sessionStore.Get(session.ID)
if err != ErrSessionNotFound {
t.Error("session must not be returned")
return
}
session.ID = NewSessionID()
sessionStore.Save(session, time.Minute)
session.IsValid = false
sessionStore.Save(session, time.Minute)
_, err = sessionStore.Get(session.ID)
if err != ErrSessionNotFound {
t.Error("session must not be returned")
return
}
}