session_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. if _, err := w.Write([]byte(`ok`)); err != nil {
  24. fmt.Printf("%s\n", err.Error())
  25. }
  26. } else {
  27. w.WriteHeader(http.StatusNotFound)
  28. if _, err := w.Write([]byte(`404`)); err != nil {
  29. fmt.Printf("%s\n", err.Error())
  30. }
  31. }
  32. }).ServeHTTP(recorder, request)
  33. if recorder.Body.String() != "ok" {
  34. t.Fatalf("bad body response, not match")
  35. }
  36. // Remember session id
  37. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  38. SessionId = recorder.Result().Cookies()[0].Value
  39. }
  40. // Get value
  41. request, err = http.NewRequest("GET", "/get", nil)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. request.Header.Set("Cookie", "session="+SessionId)
  46. recorder = httptest.NewRecorder()
  47. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  48. sess := New(w, r, "./../tmp")
  49. defer sess.Close()
  50. if r.URL.Path == "/get" {
  51. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetBool("some_bool", false)))); err != nil {
  52. fmt.Printf("%s\n", err.Error())
  53. }
  54. } else {
  55. w.WriteHeader(http.StatusNotFound)
  56. if _, err := w.Write([]byte(`404`)); err != nil {
  57. fmt.Printf("%s\n", err.Error())
  58. }
  59. }
  60. }).ServeHTTP(recorder, request)
  61. if recorder.Body.String() != "true" {
  62. t.Fatalf("bad body response, not match")
  63. }
  64. }
  65. func TestSessionInt(t *testing.T) {
  66. // Set value
  67. request, err := http.NewRequest("GET", "/set", nil)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. request.Header.Set("Cookie", "session="+SessionId)
  72. recorder := httptest.NewRecorder()
  73. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  74. sess := New(w, r, "./../tmp")
  75. defer sess.Close()
  76. if r.URL.Path == "/set" {
  77. sess.SetInt("some_int", 5)
  78. if _, err := w.Write([]byte(`ok`)); err != nil {
  79. fmt.Printf("%s\n", err.Error())
  80. }
  81. } else {
  82. w.WriteHeader(http.StatusNotFound)
  83. if _, err := w.Write([]byte(`404`)); err != nil {
  84. fmt.Printf("%s\n", err.Error())
  85. }
  86. }
  87. }).ServeHTTP(recorder, request)
  88. if recorder.Body.String() != "ok" {
  89. t.Fatalf("bad body response, not match")
  90. }
  91. // Remember session id
  92. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  93. SessionId = recorder.Result().Cookies()[0].Value
  94. }
  95. // Get value
  96. request, err = http.NewRequest("GET", "/get", nil)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. request.Header.Set("Cookie", "session="+SessionId)
  101. recorder = httptest.NewRecorder()
  102. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  103. sess := New(w, r, "./../tmp")
  104. defer sess.Close()
  105. if r.URL.Path == "/get" {
  106. if _, err := w.Write([]byte(fmt.Sprintf("%d", sess.GetInt("some_int", 0)))); err != nil {
  107. fmt.Printf("%s\n", err.Error())
  108. }
  109. } else {
  110. w.WriteHeader(http.StatusNotFound)
  111. if _, err := w.Write([]byte(`404`)); err != nil {
  112. fmt.Printf("%s\n", err.Error())
  113. }
  114. }
  115. }).ServeHTTP(recorder, request)
  116. if recorder.Body.String() != "5" {
  117. t.Fatalf("bad body response, not match")
  118. }
  119. }
  120. func TestSessionString(t *testing.T) {
  121. // Set value
  122. request, err := http.NewRequest("GET", "/set", nil)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. request.Header.Set("Cookie", "session="+SessionId)
  127. recorder := httptest.NewRecorder()
  128. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  129. sess := New(w, r, "./../tmp")
  130. defer sess.Close()
  131. if r.URL.Path == "/set" {
  132. sess.SetString("some_str", "test")
  133. if _, err := w.Write([]byte(`ok`)); err != nil {
  134. fmt.Printf("%s\n", err.Error())
  135. }
  136. } else {
  137. w.WriteHeader(http.StatusNotFound)
  138. if _, err := w.Write([]byte(`404`)); err != nil {
  139. fmt.Printf("%s\n", err.Error())
  140. }
  141. }
  142. }).ServeHTTP(recorder, request)
  143. if recorder.Body.String() != "ok" {
  144. t.Fatalf("bad body response, not match")
  145. }
  146. // Remember session id
  147. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  148. SessionId = recorder.Result().Cookies()[0].Value
  149. }
  150. // Get value
  151. request, err = http.NewRequest("GET", "/get", nil)
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. request.Header.Set("Cookie", "session="+SessionId)
  156. recorder = httptest.NewRecorder()
  157. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  158. sess := New(w, r, "./../tmp")
  159. defer sess.Close()
  160. if r.URL.Path == "/get" {
  161. if _, err := w.Write([]byte(sess.GetString("some_str", ""))); err != nil {
  162. fmt.Printf("%s\n", err.Error())
  163. }
  164. } else {
  165. w.WriteHeader(http.StatusNotFound)
  166. if _, err := w.Write([]byte(`404`)); err != nil {
  167. fmt.Printf("%s\n", err.Error())
  168. }
  169. }
  170. }).ServeHTTP(recorder, request)
  171. if recorder.Body.String() != "test" {
  172. t.Fatalf("bad body response, not match")
  173. }
  174. }
  175. func TestSessionActualFile(t *testing.T) {
  176. if SessionId == "" {
  177. t.Fatal("SessionId is empty")
  178. }
  179. fname := "./../tmp" + string(os.PathSeparator) + SessionId
  180. bytes, err := ioutil.ReadFile(fname)
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. if string(bytes) != `{"Bool":{"some_bool":true},"Int":{"some_int":5},"String":{"some_str":"test"}}` {
  185. t.Fatal("actual file content, not match")
  186. }
  187. err = os.Remove(fname)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. }
  192. func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
  193. // Set default values
  194. request, err := http.NewRequest("GET", "/set", nil)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. recorder := httptest.NewRecorder()
  199. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  200. sess := New(w, r, "./../tmp")
  201. defer sess.Close()
  202. if r.URL.Path == "/set" {
  203. sess.SetBool("some_bool", false)
  204. sess.SetInt("some_int", 0)
  205. sess.SetString("some_str", "")
  206. if _, err := w.Write([]byte(`ok`)); err != nil {
  207. fmt.Printf("%s\n", err.Error())
  208. }
  209. } else {
  210. w.WriteHeader(http.StatusNotFound)
  211. if _, err := w.Write([]byte(`404`)); err != nil {
  212. fmt.Printf("%s\n", err.Error())
  213. }
  214. }
  215. }).ServeHTTP(recorder, request)
  216. if recorder.Body.String() != "ok" {
  217. t.Fatalf("bad body response, not match")
  218. }
  219. // Remember session id
  220. var sessId string
  221. if len(recorder.Result().Cookies()) > 0 {
  222. sessId = recorder.Result().Cookies()[0].Value
  223. }
  224. if sessId == "" {
  225. t.Fatalf("session identifier is not defined")
  226. }
  227. // Get value
  228. request, err = http.NewRequest("GET", "/get", nil)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. request.Header.Set("Cookie", "session="+sessId)
  233. recorder = httptest.NewRecorder()
  234. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  235. sess := New(w, r, "./../tmp")
  236. defer sess.Close()
  237. if r.URL.Path == "/get" {
  238. if _, err := w.Write([]byte(fmt.Sprintf(
  239. "(%v)(%v)(%v)",
  240. sess.GetBool("some_bool", false),
  241. sess.GetInt("some_int", 0),
  242. sess.GetString("some_str", ""),
  243. ))); err != nil {
  244. fmt.Printf("%s\n", err.Error())
  245. }
  246. } else {
  247. w.WriteHeader(http.StatusNotFound)
  248. if _, err := w.Write([]byte(`404`)); err != nil {
  249. fmt.Printf("%s\n", err.Error())
  250. }
  251. }
  252. }).ServeHTTP(recorder, request)
  253. if recorder.Body.String() != "(false)(0)()" {
  254. t.Fatalf("bad body response, not match")
  255. }
  256. // Check session file
  257. fname := "./../tmp" + string(os.PathSeparator) + sessId
  258. _, err = ioutil.ReadFile(fname)
  259. if err == nil {
  260. _ = os.Remove(fname)
  261. t.Fatalf("session file in tmp folder do not must exists")
  262. }
  263. }
  264. func TestSessionDestroy(t *testing.T) {
  265. // Set value
  266. request, err := http.NewRequest("GET", "/set", nil)
  267. if err != nil {
  268. t.Fatal(err)
  269. }
  270. recorder := httptest.NewRecorder()
  271. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  272. sess := New(w, r, "./../tmp")
  273. defer sess.Close()
  274. if r.URL.Path == "/set" {
  275. sess.SetInt("some_var", 1)
  276. if _, err := w.Write([]byte(`ok`)); err != nil {
  277. fmt.Printf("%s\n", err.Error())
  278. }
  279. } else {
  280. w.WriteHeader(http.StatusNotFound)
  281. if _, err := w.Write([]byte(`404`)); err != nil {
  282. fmt.Printf("%s\n", err.Error())
  283. }
  284. }
  285. }).ServeHTTP(recorder, request)
  286. if recorder.Body.String() != "ok" {
  287. t.Fatalf("bad body response, not match")
  288. }
  289. // Remember session id
  290. var sessId string
  291. if len(recorder.Result().Cookies()) > 0 {
  292. sessId = recorder.Result().Cookies()[0].Value
  293. }
  294. if sessId == "" {
  295. t.Fatalf("session identifier is not defined")
  296. }
  297. // Get value
  298. request, err = http.NewRequest("GET", "/get", nil)
  299. if err != nil {
  300. t.Fatal(err)
  301. }
  302. request.Header.Set("Cookie", "session="+sessId)
  303. recorder = httptest.NewRecorder()
  304. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  305. sess := New(w, r, "./../tmp")
  306. defer sess.Close()
  307. if r.URL.Path == "/get" {
  308. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0)))); err != nil {
  309. fmt.Printf("%s\n", err.Error())
  310. }
  311. } else {
  312. w.WriteHeader(http.StatusNotFound)
  313. if _, err := w.Write([]byte(`404`)); err != nil {
  314. fmt.Printf("%s\n", err.Error())
  315. }
  316. }
  317. }).ServeHTTP(recorder, request)
  318. if recorder.Body.String() != "1" {
  319. t.Fatalf("bad body response, not match")
  320. }
  321. // Check destroy
  322. request, err = http.NewRequest("GET", "/get", nil)
  323. if err != nil {
  324. t.Fatal(err)
  325. }
  326. request.Header.Set("Cookie", "session="+sessId)
  327. recorder = httptest.NewRecorder()
  328. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  329. sess := New(w, r, "./../tmp")
  330. defer sess.Close()
  331. if r.URL.Path == "/get" {
  332. sess.SetInt("some_var", 2)
  333. err := sess.Destroy()
  334. if err == nil {
  335. if _, err := w.Write([]byte(`OK`)); err != nil {
  336. fmt.Printf("%s\n", err.Error())
  337. }
  338. } else {
  339. if _, err := w.Write([]byte(`ERROR`)); err != nil {
  340. fmt.Printf("%s\n", err.Error())
  341. }
  342. }
  343. } else {
  344. w.WriteHeader(http.StatusNotFound)
  345. if _, err := w.Write([]byte(`404`)); err != nil {
  346. fmt.Printf("%s\n", err.Error())
  347. }
  348. }
  349. }).ServeHTTP(recorder, request)
  350. if recorder.Body.String() != "OK" {
  351. t.Fatalf("bad body response, not match")
  352. }
  353. // Check session file
  354. fname := "./../tmp" + string(os.PathSeparator) + sessId
  355. _, err = ioutil.ReadFile(fname)
  356. if err == nil {
  357. _ = os.Remove(fname)
  358. t.Fatalf("session file in tmp folder do not must exists")
  359. }
  360. }