session_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package user
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. )
  7. func TestDefaultSession(t *testing.T) {
  8. ds := NewDefaultSession("test", "user1")
  9. if res := ds.ID(); res != "test" {
  10. t.Error("Unexpected id:", res)
  11. return
  12. }
  13. ds.Set("key1", "value1")
  14. if res, ok := ds.Get("key1"); !ok || res != "value1" {
  15. t.Error("Unexpected result:", res)
  16. return
  17. }
  18. if res := ds.GetAll(); fmt.Sprint(res) != "map[key1:value1]" {
  19. t.Error("Unexpected result:", res)
  20. return
  21. }
  22. if res := fmt.Sprint(ds); res != "Session: test (User:user1 Values:map[key1:value1])" {
  23. t.Error("Unexpected result:", res)
  24. return
  25. }
  26. }
  27. func TestSessionCreation(t *testing.T) {
  28. handleCallback = func(w http.ResponseWriter, r *http.Request) {
  29. param := r.URL.Query()
  30. name, ok := param["user"]
  31. if ok {
  32. // Register the new user
  33. UserSessionManager.GetSession(name[0], w, r, true)
  34. if hasCookie, isActive := UserSessionManager.CheckSessionCookie(r); !hasCookie || !isActive {
  35. t.Error("Unexpected result:", hasCookie, isActive)
  36. return
  37. }
  38. }
  39. session, _ := UserSessionManager.GetSession("", w, r, false)
  40. _, ok = param["logout"]
  41. if ok && session != nil {
  42. if hasCookie, isActive := UserSessionManager.CheckSessionCookie(r); !hasCookie || !isActive {
  43. t.Error("Unexpected result:", hasCookie, isActive)
  44. return
  45. }
  46. UserSessionManager.RemoveSessionCookie(w)
  47. UserSessionManager.Provider.Destroy(session.ID())
  48. }
  49. }
  50. res, resp := sendTestRequest(TESTQUERYURL+"?user=fred", "GET", nil, nil, nil)
  51. if res != "Content" {
  52. t.Error("Unexpected response:", res)
  53. return
  54. }
  55. // Check we have a valid session
  56. cookies := resp.Cookies()
  57. sessions, _ := UserSessionManager.Provider.GetAll()
  58. if len(sessions) != 1 {
  59. t.Error("Unexpected number of active sessions:", sessions)
  60. return
  61. }
  62. var theSession Session
  63. for _, v := range sessions {
  64. theSession = v.(Session)
  65. break
  66. }
  67. if len(cookies) != 1 ||
  68. cookies[0].Raw != fmt.Sprintf("~sid=%v; Path=/; Max-Age=%v; HttpOnly",
  69. theSession.ID(), CookieMaxLifetime) {
  70. t.Error("Unexpected cookie:", cookies)
  71. return
  72. }
  73. // The next request will have access to a session
  74. res, resp = sendTestRequest(TESTQUERYURL, "GET", nil, cookies, nil)
  75. if res != "Content - User session: fred" {
  76. t.Error("Unexpected result:", res)
  77. return
  78. }
  79. session, _ := UserSessionManager.GetSession("", nil, resp.Request, false)
  80. if session == nil {
  81. t.Error("Unexpected result")
  82. return
  83. }
  84. res, resp = sendTestRequest(TESTQUERYURL+"?logout=1", "GET", nil, cookies, nil)
  85. cookies = resp.Cookies()
  86. if res != "Content - User session: fred" {
  87. t.Error("Unexpected result:", res)
  88. return
  89. }
  90. if len(cookies) != 1 ||
  91. fmt.Sprint(cookies[0].Raw) != "~sid=; Path=/; Max-Age=0; HttpOnly" {
  92. t.Error("Unexpected cookie:", cookies[0])
  93. return
  94. }
  95. // Check the user is no longer identified in the session
  96. res, resp = sendTestRequest(TESTQUERYURL, "GET", nil, cookies, nil)
  97. if res != "Content" {
  98. t.Error("Unexpected result:", res)
  99. return
  100. }
  101. // We can still see the cookie in the session but it has now an invalid value
  102. if hasCookie, isActive := UserSessionManager.CheckSessionCookie(resp.Request); !hasCookie || isActive {
  103. t.Error("Unexpected result:", hasCookie, isActive)
  104. return
  105. }
  106. session, err := UserSessionManager.GetSession("", nil, resp.Request, false)
  107. if session != nil || err != nil {
  108. t.Error("Unexpected result:", err)
  109. return
  110. }
  111. }