|
| 1 | +package echo |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGroupRouteNotFoundFallsBackToRootHandler(t *testing.T) { |
| 13 | + e := New() |
| 14 | + e.HideBanner = true |
| 15 | + e.HidePort = true |
| 16 | + |
| 17 | + e.RouteNotFound("/*", func(c Context) error { |
| 18 | + return c.NoContent(http.StatusNotFound) |
| 19 | + }) |
| 20 | + |
| 21 | + v0 := e.Group("/v0", func(next HandlerFunc) HandlerFunc { |
| 22 | + return func(c Context) error { |
| 23 | + return next(c) |
| 24 | + } |
| 25 | + }) |
| 26 | + v0.POST("/resource", func(c Context) error { |
| 27 | + return c.NoContent(http.StatusOK) |
| 28 | + }) |
| 29 | + |
| 30 | + v1 := e.Group("/v1") |
| 31 | + v1.POST("/resource", func(c Context) error { |
| 32 | + return c.NoContent(http.StatusOK) |
| 33 | + }) |
| 34 | + |
| 35 | + srv := httptest.NewServer(e) |
| 36 | + t.Cleanup(srv.Close) |
| 37 | + |
| 38 | + for _, path := range []string{"/foo", "/v0/foo", "/v1/foo"} { |
| 39 | + t.Run(path, func(t *testing.T) { |
| 40 | + req, err := http.NewRequest(http.MethodPost, srv.URL+path, nil) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + resp, err := srv.Client().Do(req) |
| 44 | + require.NoError(t, err) |
| 45 | + defer resp.Body.Close() |
| 46 | + |
| 47 | + b, err := io.ReadAll(resp.Body) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + require.Equal(t, http.StatusNotFound, resp.StatusCode) |
| 51 | + require.Empty(t, b) |
| 52 | + require.Equal(t, "0", resp.Header.Get("Content-Length")) |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
0 commit comments