mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 17:00:54 +00:00
add cookie test
This commit is contained in:
parent
d7f2c738c8
commit
e34f8c4634
@ -105,12 +105,14 @@ func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pder *CookieProvider) SessionRead(sid string) (SessionStore, error) {
|
func (pder *CookieProvider) SessionRead(sid string) (SessionStore, error) {
|
||||||
kv := make(map[interface{}]interface{})
|
maps, _ := decodeCookie(pder.block,
|
||||||
kv, _ = decodeCookie(pder.block,
|
|
||||||
pder.config.SecurityKey,
|
pder.config.SecurityKey,
|
||||||
pder.config.SecurityName,
|
pder.config.SecurityName,
|
||||||
sid, pder.maxlifetime)
|
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
|
return rs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38
session/sess_cookie_test.go
Normal file
38
session/sess_cookie_test.go
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -85,9 +85,10 @@ func NewManager(provideName, config string) (*Manager, error) {
|
|||||||
if cf.Maxlifetime == 0 {
|
if cf.Maxlifetime == 0 {
|
||||||
cf.Maxlifetime = cf.Gclifetime
|
cf.Maxlifetime = cf.Gclifetime
|
||||||
}
|
}
|
||||||
|
err = provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig)
|
||||||
provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if cf.SessionIDHashFunc == "" {
|
if cf.SessionIDHashFunc == "" {
|
||||||
cf.SessionIDHashFunc = "sha1"
|
cf.SessionIDHashFunc = "sha1"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user