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