macaron_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2013 Martini Authors
  2. // Copyright 2014 Unknwon
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. package macaron
  16. import (
  17. "net/http"
  18. "net/http/httptest"
  19. "os"
  20. "testing"
  21. "time"
  22. . "github.com/smartystreets/goconvey/convey"
  23. )
  24. func Test_Version(t *testing.T) {
  25. Convey("Get version", t, func() {
  26. So(Version(), ShouldEqual, _VERSION)
  27. })
  28. }
  29. func Test_New(t *testing.T) {
  30. Convey("Initialize a new instance", t, func() {
  31. So(New(), ShouldNotBeNil)
  32. })
  33. Convey("Just test that Run doesn't bomb", t, func() {
  34. go New().Run()
  35. time.Sleep(1 * time.Second)
  36. os.Setenv("PORT", "4001")
  37. go New().Run("0.0.0.0")
  38. go New().Run(4002)
  39. go New().Run("0.0.0.0", 4003)
  40. })
  41. }
  42. func Test_Macaron_Before(t *testing.T) {
  43. Convey("Register before handlers", t, func() {
  44. m := New()
  45. m.Before(func(rw http.ResponseWriter, req *http.Request) bool {
  46. return false
  47. })
  48. m.Before(func(rw http.ResponseWriter, req *http.Request) bool {
  49. return true
  50. })
  51. resp := httptest.NewRecorder()
  52. req, err := http.NewRequest("GET", "/", nil)
  53. So(err, ShouldBeNil)
  54. m.ServeHTTP(resp, req)
  55. })
  56. }
  57. func Test_Macaron_ServeHTTP(t *testing.T) {
  58. Convey("Serve HTTP requests", t, func() {
  59. result := ""
  60. m := New()
  61. m.Use(func(c *Context) {
  62. result += "foo"
  63. c.Next()
  64. result += "ban"
  65. })
  66. m.Use(func(c *Context) {
  67. result += "bar"
  68. c.Next()
  69. result += "baz"
  70. })
  71. m.Get("/", func() {})
  72. m.Action(func(res http.ResponseWriter, req *http.Request) {
  73. result += "bat"
  74. res.WriteHeader(http.StatusBadRequest)
  75. })
  76. resp := httptest.NewRecorder()
  77. req, err := http.NewRequest("GET", "/", nil)
  78. So(err, ShouldBeNil)
  79. m.ServeHTTP(resp, req)
  80. So(result, ShouldEqual, "foobarbatbazban")
  81. So(resp.Code, ShouldEqual, http.StatusBadRequest)
  82. })
  83. }
  84. func Test_Macaron_Handlers(t *testing.T) {
  85. Convey("Add custom handlers", t, func() {
  86. result := ""
  87. batman := func(c *Context) {
  88. result += "batman!"
  89. }
  90. m := New()
  91. m.Use(func(c *Context) {
  92. result += "foo"
  93. c.Next()
  94. result += "ban"
  95. })
  96. m.Handlers(
  97. batman,
  98. batman,
  99. batman,
  100. )
  101. Convey("Add not callable function", func() {
  102. defer func() {
  103. So(recover(), ShouldNotBeNil)
  104. }()
  105. m.Use("shit")
  106. })
  107. m.Get("/", func() {})
  108. m.Action(func(res http.ResponseWriter, req *http.Request) {
  109. result += "bat"
  110. res.WriteHeader(http.StatusBadRequest)
  111. })
  112. resp := httptest.NewRecorder()
  113. req, err := http.NewRequest("GET", "/", nil)
  114. So(err, ShouldBeNil)
  115. m.ServeHTTP(resp, req)
  116. So(result, ShouldEqual, "batman!batman!batman!bat")
  117. So(resp.Code, ShouldEqual, http.StatusBadRequest)
  118. })
  119. }
  120. func Test_Macaron_EarlyWrite(t *testing.T) {
  121. Convey("Write early content to response", t, func() {
  122. result := ""
  123. m := New()
  124. m.Use(func(res http.ResponseWriter) {
  125. result += "foobar"
  126. res.Write([]byte("Hello world"))
  127. })
  128. m.Use(func() {
  129. result += "bat"
  130. })
  131. m.Get("/", func() {})
  132. m.Action(func(res http.ResponseWriter) {
  133. result += "baz"
  134. res.WriteHeader(http.StatusBadRequest)
  135. })
  136. resp := httptest.NewRecorder()
  137. req, err := http.NewRequest("GET", "/", nil)
  138. So(err, ShouldBeNil)
  139. m.ServeHTTP(resp, req)
  140. So(result, ShouldEqual, "foobar")
  141. So(resp.Code, ShouldEqual, http.StatusOK)
  142. })
  143. }
  144. func Test_Macaron_Written(t *testing.T) {
  145. Convey("Written sign", t, func() {
  146. resp := httptest.NewRecorder()
  147. m := New()
  148. m.Handlers(func(res http.ResponseWriter) {
  149. res.WriteHeader(http.StatusOK)
  150. })
  151. ctx := m.createContext(resp, &http.Request{Method: "GET"})
  152. So(ctx.Written(), ShouldBeFalse)
  153. ctx.run()
  154. So(ctx.Written(), ShouldBeTrue)
  155. })
  156. }
  157. func Test_Macaron_Basic_NoRace(t *testing.T) {
  158. Convey("Make sure no race between requests", t, func() {
  159. m := New()
  160. handlers := []Handler{func() {}, func() {}}
  161. // Ensure append will not realloc to trigger the race condition
  162. m.handlers = handlers[:1]
  163. m.Get("/", func() {})
  164. req, _ := http.NewRequest("GET", "/", nil)
  165. for i := 0; i < 2; i++ {
  166. go func() {
  167. resp := httptest.NewRecorder()
  168. m.ServeHTTP(resp, req)
  169. }()
  170. }
  171. })
  172. }
  173. func Test_SetENV(t *testing.T) {
  174. Convey("Get and save environment variable", t, func() {
  175. tests := []struct {
  176. in string
  177. out string
  178. }{
  179. {"", "development"},
  180. {"not_development", "not_development"},
  181. }
  182. for _, test := range tests {
  183. setENV(test.in)
  184. So(Env, ShouldEqual, test.out)
  185. }
  186. })
  187. }
  188. func Test_Config(t *testing.T) {
  189. Convey("Set and get configuration object", t, func() {
  190. So(Config(), ShouldNotBeNil)
  191. cfg, err := SetConfig([]byte(""))
  192. So(err, ShouldBeNil)
  193. So(cfg, ShouldNotBeNil)
  194. })
  195. }