MCPcopy
hub / github.com/labstack/echo / TestNonWWWRedirectWithConfig

Function TestNonWWWRedirectWithConfig

middleware/redirect_test.go:229–277  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

227}
228
229func 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
279func redirectTest(fn middlewareGenerator, host string, header http.Header) *httptest.ResponseRecorder {
280 e := echo.New()

Callers

nothing calls this directly

Calls 4

NonWWWRedirectWithConfigFunction · 0.85
redirectTestFunction · 0.85
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…