You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
4.5 KiB

  1. // Copyright 2014 The Macaron Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package session
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "strings"
  19. "testing"
  20. "time"
  21. . "github.com/smartystreets/goconvey/convey"
  22. "gopkg.in/macaron.v1"
  23. )
  24. func Test_Version(t *testing.T) {
  25. Convey("Check package version", t, func() {
  26. So(Version(), ShouldEqual, _VERSION)
  27. })
  28. }
  29. func Test_Sessioner(t *testing.T) {
  30. Convey("Use session middleware", t, func() {
  31. m := macaron.New()
  32. m.Use(Sessioner())
  33. m.Get("/", func() {})
  34. resp := httptest.NewRecorder()
  35. req, err := http.NewRequest("GET", "/", nil)
  36. So(err, ShouldBeNil)
  37. m.ServeHTTP(resp, req)
  38. })
  39. Convey("Register invalid provider", t, func() {
  40. Convey("Provider not exists", func() {
  41. defer func() {
  42. So(recover(), ShouldNotBeNil)
  43. }()
  44. m := macaron.New()
  45. m.Use(Sessioner(Options{
  46. Provider: "fake",
  47. }))
  48. })
  49. Convey("Provider value is nil", func() {
  50. defer func() {
  51. So(recover(), ShouldNotBeNil)
  52. }()
  53. Register("fake", nil)
  54. })
  55. Convey("Register twice", func() {
  56. defer func() {
  57. So(recover(), ShouldNotBeNil)
  58. }()
  59. Register("memory", &MemProvider{})
  60. })
  61. })
  62. }
  63. func testProvider(opt Options) {
  64. Convey("Basic operation", func() {
  65. m := macaron.New()
  66. m.Use(Sessioner(opt))
  67. m.Get("/", func(ctx *macaron.Context, sess Store) {
  68. sess.Set("uname", "unknwon")
  69. })
  70. m.Get("/reg", func(ctx *macaron.Context, sess Store) {
  71. raw, err := sess.RegenerateId(ctx)
  72. So(err, ShouldBeNil)
  73. So(raw, ShouldNotBeNil)
  74. uname := raw.Get("uname")
  75. So(uname, ShouldNotBeNil)
  76. So(uname, ShouldEqual, "unknwon")
  77. })
  78. m.Get("/get", func(ctx *macaron.Context, sess Store) {
  79. sid := sess.ID()
  80. So(sid, ShouldNotBeEmpty)
  81. raw, err := sess.Read(sid)
  82. So(err, ShouldBeNil)
  83. So(raw, ShouldNotBeNil)
  84. uname := sess.Get("uname")
  85. So(uname, ShouldNotBeNil)
  86. So(uname, ShouldEqual, "unknwon")
  87. So(sess.Delete("uname"), ShouldBeNil)
  88. So(sess.Get("uname"), ShouldBeNil)
  89. So(sess.Destory(ctx), ShouldBeNil)
  90. })
  91. resp := httptest.NewRecorder()
  92. req, err := http.NewRequest("GET", "/", nil)
  93. So(err, ShouldBeNil)
  94. m.ServeHTTP(resp, req)
  95. cookie := resp.Header().Get("Set-Cookie")
  96. resp = httptest.NewRecorder()
  97. req, err = http.NewRequest("GET", "/reg", nil)
  98. So(err, ShouldBeNil)
  99. req.Header.Set("Cookie", cookie)
  100. m.ServeHTTP(resp, req)
  101. cookie = resp.Header().Get("Set-Cookie")
  102. resp = httptest.NewRecorder()
  103. req, err = http.NewRequest("GET", "/get", nil)
  104. So(err, ShouldBeNil)
  105. req.Header.Set("Cookie", cookie)
  106. m.ServeHTTP(resp, req)
  107. })
  108. Convey("Regenrate empty session", func() {
  109. m := macaron.New()
  110. m.Use(Sessioner(opt))
  111. m.Get("/", func(ctx *macaron.Context, sess Store) {
  112. raw, err := sess.RegenerateId(ctx)
  113. So(err, ShouldBeNil)
  114. So(raw, ShouldNotBeNil)
  115. })
  116. resp := httptest.NewRecorder()
  117. req, err := http.NewRequest("GET", "/", nil)
  118. So(err, ShouldBeNil)
  119. req.Header.Set("Cookie", "MacaronSession=ad2c7e3cbecfcf48; Path=/;")
  120. m.ServeHTTP(resp, req)
  121. })
  122. Convey("GC session", func() {
  123. m := macaron.New()
  124. opt2 := opt
  125. opt2.Gclifetime = 1
  126. m.Use(Sessioner(opt2))
  127. m.Get("/", func(sess Store) {
  128. sess.Set("uname", "unknwon")
  129. So(sess.ID(), ShouldNotBeEmpty)
  130. uname := sess.Get("uname")
  131. So(uname, ShouldNotBeNil)
  132. So(uname, ShouldEqual, "unknwon")
  133. So(sess.Flush(), ShouldBeNil)
  134. So(sess.Get("uname"), ShouldBeNil)
  135. time.Sleep(2 * time.Second)
  136. sess.GC()
  137. So(sess.Count(), ShouldEqual, 0)
  138. })
  139. resp := httptest.NewRecorder()
  140. req, err := http.NewRequest("GET", "/", nil)
  141. So(err, ShouldBeNil)
  142. m.ServeHTTP(resp, req)
  143. })
  144. Convey("Detect invalid sid", func() {
  145. m := macaron.New()
  146. m.Use(Sessioner(opt))
  147. m.Get("/", func(ctx *macaron.Context, sess Store) {
  148. raw, err := sess.Read("../session/ad2c7e3cbecfcf486")
  149. So(strings.Contains(err.Error(), "invalid 'sid': "), ShouldBeTrue)
  150. So(raw, ShouldBeNil)
  151. })
  152. resp := httptest.NewRecorder()
  153. req, err := http.NewRequest("GET", "/", nil)
  154. So(err, ShouldBeNil)
  155. m.ServeHTTP(resp, req)
  156. })
  157. }