(t *testing.T)
| 227 | } |
| 228 | |
| 229 | func TestNonWWWRedirectWithConfig(t *testing.T) { |
| 230 | var testCases = []struct { |
| 231 | name string |
| 232 | givenCode int |
| 233 | givenSkipFunc func(c *echo.Context) bool |
| 234 | whenHost string |
| 235 | whenHeader http.Header |
| 236 | expectLocation string |
| 237 | expectStatusCode int |
| 238 | }{ |
| 239 | { |
| 240 | name: "usual redirect", |
| 241 | whenHost: "www.labstack.com", |
| 242 | expectLocation: "http://labstack.com/", |
| 243 | expectStatusCode: http.StatusMovedPermanently, |
| 244 | }, |
| 245 | { |
| 246 | name: "redirect is skipped", |
| 247 | givenSkipFunc: func(c *echo.Context) bool { |
| 248 | return true // skip always |
| 249 | }, |
| 250 | whenHost: "www.labstack.com", |
| 251 | expectLocation: "", |
| 252 | expectStatusCode: http.StatusOK, |
| 253 | }, |
| 254 | { |
| 255 | name: "redirect with custom status code", |
| 256 | givenCode: http.StatusSeeOther, |
| 257 | whenHost: "www.labstack.com", |
| 258 | expectLocation: "http://labstack.com/", |
| 259 | expectStatusCode: http.StatusSeeOther, |
| 260 | }, |
| 261 | } |
| 262 | |
| 263 | for _, tc := range testCases { |
| 264 | t.Run(tc.whenHost, func(t *testing.T) { |
| 265 | middleware := func() echo.MiddlewareFunc { |
| 266 | return NonWWWRedirectWithConfig(RedirectConfig{ |
| 267 | Skipper: tc.givenSkipFunc, |
| 268 | Code: tc.givenCode, |
| 269 | }) |
| 270 | } |
| 271 | res := redirectTest(middleware, tc.whenHost, tc.whenHeader) |
| 272 | |
| 273 | assert.Equal(t, tc.expectStatusCode, res.Code) |
| 274 | assert.Equal(t, tc.expectLocation, res.Header().Get(echo.HeaderLocation)) |
| 275 | }) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | func redirectTest(fn middlewareGenerator, host string, header http.Header) *httptest.ResponseRecorder { |
| 280 | e := echo.New() |
nothing calls this directly
no test coverage detected
searching dependent graphs…