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