middleware_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/stretchr/testify/assert"
  9. )
  10. func TestMiddlewareGeneralCase(t *testing.T) {
  11. signature := ""
  12. router := New()
  13. router.Use(func(c *Context) {
  14. signature += "A"
  15. c.Next()
  16. signature += "B"
  17. })
  18. router.Use(func(c *Context) {
  19. signature += "C"
  20. })
  21. router.GET("/", func(c *Context) {
  22. signature += "D"
  23. })
  24. router.NoRoute(func(c *Context) {
  25. signature += "X"
  26. })
  27. router.NoMethod(func(c *Context) {
  28. signature += "X"
  29. })
  30. // RUN
  31. w := performRequest(router, "GET", "/")
  32. // TEST
  33. assert.Equal(t, w.Code, 200)
  34. assert.Equal(t, signature, "ACDB")
  35. }
  36. // TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
  37. func TestMiddlewareNextOrder(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. signature += "D"
  49. })
  50. router.NoRoute(func(c *Context) {
  51. signature += "E"
  52. c.Next()
  53. signature += "F"
  54. }, func(c *Context) {
  55. signature += "G"
  56. c.Next()
  57. signature += "H"
  58. })
  59. // RUN
  60. w := performRequest(router, "GET", "/")
  61. // TEST
  62. assert.Equal(t, w.Code, 404)
  63. assert.Equal(t, signature, "ACEGHFDB")
  64. }
  65. // TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
  66. func TestMiddlewareAbortHandlersChain(t *testing.T) {
  67. signature := ""
  68. router := New()
  69. router.Use(func(c *Context) {
  70. signature += "A"
  71. })
  72. router.Use(func(c *Context) {
  73. signature += "C"
  74. c.AbortWithStatus(409)
  75. c.Next()
  76. signature += "D"
  77. })
  78. router.GET("/", func(c *Context) {
  79. signature += "D"
  80. c.Next()
  81. signature += "E"
  82. })
  83. // RUN
  84. w := performRequest(router, "GET", "/")
  85. // TEST
  86. assert.Equal(t, w.Code, 409)
  87. assert.Equal(t, signature, "ACD")
  88. }
  89. func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
  90. signature := ""
  91. router := New()
  92. router.Use(func(c *Context) {
  93. signature += "A"
  94. c.AbortWithStatus(410)
  95. c.Next()
  96. signature += "B"
  97. })
  98. router.GET("/", func(c *Context) {
  99. signature += "C"
  100. c.Next()
  101. })
  102. // RUN
  103. w := performRequest(router, "GET", "/")
  104. // TEST
  105. assert.Equal(t, w.Code, 410)
  106. assert.Equal(t, signature, "AB")
  107. }
  108. // TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
  109. // as well as Abort
  110. func TestMiddlewareFailHandlersChain(t *testing.T) {
  111. // SETUP
  112. signature := ""
  113. router := New()
  114. router.Use(func(context *Context) {
  115. signature += "A"
  116. context.Fail(500, errors.New("foo"))
  117. })
  118. router.Use(func(context *Context) {
  119. signature += "B"
  120. context.Next()
  121. signature += "C"
  122. })
  123. // RUN
  124. w := performRequest(router, "GET", "/")
  125. // TEST
  126. assert.Equal(t, w.Code, 500)
  127. assert.Equal(t, signature, "A")
  128. }