github.com/astaxie/beego@v1.12.3/session/sess_mem_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package session 16 17 import ( 18 "encoding/json" 19 "net/http" 20 "net/http/httptest" 21 "strings" 22 "testing" 23 ) 24 25 func TestMem(t *testing.T) { 26 config := `{"cookieName":"gosessionid","gclifetime":10, "enableSetCookie":true}` 27 conf := new(ManagerConfig) 28 if err := json.Unmarshal([]byte(config), conf); err != nil { 29 t.Fatal("json decode error", err) 30 } 31 globalSessions, _ := NewManager("memory", conf) 32 go globalSessions.GC() 33 r, _ := http.NewRequest("GET", "/", nil) 34 w := httptest.NewRecorder() 35 sess, err := globalSessions.SessionStart(w, r) 36 if err != nil { 37 t.Fatal("set error,", err) 38 } 39 defer sess.SessionRelease(w) 40 err = sess.Set("username", "astaxie") 41 if err != nil { 42 t.Fatal("set error,", err) 43 } 44 if username := sess.Get("username"); username != "astaxie" { 45 t.Fatal("get username error") 46 } 47 if cookiestr := w.Header().Get("Set-Cookie"); cookiestr == "" { 48 t.Fatal("setcookie error") 49 } else { 50 parts := strings.Split(strings.TrimSpace(cookiestr), ";") 51 for k, v := range parts { 52 nameval := strings.Split(v, "=") 53 if k == 0 && nameval[0] != "gosessionid" { 54 t.Fatal("error") 55 } 56 } 57 } 58 }