session_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package session
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "net/http/httptest"
  7. "os"
  8. "testing"
  9. )
  10. var SessionId string = ""
  11. func TestSessionBool(t *testing.T) {
  12. // Set value
  13. request, err := http.NewRequest("GET", "/set", nil)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. recorder := httptest.NewRecorder()
  18. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. sess := New(w, r, "./../tmp")
  20. defer sess.Close()
  21. if r.URL.Path == "/set" {
  22. sess.SetBool("some_bool", true)
  23. w.Write([]byte(`ok`))
  24. } else {
  25. w.WriteHeader(http.StatusNotFound)
  26. w.Write([]byte(`404`))
  27. }
  28. }).ServeHTTP(recorder, request)
  29. if recorder.Body.String() != "ok" {
  30. t.Fatalf("bad body response, not match")
  31. }
  32. // Remember session id
  33. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  34. SessionId = recorder.Result().Cookies()[0].Value
  35. }
  36. // Get value
  37. request, err = http.NewRequest("GET", "/get", nil)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. request.Header.Set("Cookie", "session="+SessionId)
  42. recorder = httptest.NewRecorder()
  43. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  44. sess := New(w, r, "./../tmp")
  45. defer sess.Close()
  46. if r.URL.Path == "/get" {
  47. w.Write([]byte(fmt.Sprintf("%v", sess.GetBool("some_bool", false))))
  48. } else {
  49. w.WriteHeader(http.StatusNotFound)
  50. w.Write([]byte(`404`))
  51. }
  52. }).ServeHTTP(recorder, request)
  53. if recorder.Body.String() != "true" {
  54. t.Fatalf("bad body response, not match")
  55. }
  56. }
  57. func TestSessionInt(t *testing.T) {
  58. // Set value
  59. request, err := http.NewRequest("GET", "/set", nil)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. request.Header.Set("Cookie", "session="+SessionId)
  64. recorder := httptest.NewRecorder()
  65. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  66. sess := New(w, r, "./../tmp")
  67. defer sess.Close()
  68. if r.URL.Path == "/set" {
  69. sess.SetInt("some_int", 5)
  70. w.Write([]byte(`ok`))
  71. } else {
  72. w.WriteHeader(http.StatusNotFound)
  73. w.Write([]byte(`404`))
  74. }
  75. }).ServeHTTP(recorder, request)
  76. if recorder.Body.String() != "ok" {
  77. t.Fatalf("bad body response, not match")
  78. }
  79. // Remember session id
  80. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  81. SessionId = recorder.Result().Cookies()[0].Value
  82. }
  83. // Get value
  84. request, err = http.NewRequest("GET", "/get", nil)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. request.Header.Set("Cookie", "session="+SessionId)
  89. recorder = httptest.NewRecorder()
  90. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  91. sess := New(w, r, "./../tmp")
  92. defer sess.Close()
  93. if r.URL.Path == "/get" {
  94. w.Write([]byte(fmt.Sprintf("%d", sess.GetInt("some_int", 0))))
  95. } else {
  96. w.WriteHeader(http.StatusNotFound)
  97. w.Write([]byte(`404`))
  98. }
  99. }).ServeHTTP(recorder, request)
  100. if recorder.Body.String() != "5" {
  101. t.Fatalf("bad body response, not match")
  102. }
  103. }
  104. func TestSessionString(t *testing.T) {
  105. // Set value
  106. request, err := http.NewRequest("GET", "/set", nil)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. request.Header.Set("Cookie", "session="+SessionId)
  111. recorder := httptest.NewRecorder()
  112. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  113. sess := New(w, r, "./../tmp")
  114. defer sess.Close()
  115. if r.URL.Path == "/set" {
  116. sess.SetString("some_str", "test")
  117. w.Write([]byte(`ok`))
  118. } else {
  119. w.WriteHeader(http.StatusNotFound)
  120. w.Write([]byte(`404`))
  121. }
  122. }).ServeHTTP(recorder, request)
  123. if recorder.Body.String() != "ok" {
  124. t.Fatalf("bad body response, not match")
  125. }
  126. // Remember session id
  127. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  128. SessionId = recorder.Result().Cookies()[0].Value
  129. }
  130. // Get value
  131. request, err = http.NewRequest("GET", "/get", nil)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. request.Header.Set("Cookie", "session="+SessionId)
  136. recorder = httptest.NewRecorder()
  137. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  138. sess := New(w, r, "./../tmp")
  139. defer sess.Close()
  140. if r.URL.Path == "/get" {
  141. w.Write([]byte(fmt.Sprintf("%s", sess.GetString("some_str", ""))))
  142. } else {
  143. w.WriteHeader(http.StatusNotFound)
  144. w.Write([]byte(`404`))
  145. }
  146. }).ServeHTTP(recorder, request)
  147. if recorder.Body.String() != "test" {
  148. t.Fatalf("bad body response, not match")
  149. }
  150. }
  151. func TestSessionActualFile(t *testing.T) {
  152. if SessionId == "" {
  153. t.Fatal("SessionId is empty")
  154. }
  155. fname := "./../tmp" + string(os.PathSeparator) + SessionId
  156. bytes, err := ioutil.ReadFile(fname)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. if string(bytes) != `{"Bool":{"some_bool":true},"Int":{"some_int":5},"String":{"some_str":"test"}}` {
  161. t.Fatal("actual file content, not match")
  162. }
  163. err = os.Remove(fname)
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. }