2020-08-06 09:14:36 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-08-20 14:25:37 +00:00
|
|
|
"fmt"
|
2020-08-06 09:14:36 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-08-20 14:25:37 +00:00
|
|
|
"os"
|
2020-08-06 09:14:36 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-08 09:17:15 +00:00
|
|
|
"github.com/astaxie/beego/server/web/session"
|
2020-08-06 09:14:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRedis(t *testing.T) {
|
|
|
|
sessionConfig := &session.ManagerConfig{
|
|
|
|
CookieName: "gosessionid",
|
|
|
|
EnableSetCookie: true,
|
|
|
|
Gclifetime: 3600,
|
|
|
|
Maxlifetime: 3600,
|
|
|
|
Secure: false,
|
|
|
|
CookieLifeTime: 3600,
|
|
|
|
}
|
2020-08-20 14:25:37 +00:00
|
|
|
|
|
|
|
redisAddr := os.Getenv("REDIS_ADDR")
|
|
|
|
if redisAddr == "" {
|
|
|
|
redisAddr = "127.0.0.1:6379"
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionConfig.ProviderConfig = fmt.Sprintf("%s,100,,0,30", redisAddr)
|
2020-08-06 09:14:36 +00:00
|
|
|
globalSession, err := session.NewManager("redis", sessionConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not create manager:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go globalSession.GC()
|
|
|
|
|
|
|
|
r, _ := http.NewRequest("GET", "/", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
sess, err := globalSession.SessionStart(w, r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("session start failed:", err)
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
defer sess.SessionRelease(nil, w)
|
2020-08-06 09:14:36 +00:00
|
|
|
|
|
|
|
// SET AND GET
|
2020-08-30 15:39:07 +00:00
|
|
|
err = sess.Set(nil, "username", "astaxie")
|
2020-08-06 09:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("set username failed:", err)
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
username := sess.Get(nil, "username")
|
2020-08-06 09:14:36 +00:00
|
|
|
if username != "astaxie" {
|
|
|
|
t.Fatal("get username failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE
|
2020-08-30 15:39:07 +00:00
|
|
|
err = sess.Delete(nil, "username")
|
2020-08-06 09:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("delete username failed:", err)
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
username = sess.Get(nil, "username")
|
2020-08-06 09:14:36 +00:00
|
|
|
if username != nil {
|
|
|
|
t.Fatal("delete username failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// FLUSH
|
2020-08-30 15:39:07 +00:00
|
|
|
err = sess.Set(nil, "username", "astaxie")
|
2020-08-06 09:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("set failed:", err)
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
err = sess.Set(nil, "password", "1qaz2wsx")
|
2020-08-06 09:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("set failed:", err)
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
username = sess.Get(nil, "username")
|
2020-08-06 09:14:36 +00:00
|
|
|
if username != "astaxie" {
|
|
|
|
t.Fatal("get username failed")
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
password := sess.Get(nil, "password")
|
2020-08-06 09:14:36 +00:00
|
|
|
if password != "1qaz2wsx" {
|
|
|
|
t.Fatal("get password failed")
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
err = sess.Flush(nil)
|
2020-08-06 09:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("flush failed:", err)
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
username = sess.Get(nil, "username")
|
2020-08-06 09:14:36 +00:00
|
|
|
if username != nil {
|
|
|
|
t.Fatal("flush failed")
|
|
|
|
}
|
2020-08-30 15:39:07 +00:00
|
|
|
password = sess.Get(nil, "password")
|
2020-08-06 09:14:36 +00:00
|
|
|
if password != nil {
|
|
|
|
t.Fatal("flush failed")
|
|
|
|
}
|
|
|
|
|
2020-08-30 15:39:07 +00:00
|
|
|
sess.SessionRelease(nil, w)
|
2020-08-06 09:14:36 +00:00
|
|
|
}
|