session_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 TestSessionInt64(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.SetInt64("some_int64", 10)
  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(fmt.Sprintf("%d", sess.GetInt64("some_int64", 0)))); 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() != "10" {
  172. t.Fatalf("bad body response, not match")
  173. }
  174. }
  175. func TestSessionString(t *testing.T) {
  176. // Set value
  177. request, err := http.NewRequest("GET", "/set", nil)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. request.Header.Set("Cookie", "session="+SessionId)
  182. recorder := httptest.NewRecorder()
  183. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  184. sess := New(w, r, "./../tmp")
  185. defer sess.Close()
  186. if r.URL.Path == "/set" {
  187. sess.SetString("some_str", "test")
  188. if _, err := w.Write([]byte(`ok`)); err != nil {
  189. fmt.Printf("%s\n", err.Error())
  190. }
  191. } else {
  192. w.WriteHeader(http.StatusNotFound)
  193. if _, err := w.Write([]byte(`404`)); err != nil {
  194. fmt.Printf("%s\n", err.Error())
  195. }
  196. }
  197. }).ServeHTTP(recorder, request)
  198. if recorder.Body.String() != "ok" {
  199. t.Fatalf("bad body response, not match")
  200. }
  201. // Remember session id
  202. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  203. SessionId = recorder.Result().Cookies()[0].Value
  204. }
  205. // Get value
  206. request, err = http.NewRequest("GET", "/get", nil)
  207. if err != nil {
  208. t.Fatal(err)
  209. }
  210. request.Header.Set("Cookie", "session="+SessionId)
  211. recorder = httptest.NewRecorder()
  212. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  213. sess := New(w, r, "./../tmp")
  214. defer sess.Close()
  215. if r.URL.Path == "/get" {
  216. if _, err := w.Write([]byte(sess.GetString("some_str", ""))); err != nil {
  217. fmt.Printf("%s\n", err.Error())
  218. }
  219. } else {
  220. w.WriteHeader(http.StatusNotFound)
  221. if _, err := w.Write([]byte(`404`)); err != nil {
  222. fmt.Printf("%s\n", err.Error())
  223. }
  224. }
  225. }).ServeHTTP(recorder, request)
  226. if recorder.Body.String() != "test" {
  227. t.Fatalf("bad body response, not match")
  228. }
  229. }
  230. func TestSessionActualFile(t *testing.T) {
  231. if SessionId == "" {
  232. t.Fatal("SessionId is empty")
  233. }
  234. fname := "./../tmp" + string(os.PathSeparator) + SessionId
  235. bytes, err := ioutil.ReadFile(fname)
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. if string(bytes) != `{"Bool":{"some_bool":true},"Int":{"some_int":5},"Int64":{"some_int64":10},"String":{"some_str":"test"}}` {
  240. t.Fatal("actual file content, not match")
  241. }
  242. err = os.Remove(fname)
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. }
  247. func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
  248. // Set default values
  249. request, err := http.NewRequest("GET", "/set", nil)
  250. if err != nil {
  251. t.Fatal(err)
  252. }
  253. recorder := httptest.NewRecorder()
  254. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  255. sess := New(w, r, "./../tmp")
  256. defer sess.Close()
  257. if r.URL.Path == "/set" {
  258. sess.SetBool("some_bool", false)
  259. sess.SetInt("some_int", 0)
  260. sess.SetInt64("some_int64", 0)
  261. sess.SetString("some_str", "")
  262. if _, err := w.Write([]byte(`ok`)); err != nil {
  263. fmt.Printf("%s\n", err.Error())
  264. }
  265. } else {
  266. w.WriteHeader(http.StatusNotFound)
  267. if _, err := w.Write([]byte(`404`)); err != nil {
  268. fmt.Printf("%s\n", err.Error())
  269. }
  270. }
  271. }).ServeHTTP(recorder, request)
  272. if recorder.Body.String() != "ok" {
  273. t.Fatalf("bad body response, not match")
  274. }
  275. // Remember session id
  276. var sessId string
  277. if len(recorder.Result().Cookies()) > 0 {
  278. sessId = recorder.Result().Cookies()[0].Value
  279. }
  280. if sessId == "" {
  281. t.Fatalf("session identifier is not defined")
  282. }
  283. // Get value
  284. request, err = http.NewRequest("GET", "/get", nil)
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. request.Header.Set("Cookie", "session="+sessId)
  289. recorder = httptest.NewRecorder()
  290. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  291. sess := New(w, r, "./../tmp")
  292. defer sess.Close()
  293. if r.URL.Path == "/get" {
  294. if _, err := w.Write([]byte(fmt.Sprintf(
  295. "(%v)(%v)(%v)(%v)",
  296. sess.GetBool("some_bool", false),
  297. sess.GetInt("some_int", 0),
  298. sess.GetInt64("some_int64", 0),
  299. sess.GetString("some_str", ""),
  300. ))); err != nil {
  301. fmt.Printf("%s\n", err.Error())
  302. }
  303. } else {
  304. w.WriteHeader(http.StatusNotFound)
  305. if _, err := w.Write([]byte(`404`)); err != nil {
  306. fmt.Printf("%s\n", err.Error())
  307. }
  308. }
  309. }).ServeHTTP(recorder, request)
  310. if recorder.Body.String() != "(false)(0)(0)()" {
  311. t.Fatalf("bad body response, not match")
  312. }
  313. // Check session file
  314. fname := "./../tmp" + string(os.PathSeparator) + sessId
  315. _, err = ioutil.ReadFile(fname)
  316. if err == nil {
  317. _ = os.Remove(fname)
  318. t.Fatalf("session file in tmp folder do not must exists")
  319. }
  320. }
  321. func TestSessionDestroy(t *testing.T) {
  322. // Set value
  323. request, err := http.NewRequest("GET", "/set", nil)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  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 == "/set" {
  332. sess.SetInt("some_var", 1)
  333. if _, err := w.Write([]byte(`ok`)); err != nil {
  334. fmt.Printf("%s\n", err.Error())
  335. }
  336. } else {
  337. w.WriteHeader(http.StatusNotFound)
  338. if _, err := w.Write([]byte(`404`)); err != nil {
  339. fmt.Printf("%s\n", err.Error())
  340. }
  341. }
  342. }).ServeHTTP(recorder, request)
  343. if recorder.Body.String() != "ok" {
  344. t.Fatalf("bad body response, not match")
  345. }
  346. // Remember session id
  347. var sessId string
  348. if len(recorder.Result().Cookies()) > 0 {
  349. sessId = recorder.Result().Cookies()[0].Value
  350. }
  351. if sessId == "" {
  352. t.Fatalf("session identifier is not defined")
  353. }
  354. // Get value
  355. request, err = http.NewRequest("GET", "/get", nil)
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. request.Header.Set("Cookie", "session="+sessId)
  360. recorder = httptest.NewRecorder()
  361. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  362. sess := New(w, r, "./../tmp")
  363. defer sess.Close()
  364. if r.URL.Path == "/get" {
  365. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0)))); err != nil {
  366. fmt.Printf("%s\n", err.Error())
  367. }
  368. } else {
  369. w.WriteHeader(http.StatusNotFound)
  370. if _, err := w.Write([]byte(`404`)); err != nil {
  371. fmt.Printf("%s\n", err.Error())
  372. }
  373. }
  374. }).ServeHTTP(recorder, request)
  375. if recorder.Body.String() != "1" {
  376. t.Fatalf("bad body response, not match")
  377. }
  378. // Check destroy
  379. request, err = http.NewRequest("GET", "/get", nil)
  380. if err != nil {
  381. t.Fatal(err)
  382. }
  383. request.Header.Set("Cookie", "session="+sessId)
  384. recorder = httptest.NewRecorder()
  385. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  386. sess := New(w, r, "./../tmp")
  387. defer sess.Close()
  388. if r.URL.Path == "/get" {
  389. sess.SetInt("some_var", 2)
  390. err := sess.Destroy()
  391. if err == nil {
  392. if _, err := w.Write([]byte(`OK`)); err != nil {
  393. fmt.Printf("%s\n", err.Error())
  394. }
  395. } else {
  396. if _, err := w.Write([]byte(`ERROR`)); err != nil {
  397. fmt.Printf("%s\n", err.Error())
  398. }
  399. }
  400. } else {
  401. w.WriteHeader(http.StatusNotFound)
  402. if _, err := w.Write([]byte(`404`)); err != nil {
  403. fmt.Printf("%s\n", err.Error())
  404. }
  405. }
  406. }).ServeHTTP(recorder, request)
  407. if recorder.Body.String() != "OK" {
  408. t.Fatalf("bad body response, not match")
  409. }
  410. // Check session file
  411. fname := "./../tmp" + string(os.PathSeparator) + sessId
  412. _, err = ioutil.ReadFile(fname)
  413. if err == nil {
  414. _ = os.Remove(fname)
  415. t.Fatalf("session file in tmp folder do not must exists")
  416. }
  417. }