sess_mem_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. package session
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "net/http/httptest"
  19. "strings"
  20. "testing"
  21. )
  22. func TestMem(t *testing.T) {
  23. config := `{"cookieName":"gosessionid","gclifetime":10, "enableSetCookie":true}`
  24. conf := new(ManagerConfig)
  25. if err := json.Unmarshal([]byte(config), conf); err != nil {
  26. t.Fatal("json decode error", err)
  27. }
  28. globalSessions, _ := NewManager("memory", conf)
  29. go globalSessions.GC()
  30. r, _ := http.NewRequest("GET", "/", nil)
  31. w := httptest.NewRecorder()
  32. sess, err := globalSessions.SessionStart(w, r)
  33. if err != nil {
  34. t.Fatal("set error,", err)
  35. }
  36. defer sess.SessionRelease(w)
  37. err = sess.Set("username", "astaxie")
  38. if err != nil {
  39. t.Fatal("set error,", err)
  40. }
  41. if username := sess.Get("username"); username != "astaxie" {
  42. t.Fatal("get username error")
  43. }
  44. if cookiestr := w.Header().Get("Set-Cookie"); cookiestr == "" {
  45. t.Fatal("setcookie error")
  46. } else {
  47. parts := strings.Split(strings.TrimSpace(cookiestr), ";")
  48. for k, v := range parts {
  49. nameval := strings.Split(v, "=")
  50. if k == 0 && nameval[0] != "gosessionid" {
  51. t.Fatal("error")
  52. }
  53. }
  54. }
  55. }