session_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. // Is set (false)
  13. request, err := http.NewRequest("GET", "/isset", nil)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. request.Header.Set("Cookie", "session="+SessionId)
  18. recorder := httptest.NewRecorder()
  19. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. sess, _ := New(w, r, "./../tmp")
  21. defer sess.Close()
  22. if r.URL.Path == "/isset" {
  23. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetBool("some_bool")))); 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() != "false" {
  34. t.Fatal("bad body response, not match")
  35. }
  36. // Set value
  37. request, err = http.NewRequest("GET", "/set", nil)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. recorder = httptest.NewRecorder()
  42. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  43. sess, _ := New(w, r, "./../tmp")
  44. defer sess.Close()
  45. if r.URL.Path == "/set" {
  46. sess.SetBool("some_bool", true)
  47. if _, err := w.Write([]byte(`ok`)); err != nil {
  48. fmt.Printf("%s\n", err.Error())
  49. }
  50. } else {
  51. w.WriteHeader(http.StatusNotFound)
  52. if _, err := w.Write([]byte(`404`)); err != nil {
  53. fmt.Printf("%s\n", err.Error())
  54. }
  55. }
  56. }).ServeHTTP(recorder, request)
  57. if recorder.Body.String() != "ok" {
  58. t.Fatal("bad body response, not match")
  59. }
  60. // Remember session id
  61. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  62. SessionId = recorder.Result().Cookies()[0].Value
  63. }
  64. // Get value
  65. request, err = http.NewRequest("GET", "/get", nil)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. request.Header.Set("Cookie", "session="+SessionId)
  70. recorder = httptest.NewRecorder()
  71. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  72. sess, _ := New(w, r, "./../tmp")
  73. defer sess.Close()
  74. if r.URL.Path == "/get" {
  75. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetBool("some_bool", false)))); err != nil {
  76. fmt.Printf("%s\n", err.Error())
  77. }
  78. } else {
  79. w.WriteHeader(http.StatusNotFound)
  80. if _, err := w.Write([]byte(`404`)); err != nil {
  81. fmt.Printf("%s\n", err.Error())
  82. }
  83. }
  84. }).ServeHTTP(recorder, request)
  85. if recorder.Body.String() != "true" {
  86. t.Fatal("bad body response, not match")
  87. }
  88. // Is set (true)
  89. request, err = http.NewRequest("GET", "/isset", nil)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. request.Header.Set("Cookie", "session="+SessionId)
  94. recorder = httptest.NewRecorder()
  95. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  96. sess, _ := New(w, r, "./../tmp")
  97. defer sess.Close()
  98. if r.URL.Path == "/isset" {
  99. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetBool("some_bool")))); err != nil {
  100. fmt.Printf("%s\n", err.Error())
  101. }
  102. } else {
  103. w.WriteHeader(http.StatusNotFound)
  104. if _, err := w.Write([]byte(`404`)); err != nil {
  105. fmt.Printf("%s\n", err.Error())
  106. }
  107. }
  108. }).ServeHTTP(recorder, request)
  109. if recorder.Body.String() != "true" {
  110. t.Fatal("bad body response, not match")
  111. }
  112. // Del
  113. request, err = http.NewRequest("GET", "/del", nil)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. request.Header.Set("Cookie", "session="+SessionId)
  118. recorder = httptest.NewRecorder()
  119. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  120. sess, _ := New(w, r, "./../tmp")
  121. defer sess.Close()
  122. if r.URL.Path == "/del" {
  123. sess.DelBool("some_bool")
  124. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetBool("some_bool")))); err != nil {
  125. fmt.Printf("%s\n", err.Error())
  126. }
  127. } else {
  128. w.WriteHeader(http.StatusNotFound)
  129. if _, err := w.Write([]byte(`404`)); err != nil {
  130. fmt.Printf("%s\n", err.Error())
  131. }
  132. }
  133. }).ServeHTTP(recorder, request)
  134. if recorder.Body.String() != "false" {
  135. t.Fatal("bad body response, not match")
  136. }
  137. }
  138. func TestSessionInt(t *testing.T) {
  139. // Is set (false)
  140. request, err := http.NewRequest("GET", "/isset", nil)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. request.Header.Set("Cookie", "session="+SessionId)
  145. recorder := httptest.NewRecorder()
  146. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  147. sess, _ := New(w, r, "./../tmp")
  148. defer sess.Close()
  149. if r.URL.Path == "/isset" {
  150. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt("some_int")))); err != nil {
  151. fmt.Printf("%s\n", err.Error())
  152. }
  153. } else {
  154. w.WriteHeader(http.StatusNotFound)
  155. if _, err := w.Write([]byte(`404`)); err != nil {
  156. fmt.Printf("%s\n", err.Error())
  157. }
  158. }
  159. }).ServeHTTP(recorder, request)
  160. if recorder.Body.String() != "false" {
  161. t.Fatal("bad body response, not match")
  162. }
  163. // Set value
  164. request, err = http.NewRequest("GET", "/set", nil)
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. request.Header.Set("Cookie", "session="+SessionId)
  169. recorder = httptest.NewRecorder()
  170. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  171. sess, _ := New(w, r, "./../tmp")
  172. defer sess.Close()
  173. if r.URL.Path == "/set" {
  174. sess.SetInt("some_int", 5)
  175. if _, err := w.Write([]byte(`ok`)); err != nil {
  176. fmt.Printf("%s\n", err.Error())
  177. }
  178. } else {
  179. w.WriteHeader(http.StatusNotFound)
  180. if _, err := w.Write([]byte(`404`)); err != nil {
  181. fmt.Printf("%s\n", err.Error())
  182. }
  183. }
  184. }).ServeHTTP(recorder, request)
  185. if recorder.Body.String() != "ok" {
  186. t.Fatal("bad body response, not match")
  187. }
  188. // Remember session id
  189. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  190. SessionId = recorder.Result().Cookies()[0].Value
  191. }
  192. // Get value
  193. request, err = http.NewRequest("GET", "/get", nil)
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. request.Header.Set("Cookie", "session="+SessionId)
  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 == "/get" {
  203. if _, err := w.Write([]byte(fmt.Sprintf("%d", sess.GetInt("some_int", 0)))); err != nil {
  204. fmt.Printf("%s\n", err.Error())
  205. }
  206. } else {
  207. w.WriteHeader(http.StatusNotFound)
  208. if _, err := w.Write([]byte(`404`)); err != nil {
  209. fmt.Printf("%s\n", err.Error())
  210. }
  211. }
  212. }).ServeHTTP(recorder, request)
  213. if recorder.Body.String() != "5" {
  214. t.Fatal("bad body response, not match")
  215. }
  216. // Is set (true)
  217. request, err = http.NewRequest("GET", "/isset", nil)
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. request.Header.Set("Cookie", "session="+SessionId)
  222. recorder = httptest.NewRecorder()
  223. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  224. sess, _ := New(w, r, "./../tmp")
  225. defer sess.Close()
  226. if r.URL.Path == "/isset" {
  227. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt("some_int")))); err != nil {
  228. fmt.Printf("%s\n", err.Error())
  229. }
  230. } else {
  231. w.WriteHeader(http.StatusNotFound)
  232. if _, err := w.Write([]byte(`404`)); err != nil {
  233. fmt.Printf("%s\n", err.Error())
  234. }
  235. }
  236. }).ServeHTTP(recorder, request)
  237. if recorder.Body.String() != "true" {
  238. t.Fatal("bad body response, not match")
  239. }
  240. // Del
  241. request, err = http.NewRequest("GET", "/del", nil)
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. request.Header.Set("Cookie", "session="+SessionId)
  246. recorder = httptest.NewRecorder()
  247. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  248. sess, _ := New(w, r, "./../tmp")
  249. defer sess.Close()
  250. if r.URL.Path == "/del" {
  251. sess.DelInt("some_int")
  252. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt("some_int")))); err != nil {
  253. fmt.Printf("%s\n", err.Error())
  254. }
  255. } else {
  256. w.WriteHeader(http.StatusNotFound)
  257. if _, err := w.Write([]byte(`404`)); err != nil {
  258. fmt.Printf("%s\n", err.Error())
  259. }
  260. }
  261. }).ServeHTTP(recorder, request)
  262. if recorder.Body.String() != "false" {
  263. t.Fatal("bad body response, not match")
  264. }
  265. }
  266. func TestSessionInt64(t *testing.T) {
  267. // Is set (false)
  268. request, err := http.NewRequest("GET", "/isset", nil)
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. request.Header.Set("Cookie", "session="+SessionId)
  273. recorder := httptest.NewRecorder()
  274. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  275. sess, _ := New(w, r, "./../tmp")
  276. defer sess.Close()
  277. if r.URL.Path == "/isset" {
  278. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt64("some_int64")))); err != nil {
  279. fmt.Printf("%s\n", err.Error())
  280. }
  281. } else {
  282. w.WriteHeader(http.StatusNotFound)
  283. if _, err := w.Write([]byte(`404`)); err != nil {
  284. fmt.Printf("%s\n", err.Error())
  285. }
  286. }
  287. }).ServeHTTP(recorder, request)
  288. if recorder.Body.String() != "false" {
  289. t.Fatal("bad body response, not match")
  290. }
  291. // Set value
  292. request, err = http.NewRequest("GET", "/set", nil)
  293. if err != nil {
  294. t.Fatal(err)
  295. }
  296. request.Header.Set("Cookie", "session="+SessionId)
  297. recorder = httptest.NewRecorder()
  298. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  299. sess, _ := New(w, r, "./../tmp")
  300. defer sess.Close()
  301. if r.URL.Path == "/set" {
  302. sess.SetInt64("some_int64", 10)
  303. if _, err := w.Write([]byte(`ok`)); err != nil {
  304. fmt.Printf("%s\n", err.Error())
  305. }
  306. } else {
  307. w.WriteHeader(http.StatusNotFound)
  308. if _, err := w.Write([]byte(`404`)); err != nil {
  309. fmt.Printf("%s\n", err.Error())
  310. }
  311. }
  312. }).ServeHTTP(recorder, request)
  313. if recorder.Body.String() != "ok" {
  314. t.Fatal("bad body response, not match")
  315. }
  316. // Remember session id
  317. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  318. SessionId = recorder.Result().Cookies()[0].Value
  319. }
  320. // Get value
  321. request, err = http.NewRequest("GET", "/get", nil)
  322. if err != nil {
  323. t.Fatal(err)
  324. }
  325. request.Header.Set("Cookie", "session="+SessionId)
  326. recorder = httptest.NewRecorder()
  327. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  328. sess, _ := New(w, r, "./../tmp")
  329. defer sess.Close()
  330. if r.URL.Path == "/get" {
  331. if _, err := w.Write([]byte(fmt.Sprintf("%d", sess.GetInt64("some_int64", 0)))); err != nil {
  332. fmt.Printf("%s\n", err.Error())
  333. }
  334. } else {
  335. w.WriteHeader(http.StatusNotFound)
  336. if _, err := w.Write([]byte(`404`)); err != nil {
  337. fmt.Printf("%s\n", err.Error())
  338. }
  339. }
  340. }).ServeHTTP(recorder, request)
  341. if recorder.Body.String() != "10" {
  342. t.Fatal("bad body response, not match")
  343. }
  344. // Is set (true)
  345. request, err = http.NewRequest("GET", "/isset", nil)
  346. if err != nil {
  347. t.Fatal(err)
  348. }
  349. request.Header.Set("Cookie", "session="+SessionId)
  350. recorder = httptest.NewRecorder()
  351. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  352. sess, _ := New(w, r, "./../tmp")
  353. defer sess.Close()
  354. if r.URL.Path == "/isset" {
  355. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt64("some_int64")))); err != nil {
  356. fmt.Printf("%s\n", err.Error())
  357. }
  358. } else {
  359. w.WriteHeader(http.StatusNotFound)
  360. if _, err := w.Write([]byte(`404`)); err != nil {
  361. fmt.Printf("%s\n", err.Error())
  362. }
  363. }
  364. }).ServeHTTP(recorder, request)
  365. if recorder.Body.String() != "true" {
  366. t.Fatal("bad body response, not match")
  367. }
  368. // Del
  369. request, err = http.NewRequest("GET", "/del", nil)
  370. if err != nil {
  371. t.Fatal(err)
  372. }
  373. request.Header.Set("Cookie", "session="+SessionId)
  374. recorder = httptest.NewRecorder()
  375. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  376. sess, _ := New(w, r, "./../tmp")
  377. defer sess.Close()
  378. if r.URL.Path == "/del" {
  379. sess.DelInt64("some_int64")
  380. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetInt64("some_int64")))); err != nil {
  381. fmt.Printf("%s\n", err.Error())
  382. }
  383. } else {
  384. w.WriteHeader(http.StatusNotFound)
  385. if _, err := w.Write([]byte(`404`)); err != nil {
  386. fmt.Printf("%s\n", err.Error())
  387. }
  388. }
  389. }).ServeHTTP(recorder, request)
  390. if recorder.Body.String() != "false" {
  391. t.Fatal("bad body response, not match")
  392. }
  393. }
  394. func TestSessionString(t *testing.T) {
  395. // Is set (false)
  396. request, err := http.NewRequest("GET", "/isset", nil)
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. request.Header.Set("Cookie", "session="+SessionId)
  401. recorder := httptest.NewRecorder()
  402. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  403. sess, _ := New(w, r, "./../tmp")
  404. defer sess.Close()
  405. if r.URL.Path == "/isset" {
  406. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetString("some_str")))); err != nil {
  407. fmt.Printf("%s\n", err.Error())
  408. }
  409. } else {
  410. w.WriteHeader(http.StatusNotFound)
  411. if _, err := w.Write([]byte(`404`)); err != nil {
  412. fmt.Printf("%s\n", err.Error())
  413. }
  414. }
  415. }).ServeHTTP(recorder, request)
  416. if recorder.Body.String() != "false" {
  417. t.Fatal("bad body response, not match")
  418. }
  419. // Set value
  420. request, err = http.NewRequest("GET", "/set", nil)
  421. if err != nil {
  422. t.Fatal(err)
  423. }
  424. request.Header.Set("Cookie", "session="+SessionId)
  425. recorder = httptest.NewRecorder()
  426. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  427. sess, _ := New(w, r, "./../tmp")
  428. defer sess.Close()
  429. if r.URL.Path == "/set" {
  430. sess.SetString("some_str", "test")
  431. if _, err := w.Write([]byte(`ok`)); err != nil {
  432. fmt.Printf("%s\n", err.Error())
  433. }
  434. } else {
  435. w.WriteHeader(http.StatusNotFound)
  436. if _, err := w.Write([]byte(`404`)); err != nil {
  437. fmt.Printf("%s\n", err.Error())
  438. }
  439. }
  440. }).ServeHTTP(recorder, request)
  441. if recorder.Body.String() != "ok" {
  442. t.Fatal("bad body response, not match")
  443. }
  444. // Remember session id
  445. if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
  446. SessionId = recorder.Result().Cookies()[0].Value
  447. }
  448. // Get value
  449. request, err = http.NewRequest("GET", "/get", nil)
  450. if err != nil {
  451. t.Fatal(err)
  452. }
  453. request.Header.Set("Cookie", "session="+SessionId)
  454. recorder = httptest.NewRecorder()
  455. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  456. sess, _ := New(w, r, "./../tmp")
  457. defer sess.Close()
  458. if r.URL.Path == "/get" {
  459. if _, err := w.Write([]byte(sess.GetString("some_str", ""))); err != nil {
  460. fmt.Printf("%s\n", err.Error())
  461. }
  462. } else {
  463. w.WriteHeader(http.StatusNotFound)
  464. if _, err := w.Write([]byte(`404`)); err != nil {
  465. fmt.Printf("%s\n", err.Error())
  466. }
  467. }
  468. }).ServeHTTP(recorder, request)
  469. if recorder.Body.String() != "test" {
  470. t.Fatal("bad body response, not match")
  471. }
  472. // Is set (true)
  473. request, err = http.NewRequest("GET", "/isset", nil)
  474. if err != nil {
  475. t.Fatal(err)
  476. }
  477. request.Header.Set("Cookie", "session="+SessionId)
  478. recorder = httptest.NewRecorder()
  479. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  480. sess, _ := New(w, r, "./../tmp")
  481. defer sess.Close()
  482. if r.URL.Path == "/isset" {
  483. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetString("some_str")))); err != nil {
  484. fmt.Printf("%s\n", err.Error())
  485. }
  486. } else {
  487. w.WriteHeader(http.StatusNotFound)
  488. if _, err := w.Write([]byte(`404`)); err != nil {
  489. fmt.Printf("%s\n", err.Error())
  490. }
  491. }
  492. }).ServeHTTP(recorder, request)
  493. if recorder.Body.String() != "true" {
  494. t.Fatal("bad body response, not match")
  495. }
  496. // Del
  497. request, err = http.NewRequest("GET", "/del", nil)
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. request.Header.Set("Cookie", "session="+SessionId)
  502. recorder = httptest.NewRecorder()
  503. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  504. sess, _ := New(w, r, "./../tmp")
  505. defer sess.Close()
  506. if r.URL.Path == "/del" {
  507. sess.DelString("some_str")
  508. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.IsSetString("some_str")))); err != nil {
  509. fmt.Printf("%s\n", err.Error())
  510. }
  511. } else {
  512. w.WriteHeader(http.StatusNotFound)
  513. if _, err := w.Write([]byte(`404`)); err != nil {
  514. fmt.Printf("%s\n", err.Error())
  515. }
  516. }
  517. }).ServeHTTP(recorder, request)
  518. if recorder.Body.String() != "false" {
  519. t.Fatal("bad body response, not match")
  520. }
  521. }
  522. func TestSessionActualFile(t *testing.T) {
  523. if SessionId == "" {
  524. t.Fatal("SessionId is empty")
  525. }
  526. fname := "./../tmp" + string(os.PathSeparator) + SessionId
  527. bytes, err := ioutil.ReadFile(fname)
  528. if err != nil {
  529. t.Fatal(err)
  530. }
  531. if string(bytes) != `{"Bool":{},"Int":{},"Int64":{},"String":{}}` {
  532. t.Fatal("actual file content, not match")
  533. }
  534. err = os.Remove(fname)
  535. if err != nil {
  536. t.Fatal(err)
  537. }
  538. }
  539. func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
  540. // Set default values
  541. request, err := http.NewRequest("GET", "/set", nil)
  542. if err != nil {
  543. t.Fatal(err)
  544. }
  545. recorder := httptest.NewRecorder()
  546. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  547. sess, _ := New(w, r, "./../tmp")
  548. defer sess.Close()
  549. if r.URL.Path == "/set" {
  550. sess.SetBool("some_bool", false)
  551. sess.SetInt("some_int", 0)
  552. sess.SetInt64("some_int64", 0)
  553. sess.SetString("some_str", "")
  554. if _, err := w.Write([]byte(`ok`)); err != nil {
  555. fmt.Printf("%s\n", err.Error())
  556. }
  557. } else {
  558. w.WriteHeader(http.StatusNotFound)
  559. if _, err := w.Write([]byte(`404`)); err != nil {
  560. fmt.Printf("%s\n", err.Error())
  561. }
  562. }
  563. }).ServeHTTP(recorder, request)
  564. if recorder.Body.String() != "ok" {
  565. t.Fatal("bad body response, not match")
  566. }
  567. // Remember session id
  568. var sessId string
  569. if len(recorder.Result().Cookies()) > 0 {
  570. sessId = recorder.Result().Cookies()[0].Value
  571. }
  572. if sessId == "" {
  573. t.Fatal("session identifier is not defined")
  574. }
  575. // Get value
  576. request, err = http.NewRequest("GET", "/get", nil)
  577. if err != nil {
  578. t.Fatal(err)
  579. }
  580. request.Header.Set("Cookie", "session="+sessId)
  581. recorder = httptest.NewRecorder()
  582. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  583. sess, _ := New(w, r, "./../tmp")
  584. defer sess.Close()
  585. if r.URL.Path == "/get" {
  586. if _, err := w.Write([]byte(fmt.Sprintf(
  587. "(%v)(%v)(%v)(%v)",
  588. sess.GetBool("some_bool", false),
  589. sess.GetInt("some_int", 0),
  590. sess.GetInt64("some_int64", 0),
  591. sess.GetString("some_str", ""),
  592. ))); err != nil {
  593. fmt.Printf("%s\n", err.Error())
  594. }
  595. } else {
  596. w.WriteHeader(http.StatusNotFound)
  597. if _, err := w.Write([]byte(`404`)); err != nil {
  598. fmt.Printf("%s\n", err.Error())
  599. }
  600. }
  601. }).ServeHTTP(recorder, request)
  602. if recorder.Body.String() != "(false)(0)(0)()" {
  603. t.Fatal("bad body response, not match")
  604. }
  605. // Check session file
  606. fname := "./../tmp" + string(os.PathSeparator) + sessId
  607. _, err = ioutil.ReadFile(fname)
  608. if err == nil {
  609. _ = os.Remove(fname)
  610. t.Fatal("session file in tmp folder do not must exists")
  611. }
  612. }
  613. func TestSessionDestroy(t *testing.T) {
  614. // Set value
  615. request, err := http.NewRequest("GET", "/set", nil)
  616. if err != nil {
  617. t.Fatal(err)
  618. }
  619. recorder := httptest.NewRecorder()
  620. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  621. sess, _ := New(w, r, "./../tmp")
  622. defer sess.Close()
  623. if r.URL.Path == "/set" {
  624. sess.SetInt("some_var", 1)
  625. if _, err := w.Write([]byte(`ok`)); err != nil {
  626. fmt.Printf("%s\n", err.Error())
  627. }
  628. } else {
  629. w.WriteHeader(http.StatusNotFound)
  630. if _, err := w.Write([]byte(`404`)); err != nil {
  631. fmt.Printf("%s\n", err.Error())
  632. }
  633. }
  634. }).ServeHTTP(recorder, request)
  635. if recorder.Body.String() != "ok" {
  636. t.Fatal("bad body response, not match")
  637. }
  638. // Remember session id
  639. var sessId string
  640. if len(recorder.Result().Cookies()) > 0 {
  641. sessId = recorder.Result().Cookies()[0].Value
  642. }
  643. if sessId == "" {
  644. t.Fatal("session identifier is not defined")
  645. }
  646. // Get value
  647. request, err = http.NewRequest("GET", "/get", nil)
  648. if err != nil {
  649. t.Fatal(err)
  650. }
  651. request.Header.Set("Cookie", "session="+sessId)
  652. recorder = httptest.NewRecorder()
  653. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  654. sess, _ := New(w, r, "./../tmp")
  655. defer sess.Close()
  656. if r.URL.Path == "/get" {
  657. if _, err := w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0)))); err != nil {
  658. fmt.Printf("%s\n", err.Error())
  659. }
  660. } else {
  661. w.WriteHeader(http.StatusNotFound)
  662. if _, err := w.Write([]byte(`404`)); err != nil {
  663. fmt.Printf("%s\n", err.Error())
  664. }
  665. }
  666. }).ServeHTTP(recorder, request)
  667. if recorder.Body.String() != "1" {
  668. t.Fatal("bad body response, not match")
  669. }
  670. // Check destroy
  671. request, err = http.NewRequest("GET", "/get", nil)
  672. if err != nil {
  673. t.Fatal(err)
  674. }
  675. request.Header.Set("Cookie", "session="+sessId)
  676. recorder = httptest.NewRecorder()
  677. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  678. sess, _ := New(w, r, "./../tmp")
  679. defer sess.Close()
  680. if r.URL.Path == "/get" {
  681. sess.SetInt("some_var", 2)
  682. err := sess.Destroy()
  683. if err == nil {
  684. if _, err := w.Write([]byte(`OK`)); err != nil {
  685. fmt.Printf("%s\n", err.Error())
  686. }
  687. } else {
  688. if _, err := w.Write([]byte(`ERROR`)); err != nil {
  689. fmt.Printf("%s\n", err.Error())
  690. }
  691. }
  692. } else {
  693. w.WriteHeader(http.StatusNotFound)
  694. if _, err := w.Write([]byte(`404`)); err != nil {
  695. fmt.Printf("%s\n", err.Error())
  696. }
  697. }
  698. }).ServeHTTP(recorder, request)
  699. if recorder.Body.String() != "OK" {
  700. t.Fatal("bad body response, not match")
  701. }
  702. // Check session file
  703. fname := "./../tmp" + string(os.PathSeparator) + sessId
  704. _, err = ioutil.ReadFile(fname)
  705. if err == nil {
  706. _ = os.Remove(fname)
  707. t.Fatal("session file in tmp folder do not must exists")
  708. }
  709. }