middleware_test.go 4.8 KB

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