Skip to content

Commit 662f4d0

Browse files
committed
add testcase
1 parent ab589dc commit 662f4d0

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

echo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ type Config struct {
295295

296296
// NoGroupAutoRegister404Routes bool is a flag that indicates whether echo.Group should NOT register 404 routes automatically
297297
// when there are middlewares registered with the group.
298+
// Note: if you decide not to register 404 routes automatically, make sure to check if all your middlewares are executed
299+
// as expected. For example - CORS middleware.
298300
NoGroupAutoRegister404Routes bool
299301
}
300302

group_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ func TestGroup_withoutRouteWillExecuteMiddleware(t *testing.T) {
3535
assert.True(t, called)
3636
}
3737

38+
func TestGroup_withoutRouteWillNotExecuteMiddleware(t *testing.T) {
39+
e := NewWithConfig(Config{NoGroupAutoRegister404Routes: true})
40+
41+
called := false
42+
mw := func(next HandlerFunc) HandlerFunc {
43+
return func(c *Context) error {
44+
called = true
45+
return c.NoContent(http.StatusTeapot)
46+
}
47+
}
48+
// even though group has middleware it will be executed when there are no routes under that group
49+
// because implicit routes ("" and "/*") are created for the group
50+
_ = e.Group("/group", mw)
51+
52+
status, body := request(http.MethodGet, "/group/nope", e)
53+
assert.Equal(t, http.StatusNotFound, status)
54+
assert.Equal(t, `{"message":"Not Found"}`+"\n", body)
55+
56+
assert.False(t, called)
57+
}
58+
3859
func TestGroup_withRoutesWillExecuteMiddlewareFor404(t *testing.T) {
3960
e := New()
4061

0 commit comments

Comments
 (0)