diff --git a/session/sess_cookie.go b/session/sess_cookie.go index deff70a0..7962be18 100644 --- a/session/sess_cookie.go +++ b/session/sess_cookie.go @@ -105,12 +105,14 @@ func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error } func (pder *CookieProvider) SessionRead(sid string) (SessionStore, error) { - kv := make(map[interface{}]interface{}) - kv, _ = decodeCookie(pder.block, + maps, _ := decodeCookie(pder.block, pder.config.SecurityKey, pder.config.SecurityName, sid, pder.maxlifetime) - rs := &CookieSessionStore{sid: sid, values: kv} + if maps == nil { + maps = make(map[interface{}]interface{}) + } + rs := &CookieSessionStore{sid: sid, values: maps} return rs, nil } diff --git a/session/sess_cookie_test.go b/session/sess_cookie_test.go new file mode 100644 index 00000000..154c15a2 --- /dev/null +++ b/session/sess_cookie_test.go @@ -0,0 +1,38 @@ +package session + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestCookie(t *testing.T) { + config := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}` + globalSessions, err := NewManager("cookie", config) + if err != nil { + t.Fatal("init cookie session err", err) + } + r, _ := http.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + sess := globalSessions.SessionStart(w, r) + err = sess.Set("username", "astaxie") + if err != nil { + t.Fatal("set error,", err) + } + if username := sess.Get("username"); username != "astaxie" { + t.Fatal("get username error") + } + sess.SessionRelease(w) + if cookiestr := w.Header().Get("Set-Cookie"); cookiestr == "" { + t.Fatal("setcookie error") + } else { + parts := strings.Split(strings.TrimSpace(cookiestr), ";") + for k, v := range parts { + nameval := strings.Split(v, "=") + if k == 0 && nameval[0] != "gosessionid" { + t.Fatal("error") + } + } + } +} diff --git a/session/session.go b/session/session.go index 0447dac7..f41ba85b 100644 --- a/session/session.go +++ b/session/session.go @@ -85,9 +85,10 @@ func NewManager(provideName, config string) (*Manager, error) { if cf.Maxlifetime == 0 { cf.Maxlifetime = cf.Gclifetime } - - provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig) - + err = provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig) + if err != nil { + return nil, err + } if cf.SessionIDHashFunc == "" { cf.SessionIDHashFunc = "sha1" }