middleware_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "errors"
  7. "strings"
  8. "testing"
  9. "github.com/manucorporat/sse"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestMiddlewareGeneralCase(t *testing.T) {
  13. signature := ""
  14. router := New()
  15. router.Use(func(c *Context) {
  16. signature += "A"
  17. c.Next()
  18. signature += "B"
  19. })
  20. router.Use(func(c *Context) {
  21. signature += "C"
  22. })
  23. router.GET("/", func(c *Context) {
  24. signature += "D"
  25. })
  26. router.NoRoute(func(c *Context) {
  27. signature += " X "
  28. })
  29. router.NoMethod(func(c *Context) {
  30. signature += " XX "
  31. })
  32. // RUN
  33. w := performRequest(router, "GET", "/")
  34. // TEST
  35. assert.Equal(t, w.Code, 200)
  36. assert.Equal(t, signature, "ACDB")
  37. }
  38. func TestMiddlewareNoRoute(t *testing.T) {
  39. signature := ""
  40. router := New()
  41. router.Use(func(c *Context) {
  42. signature += "A"
  43. c.Next()
  44. signature += "B"
  45. })
  46. router.Use(func(c *Context) {
  47. signature += "C"
  48. c.Next()
  49. c.Next()
  50. c.Next()
  51. c.Next()
  52. signature += "D"
  53. })
  54. router.NoRoute(func(c *Context) {
  55. signature += "E"
  56. c.Next()
  57. signature += "F"
  58. }, func(c *Context) {
  59. signature += "G"
  60. c.Next()
  61. signature += "H"
  62. })
  63. router.NoMethod(func(c *Context) {
  64. signature += " X "
  65. })
  66. // RUN
  67. w := performRequest(router, "GET", "/")
  68. // TEST
  69. assert.Equal(t, w.Code, 404)
  70. assert.Equal(t, signature, "ACEGHFDB")
  71. }
  72. func TestMiddlewareNoMethodEnabled(t *testing.T) {
  73. signature := ""
  74. router := New()
  75. router.HandleMethodNotAllowed = true
  76. router.Use(func(c *Context) {
  77. signature += "A"
  78. c.Next()
  79. signature += "B"
  80. })
  81. router.Use(func(c *Context) {
  82. signature += "C"
  83. c.Next()
  84. signature += "D"
  85. })
  86. router.NoMethod(func(c *Context) {
  87. signature += "E"
  88. c.Next()
  89. signature += "F"
  90. }, func(c *Context) {
  91. signature += "G"
  92. c.Next()
  93. signature += "H"
  94. })
  95. router.NoRoute(func(c *Context) {
  96. signature += " X "
  97. })
  98. router.POST("/", func(c *Context) {
  99. signature += " XX "
  100. })
  101. // RUN
  102. w := performRequest(router, "GET", "/")
  103. // TEST
  104. assert.Equal(t, w.Code, 405)
  105. assert.Equal(t, signature, "ACEGHFDB")
  106. }
  107. func TestMiddlewareNoMethodDisabled(t *testing.T) {
  108. signature := ""
  109. router := New()
  110. router.HandleMethodNotAllowed = false
  111. router.Use(func(c *Context) {
  112. signature += "A"
  113. c.Next()
  114. signature += "B"
  115. })
  116. router.Use(func(c *Context) {
  117. signature += "C"
  118. c.Next()
  119. signature += "D"
  120. })
  121. router.NoMethod(func(c *Context) {
  122. signature += "E"
  123. c.Next()
  124. signature += "F"
  125. }, func(c *Context) {
  126. signature += "G"
  127. c.Next()
  128. signature += "H"
  129. })
  130. router.NoRoute(func(c *Context) {
  131. signature += " X "
  132. })
  133. router.POST("/", func(c *Context) {
  134. signature += " XX "
  135. })
  136. // RUN
  137. w := performRequest(router, "GET", "/")
  138. // TEST
  139. assert.Equal(t, w.Code, 404)
  140. assert.Equal(t, signature, "AC X DB")
  141. }
  142. func TestMiddlewareAbort(t *testing.T) {
  143. signature := ""
  144. router := New()
  145. router.Use(func(c *Context) {
  146. signature += "A"
  147. })
  148. router.Use(func(c *Context) {
  149. signature += "C"
  150. c.AbortWithStatus(401)
  151. c.Next()
  152. signature += "D"
  153. })
  154. router.GET("/", func(c *Context) {
  155. signature += " X "
  156. c.Next()
  157. signature += " XX "
  158. })
  159. // RUN
  160. w := performRequest(router, "GET", "/")
  161. // TEST
  162. assert.Equal(t, w.Code, 401)
  163. assert.Equal(t, signature, "ACD")
  164. }
  165. func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
  166. signature := ""
  167. router := New()
  168. router.Use(func(c *Context) {
  169. signature += "A"
  170. c.Next()
  171. c.AbortWithStatus(410)
  172. signature += "B"
  173. })
  174. router.GET("/", func(c *Context) {
  175. signature += "C"
  176. c.Next()
  177. })
  178. // RUN
  179. w := performRequest(router, "GET", "/")
  180. // TEST
  181. assert.Equal(t, w.Code, 410)
  182. assert.Equal(t, signature, "ACB")
  183. }
  184. // TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
  185. // as well as Abort
  186. func TestMiddlewareFailHandlersChain(t *testing.T) {
  187. // SETUP
  188. signature := ""
  189. router := New()
  190. router.Use(func(context *Context) {
  191. signature += "A"
  192. context.AbortWithError(500, errors.New("foo"))
  193. })
  194. router.Use(func(context *Context) {
  195. signature += "B"
  196. context.Next()
  197. signature += "C"
  198. })
  199. // RUN
  200. w := performRequest(router, "GET", "/")
  201. // TEST
  202. assert.Equal(t, w.Code, 500)
  203. assert.Equal(t, signature, "A")
  204. }
  205. func TestMiddlewareWrite(t *testing.T) {
  206. router := New()
  207. router.Use(func(c *Context) {
  208. c.String(400, "hola\n")
  209. })
  210. router.Use(func(c *Context) {
  211. c.XML(400, H{"foo": "bar"})
  212. })
  213. router.Use(func(c *Context) {
  214. c.JSON(400, H{"foo": "bar"})
  215. })
  216. router.GET("/", func(c *Context) {
  217. c.JSON(400, H{"foo": "bar"})
  218. }, func(c *Context) {
  219. c.Render(400, sse.Event{
  220. Event: "test",
  221. Data: "message",
  222. })
  223. })
  224. w := performRequest(router, "GET", "/")
  225. assert.Equal(t, w.Code, 400)
  226. assert.Equal(t, strings.Replace(w.Body.String(), " ", "", -1), strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}\n{\"foo\":\"bar\"}\nevent:test\ndata:message\n\n", " ", "", -1))
  227. }