cookie_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package tango
  2. import (
  3. "testing"
  4. "bytes"
  5. "strings"
  6. "net/http"
  7. "net/http/httptest"
  8. )
  9. func TestCookie1(t *testing.T) {
  10. buff := bytes.NewBufferString("")
  11. recorder := httptest.NewRecorder()
  12. recorder.Body = buff
  13. o := Classic()
  14. o.Get("/", func(ctx *Context) string {
  15. ck := ctx.Cookies().Get("name")
  16. if ck != nil {
  17. return ck.Value
  18. }
  19. return ""
  20. })
  21. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  22. if err != nil {
  23. t.Error(err)
  24. }
  25. req.AddCookie(NewCookie("name", "test"))
  26. o.ServeHTTP(recorder, req)
  27. expect(t, recorder.Code, http.StatusOK)
  28. refute(t, len(buff.String()), 0)
  29. expect(t, buff.String(), "test")
  30. }
  31. func TestCookie2(t *testing.T) {
  32. buff := bytes.NewBufferString("")
  33. recorder := httptest.NewRecorder()
  34. recorder.Body = buff
  35. o := Classic()
  36. o.Get("/", func(ctx *Context) string {
  37. ctx.Cookies().Set(NewCookie("name", "test"))
  38. return "test"
  39. })
  40. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  41. if err != nil {
  42. t.Error(err)
  43. }
  44. o.ServeHTTP(recorder, req)
  45. expect(t, recorder.Code, http.StatusOK)
  46. refute(t, len(buff.String()), 0)
  47. expect(t, buff.String(), "test")
  48. expect(t, strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0], "name=test")
  49. }
  50. func TestCookie3(t *testing.T) {
  51. buff := bytes.NewBufferString("")
  52. recorder := httptest.NewRecorder()
  53. recorder.Body = buff
  54. o := Classic()
  55. o.Get("/", func(ctx *Context) string {
  56. ck := ctx.SecureCookies("sssss").Get("name")
  57. if ck != nil {
  58. return ck.Value
  59. }
  60. return ""
  61. })
  62. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  63. if err != nil {
  64. t.Error(err)
  65. }
  66. req.AddCookie(NewSecureCookie("sssss", "name", "test"))
  67. o.ServeHTTP(recorder, req)
  68. expect(t, recorder.Code, http.StatusOK)
  69. refute(t, len(buff.String()), 0)
  70. expect(t, buff.String(), "test")
  71. }
  72. func TestCookie4(t *testing.T) {
  73. buff := bytes.NewBufferString("")
  74. recorder := httptest.NewRecorder()
  75. recorder.Body = buff
  76. o := Classic()
  77. o.Get("/", func(ctx *Context) string {
  78. ctx.SecureCookies("ttttt").Set(NewSecureCookie("ttttt", "name", "test"))
  79. return "test"
  80. })
  81. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  82. if err != nil {
  83. t.Error(err)
  84. }
  85. o.ServeHTTP(recorder, req)
  86. expect(t, recorder.Code, http.StatusOK)
  87. refute(t, len(buff.String()), 0)
  88. expect(t, buff.String(), "test")
  89. r := strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0]
  90. s := strings.SplitN(r, "=", 2)
  91. name, value := s[0], s[1]
  92. expect(t, name, "name")
  93. expect(t, parseSecureCookie("ttttt", value), "test")
  94. }