e34f8c46 by astaxie

add cookie test

1 parent d7f2c738
...@@ -105,12 +105,14 @@ func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error ...@@ -105,12 +105,14 @@ func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error
105 } 105 }
106 106
107 func (pder *CookieProvider) SessionRead(sid string) (SessionStore, error) { 107 func (pder *CookieProvider) SessionRead(sid string) (SessionStore, error) {
108 kv := make(map[interface{}]interface{}) 108 maps, _ := decodeCookie(pder.block,
109 kv, _ = decodeCookie(pder.block,
110 pder.config.SecurityKey, 109 pder.config.SecurityKey,
111 pder.config.SecurityName, 110 pder.config.SecurityName,
112 sid, pder.maxlifetime) 111 sid, pder.maxlifetime)
113 rs := &CookieSessionStore{sid: sid, values: kv} 112 if maps == nil {
113 maps = make(map[interface{}]interface{})
114 }
115 rs := &CookieSessionStore{sid: sid, values: maps}
114 return rs, nil 116 return rs, nil
115 } 117 }
116 118
......
1 package session
2
3 import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8 )
9
10 func TestCookie(t *testing.T) {
11 config := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
12 globalSessions, err := NewManager("cookie", config)
13 if err != nil {
14 t.Fatal("init cookie session err", err)
15 }
16 r, _ := http.NewRequest("GET", "/", nil)
17 w := httptest.NewRecorder()
18 sess := globalSessions.SessionStart(w, r)
19 err = sess.Set("username", "astaxie")
20 if err != nil {
21 t.Fatal("set error,", err)
22 }
23 if username := sess.Get("username"); username != "astaxie" {
24 t.Fatal("get username error")
25 }
26 sess.SessionRelease(w)
27 if cookiestr := w.Header().Get("Set-Cookie"); cookiestr == "" {
28 t.Fatal("setcookie error")
29 } else {
30 parts := strings.Split(strings.TrimSpace(cookiestr), ";")
31 for k, v := range parts {
32 nameval := strings.Split(v, "=")
33 if k == 0 && nameval[0] != "gosessionid" {
34 t.Fatal("error")
35 }
36 }
37 }
38 }
...@@ -85,9 +85,10 @@ func NewManager(provideName, config string) (*Manager, error) { ...@@ -85,9 +85,10 @@ func NewManager(provideName, config string) (*Manager, error) {
85 if cf.Maxlifetime == 0 { 85 if cf.Maxlifetime == 0 {
86 cf.Maxlifetime = cf.Gclifetime 86 cf.Maxlifetime = cf.Gclifetime
87 } 87 }
88 88 err = provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig)
89 provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig) 89 if err != nil {
90 90 return nil, err
91 }
91 if cf.SessionIDHashFunc == "" { 92 if cf.SessionIDHashFunc == "" {
92 cf.SessionIDHashFunc = "sha1" 93 cf.SessionIDHashFunc = "sha1"
93 } 94 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!