session_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. }
  168. func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
  169. // Set default values
  170. request, err := http.NewRequest("GET", "/set", nil)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. recorder := httptest.NewRecorder()
  175. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  176. sess := New(w, r, "./../tmp")
  177. defer sess.Close()
  178. if r.URL.Path == "/set" {
  179. sess.SetBool("some_bool", false)
  180. sess.SetInt("some_int", 0)
  181. sess.SetString("some_str", "")
  182. w.Write([]byte(`ok`))
  183. } else {
  184. w.WriteHeader(http.StatusNotFound)
  185. w.Write([]byte(`404`))
  186. }
  187. }).ServeHTTP(recorder, request)
  188. if recorder.Body.String() != "ok" {
  189. t.Fatalf("bad body response, not match")
  190. }
  191. // Remember session id
  192. var sessId string
  193. if len(recorder.Result().Cookies()) > 0 {
  194. sessId = recorder.Result().Cookies()[0].Value
  195. }
  196. if sessId == "" {
  197. t.Fatalf("session identifier is not defined")
  198. }
  199. // Get value
  200. request, err = http.NewRequest("GET", "/get", nil)
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. request.Header.Set("Cookie", "session="+sessId)
  205. recorder = httptest.NewRecorder()
  206. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  207. sess := New(w, r, "./../tmp")
  208. defer sess.Close()
  209. if r.URL.Path == "/get" {
  210. w.Write([]byte(fmt.Sprintf(
  211. "(%v)(%v)(%v)",
  212. sess.GetBool("some_bool", false),
  213. sess.GetInt("some_int", 0),
  214. sess.GetString("some_str", ""),
  215. )))
  216. } else {
  217. w.WriteHeader(http.StatusNotFound)
  218. w.Write([]byte(`404`))
  219. }
  220. }).ServeHTTP(recorder, request)
  221. if recorder.Body.String() != "(false)(0)()" {
  222. t.Fatalf("bad body response, not match")
  223. }
  224. // Check session file
  225. fname := "./../tmp" + string(os.PathSeparator) + sessId
  226. _, err = ioutil.ReadFile(fname)
  227. if err == nil {
  228. _ = os.Remove(fname)
  229. t.Fatalf("session file in tmp folder do not must exists")
  230. }
  231. }
  232. func TestSessionDestroy(t *testing.T) {
  233. // Set value
  234. request, err := http.NewRequest("GET", "/set", nil)
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. recorder := httptest.NewRecorder()
  239. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  240. sess := New(w, r, "./../tmp")
  241. defer sess.Close()
  242. if r.URL.Path == "/set" {
  243. sess.SetInt("some_var", 1)
  244. w.Write([]byte(`ok`))
  245. } else {
  246. w.WriteHeader(http.StatusNotFound)
  247. w.Write([]byte(`404`))
  248. }
  249. }).ServeHTTP(recorder, request)
  250. if recorder.Body.String() != "ok" {
  251. t.Fatalf("bad body response, not match")
  252. }
  253. // Remember session id
  254. var sessId string
  255. if len(recorder.Result().Cookies()) > 0 {
  256. sessId = recorder.Result().Cookies()[0].Value
  257. }
  258. if sessId == "" {
  259. t.Fatalf("session identifier is not defined")
  260. }
  261. // Get value
  262. request, err = http.NewRequest("GET", "/get", nil)
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. request.Header.Set("Cookie", "session="+sessId)
  267. recorder = httptest.NewRecorder()
  268. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  269. sess := New(w, r, "./../tmp")
  270. defer sess.Close()
  271. if r.URL.Path == "/get" {
  272. w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0))))
  273. } else {
  274. w.WriteHeader(http.StatusNotFound)
  275. w.Write([]byte(`404`))
  276. }
  277. }).ServeHTTP(recorder, request)
  278. if recorder.Body.String() != "1" {
  279. t.Fatalf("bad body response, not match")
  280. }
  281. // Check destroy
  282. request, err = http.NewRequest("GET", "/get", nil)
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. request.Header.Set("Cookie", "session="+sessId)
  287. recorder = httptest.NewRecorder()
  288. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  289. sess := New(w, r, "./../tmp")
  290. defer sess.Close()
  291. if r.URL.Path == "/get" {
  292. sess.SetInt("some_var", 2)
  293. err := sess.Destroy()
  294. if err == nil {
  295. w.Write([]byte(`OK`))
  296. } else {
  297. w.Write([]byte(`ERROR`))
  298. }
  299. } else {
  300. w.WriteHeader(http.StatusNotFound)
  301. w.Write([]byte(`404`))
  302. }
  303. }).ServeHTTP(recorder, request)
  304. if recorder.Body.String() != "OK" {
  305. t.Fatalf("bad body response, not match")
  306. }
  307. // Check session file
  308. fname := "./../tmp" + string(os.PathSeparator) + sessId
  309. _, err = ioutil.ReadFile(fname)
  310. if err == nil {
  311. _ = os.Remove(fname)
  312. t.Fatalf("session file in tmp folder do not must exists")
  313. }
  314. }