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

Function TestGroup_RouteNotFound

group_test.go:309–372  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

307}
308
309func TestGroup_RouteNotFound(t *testing.T) {
310 var testCases = []struct {
311 expectRoute any
312 name string
313 whenURL string
314 expectCode int
315 }{
316 {
317 name: "404, route to static not found handler /group/a/c/xx",
318 whenURL: "/group/a/c/xx",
319 expectRoute: "GET /group/a/c/xx",
320 expectCode: http.StatusNotFound,
321 },
322 {
323 name: "404, route to path param not found handler /group/a/:file",
324 whenURL: "/group/a/echo.exe",
325 expectRoute: "GET /group/a/:file",
326 expectCode: http.StatusNotFound,
327 },
328 {
329 name: "404, route to any not found handler /group/*",
330 whenURL: "/group/b/echo.exe",
331 expectRoute: "GET /group/*",
332 expectCode: http.StatusNotFound,
333 },
334 {
335 name: "200, route /group/a/c/df to /group/a/c/df",
336 whenURL: "/group/a/c/df",
337 expectRoute: "GET /group/a/c/df",
338 expectCode: http.StatusOK,
339 },
340 }
341
342 for _, tc := range testCases {
343 t.Run(tc.name, func(t *testing.T) {
344 e := New()
345 g := e.Group("/group")
346
347 okHandler := func(c *Context) error {
348 return c.String(http.StatusOK, c.Request().Method+" "+c.Path())
349 }
350 notFoundHandler := func(c *Context) error {
351 return c.String(http.StatusNotFound, c.Request().Method+" "+c.Path())
352 }
353
354 g.GET("/", okHandler)
355 g.GET("/a/c/df", okHandler)
356 g.GET("/a/b*", okHandler)
357 g.PUT("/*", okHandler)
358
359 g.RouteNotFound("/a/c/xx", notFoundHandler) // static
360 g.RouteNotFound("/a/:file", notFoundHandler) // param
361 g.RouteNotFound("/*", notFoundHandler) // any
362
363 req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil)
364 rec := httptest.NewRecorder()
365
366 e.ServeHTTP(rec, req)

Callers

nothing calls this directly

Calls 9

NewFunction · 0.85
RequestMethod · 0.80
PathMethod · 0.80
ServeHTTPMethod · 0.80
GroupMethod · 0.45
StringMethod · 0.45
GETMethod · 0.45
PUTMethod · 0.45
RouteNotFoundMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…