feat(session): support v1
support v1
This commit is contained in:
@@ -20,14 +20,16 @@
|
||||
//
|
||||
// Usage:
|
||||
// import(
|
||||
// _ "github.com/beego/beego/session/redis"
|
||||
// "github.com/beego/beego/session"
|
||||
//
|
||||
// _ "github.com/beego/beego/session/redis"
|
||||
// "github.com/beego/beego/session"
|
||||
//
|
||||
// )
|
||||
//
|
||||
// func init() {
|
||||
// globalSessions, _ = session.NewManager("redis", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"127.0.0.1:7070"}``)
|
||||
// go globalSessions.GC()
|
||||
// }
|
||||
// func init() {
|
||||
// globalSessions, _ = session.NewManager("redis", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"127.0.0.1:7070"}``)
|
||||
// go globalSessions.GC()
|
||||
// }
|
||||
//
|
||||
// more docs: http://beego.me/docs/module/session.md
|
||||
package redis
|
||||
@@ -99,13 +101,28 @@ func (rs *SessionStore) SessionID() string {
|
||||
|
||||
// SessionRelease save session values to redis
|
||||
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
b, err := session.EncodeGob(rs.values)
|
||||
rs.releaseSession(w, false)
|
||||
}
|
||||
|
||||
// SessionReleaseIfPresent save session values to redis when key is present
|
||||
func (rs *SessionStore) SessionReleaseIfPresent(w http.ResponseWriter) {
|
||||
rs.releaseSession(w, true)
|
||||
}
|
||||
|
||||
func (rs *SessionStore) releaseSession(_ http.ResponseWriter, requirePresent bool) {
|
||||
rs.lock.RLock()
|
||||
values := rs.values
|
||||
rs.lock.RUnlock()
|
||||
b, err := session.EncodeGob(values)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
c := rs.p.Get()
|
||||
defer c.Close()
|
||||
c.Do("SETEX", rs.sid, rs.maxlifetime, string(b))
|
||||
if requirePresent {
|
||||
c.Do("SETXX", rs.sid, string(b), time.Duration(rs.maxlifetime)*time.Second, "XX")
|
||||
} else {
|
||||
c.Do("SETEX", rs.sid, string(b), time.Duration(rs.maxlifetime)*time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// Provider redis session provider
|
||||
@@ -227,8 +244,8 @@ func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error)
|
||||
defer c.Close()
|
||||
|
||||
if existed, _ := redis.Int(c.Do("EXISTS", oldsid)); existed == 0 {
|
||||
// oldsid doesn't exists, set the new sid directly
|
||||
// ignore error here, since if it return error
|
||||
// oldsid doesn't exist, set the new sid directly
|
||||
// ignore error here, since if it returns error
|
||||
// the existed value will be 0
|
||||
c.Do("SET", sid, "", "EX", rp.maxlifetime)
|
||||
} else {
|
||||
|
||||
148
session/redis/session_redis_test.go
Normal file
148
session/redis/session_redis_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/beego/beego/session"
|
||||
)
|
||||
|
||||
func TestRedis(t *testing.T) {
|
||||
globalSession, err := setupSessionManager(t)
|
||||
if err != nil {
|
||||
t.Fatal(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)
|
||||
}
|
||||
defer sess.SessionRelease(w)
|
||||
|
||||
// SET AND GET
|
||||
err = sess.Set("username", "astaxie")
|
||||
if err != nil {
|
||||
t.Fatal("set username failed:", err)
|
||||
}
|
||||
username := sess.Get("username")
|
||||
if username != "astaxie" {
|
||||
t.Fatal("get username failed")
|
||||
}
|
||||
|
||||
// DELETE
|
||||
err = sess.Delete("username")
|
||||
if err != nil {
|
||||
t.Fatal("delete username failed:", err)
|
||||
}
|
||||
username = sess.Get("username")
|
||||
if username != nil {
|
||||
t.Fatal("delete username failed")
|
||||
}
|
||||
|
||||
// FLUSH
|
||||
err = sess.Set("username", "astaxie")
|
||||
if err != nil {
|
||||
t.Fatal("set failed:", err)
|
||||
}
|
||||
err = sess.Set("password", "1qaz2wsx")
|
||||
if err != nil {
|
||||
t.Fatal("set failed:", err)
|
||||
}
|
||||
username = sess.Get("username")
|
||||
if username != "astaxie" {
|
||||
t.Fatal("get username failed")
|
||||
}
|
||||
password := sess.Get("password")
|
||||
if password != "1qaz2wsx" {
|
||||
t.Fatal("get password failed")
|
||||
}
|
||||
err = sess.Flush()
|
||||
if err != nil {
|
||||
t.Fatal("flush failed:", err)
|
||||
}
|
||||
username = sess.Get("username")
|
||||
if username != nil {
|
||||
t.Fatal("flush failed")
|
||||
}
|
||||
password = sess.Get("password")
|
||||
if password != nil {
|
||||
t.Fatal("flush failed")
|
||||
}
|
||||
|
||||
sess.SessionRelease(w)
|
||||
}
|
||||
|
||||
func TestProvider_SessionInit(t *testing.T) {
|
||||
savePath := `
|
||||
{ "save_path": "my save path", "idle_timeout": "3s"}
|
||||
`
|
||||
cp := &Provider{}
|
||||
cp.SessionInit(12, savePath)
|
||||
assert.Equal(t, int64(12), cp.maxlifetime)
|
||||
}
|
||||
|
||||
func TestStoreSessionReleaseIfPresentAndSessionDestroy(t *testing.T) {
|
||||
globalSessions, err := setupSessionManager(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// todo test if e==nil
|
||||
go globalSessions.GC()
|
||||
|
||||
r, _ := http.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
sess, err := globalSessions.SessionStart(w, r)
|
||||
if err != nil {
|
||||
t.Fatal("session start failed:", err)
|
||||
}
|
||||
|
||||
if err := globalSessions.GetProvider().SessionDestroy(sess.SessionID()); err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
sess.SessionReleaseIfPresent(httptest.NewRecorder())
|
||||
}()
|
||||
wg.Wait()
|
||||
|
||||
if globalSessions.GetProvider().SessionExist(sess.SessionID()) {
|
||||
t.Fatalf("session %s should exist", sess.SessionID())
|
||||
}
|
||||
}
|
||||
|
||||
func setupSessionManager(t *testing.T) (*session.Manager, error) {
|
||||
redisAddr := os.Getenv("REDIS_ADDR")
|
||||
if redisAddr == "" {
|
||||
redisAddr = "127.0.0.1:6379"
|
||||
}
|
||||
redisConfig := fmt.Sprintf("%s,100,,0,30", redisAddr)
|
||||
sessionConfig := &session.ManagerConfig{}
|
||||
sessionConfig.CookieName = "gosessionid"
|
||||
sessionConfig.EnableSetCookie = true
|
||||
sessionConfig.Gclifetime = 3600
|
||||
sessionConfig.Maxlifetime = 3600
|
||||
sessionConfig.Secure = false
|
||||
sessionConfig.CookieLifeTime = 3600
|
||||
sessionConfig.ProviderConfig = redisConfig
|
||||
globalSessions, err := session.NewManager("redis", sessionConfig)
|
||||
if err != nil {
|
||||
t.Log("could not create manager: ", err)
|
||||
return nil, err
|
||||
}
|
||||
return globalSessions, nil
|
||||
}
|
||||
Reference in New Issue
Block a user