forked from rbcervilla/redisstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisstore_test.go
158 lines (127 loc) · 3.13 KB
/
redisstore_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package redisstore
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/gorilla/sessions"
"net/http"
"net/http/httptest"
"testing"
)
const (
redisAddr = "localhost:6379"
)
func TestNew(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
})
store, err := NewRedisStore(context.Background(), client)
if err != nil {
t.Fatal("failed to create redis store", err)
}
req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
t.Fatal("failed to create request", err)
}
session, err := store.New(req, "hello")
if err != nil {
t.Fatal("failed to create session", err)
}
if session.IsNew == false {
t.Fatal("session is not new")
}
}
func TestOptions(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
})
store, err := NewRedisStore(context.Background(), client)
if err != nil {
t.Fatal("failed to create redis store", err)
}
opts := sessions.Options{
Path: "/path",
MaxAge: 99999,
}
store.Options(opts)
req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
t.Fatal("failed to create request", err)
}
session, err := store.New(req, "hello")
if session.Options.Path != opts.Path || session.Options.MaxAge != opts.MaxAge {
t.Fatal("failed to set options")
}
}
func TestSave(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
})
store, err := NewRedisStore(context.Background(), client)
if err != nil {
t.Fatal("failed to create redis store", err)
}
req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
t.Fatal("failed to create request", err)
}
w := httptest.NewRecorder()
session, err := store.New(req, "hello")
if err != nil {
t.Fatal("failed to create session", err)
}
session.Values["key"] = "value"
err = session.Save(req, w)
if err != nil {
t.Fatal("failed to save: ", err)
}
}
func TestDelete(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
})
store, err := NewRedisStore(context.Background(), client)
if err != nil {
t.Fatal("failed to create redis store", err)
}
req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
t.Fatal("failed to create request", err)
}
w := httptest.NewRecorder()
session, err := store.New(req, "hello")
if err != nil {
t.Fatal("failed to create session", err)
}
session.Values["key"] = "value"
err = session.Save(req, w)
if err != nil {
t.Fatal("failed to save session: ", err)
}
session.Options.MaxAge = -1
err = session.Save(req, w)
if err != nil {
t.Fatal("failed to delete session: ", err)
}
}
func TestClose(t *testing.T) {
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
})
cmd := client.Ping(context.Background())
err := cmd.Err()
if err != nil {
t.Fatal("connection is not opened")
}
store, err := NewRedisStore(context.Background(), client)
if err != nil {
t.Fatal("failed to create redis store", err)
}
err = store.Close()
if err != nil {
t.Fatal("failed to close")
}
cmd = client.Ping(context.Background())
if cmd.Err() == nil {
t.Fatal("connection is properly closed")
}
}