sess_cookie_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 TestCookie(t *testing.T) {
  23. config := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
  24. conf := new(ManagerConfig)
  25. if err := json.Unmarshal([]byte(config), conf); err != nil {
  26. t.Fatal("json decode error", err)
  27. }
  28. globalSessions, err := NewManager("cookie", conf)
  29. if err != nil {
  30. t.Fatal("init cookie session err", err)
  31. }
  32. r, _ := http.NewRequest("GET", "/", nil)
  33. w := httptest.NewRecorder()
  34. sess, err := globalSessions.SessionStart(w, r)
  35. if err != nil {
  36. t.Fatal("set error,", err)
  37. }
  38. err = sess.Set("username", "astaxie")
  39. if err != nil {
  40. t.Fatal("set error,", err)
  41. }
  42. if username := sess.Get("username"); username != "astaxie" {
  43. t.Fatal("get username error")
  44. }
  45. sess.SessionRelease(w)
  46. if cookiestr := w.Header().Get("Set-Cookie"); cookiestr == "" {
  47. t.Fatal("setcookie error")
  48. } else {
  49. parts := strings.Split(strings.TrimSpace(cookiestr), ";")
  50. for k, v := range parts {
  51. nameval := strings.Split(v, "=")
  52. if k == 0 && nameval[0] != "gosessionid" {
  53. t.Fatal("error")
  54. }
  55. }
  56. }
  57. }
  58. func TestDestorySessionCookie(t *testing.T) {
  59. config := `{"cookieName":"gosessionid","enableSetCookie":true,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
  60. conf := new(ManagerConfig)
  61. if err := json.Unmarshal([]byte(config), conf); err != nil {
  62. t.Fatal("json decode error", err)
  63. }
  64. globalSessions, err := NewManager("cookie", conf)
  65. if err != nil {
  66. t.Fatal("init cookie session err", err)
  67. }
  68. r, _ := http.NewRequest("GET", "/", nil)
  69. w := httptest.NewRecorder()
  70. session, err := globalSessions.SessionStart(w, r)
  71. if err != nil {
  72. t.Fatal("session start err,", err)
  73. }
  74. // request again ,will get same sesssion id .
  75. r1, _ := http.NewRequest("GET", "/", nil)
  76. r1.Header.Set("Cookie", w.Header().Get("Set-Cookie"))
  77. w = httptest.NewRecorder()
  78. newSession, err := globalSessions.SessionStart(w, r1)
  79. if err != nil {
  80. t.Fatal("session start err,", err)
  81. }
  82. if newSession.SessionID() != session.SessionID() {
  83. t.Fatal("get cookie session id is not the same again.")
  84. }
  85. // After destroy session , will get a new session id .
  86. globalSessions.SessionDestroy(w, r1)
  87. r2, _ := http.NewRequest("GET", "/", nil)
  88. r2.Header.Set("Cookie", w.Header().Get("Set-Cookie"))
  89. w = httptest.NewRecorder()
  90. newSession, err = globalSessions.SessionStart(w, r2)
  91. if err != nil {
  92. t.Fatal("session start error")
  93. }
  94. if newSession.SessionID() == session.SessionID() {
  95. t.Fatal("after destroy session and reqeust again ,get cookie session id is same.")
  96. }
  97. }