-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RouteNotFound handler does not falls back to root one #2485
Comments
this is somewhat of a "feature" because of this block Lines 21 to 32 in a2e7085
|
@aldas thanks for your reply! From a framework user perspective this behaviour is surprising. The fact that it happens only when a middleware is added to the group make it feel more like an unintended side effect than a desired behaviour. The reason why this behaviour happens is also a bit obscure, in particular the wording Would it be possible to consider a change in behaviour to make it less surprising? From the framework user perspective the ideal behaviour would be to use Echo global route not found handlers for paths In case this isn't be possible, would it be ok to add/update the documentation to signal this particular behaviour? |
I think I have found a bug, that is either related to this issue or happens because of the same "somewhat feature". Due to the added NotFoundHandlers when using Middlewares on Groups, echo does not return a method not allowed status when using the wrong method. It always returns a not found status, even if the route exists with a different method. package main_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/require"
)
func server() http.Handler {
e := echo.New()
// group without middle ware
g1 := e.Group("/api/v1")
g1.GET("/hello", func(c echo.Context) error {
return c.String(200, "Hello, World!")
})
// group with middle ware
g2 := e.Group("/api/v2")
g2.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return next(c)
}
})
g2.GET("/hello", func(c echo.Context) error {
return c.String(200, "Hello, World!")
})
return e
}
func TestMethodNotAllowed(t *testing.T) {
srv := httptest.NewServer(server())
req, _ := http.NewRequest(http.MethodPost, srv.URL+"/api/v1/hello", nil)
resp, err := srv.Client().Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode, "/api/v1/hello should not allow POST and return 405")
req, _ = http.NewRequest(http.MethodPost, srv.URL+"/api/v2/hello", nil)
resp, err = srv.Client().Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode, "/api/v2/hello should not allow POST and return 405")
} |
@escb005, yep, it does not seems to work properly. I'll take a look into it. It is probably time to investigate how to remove these 2 hidden routes that are added and replace them with something else that helps to execute middlewares. |
do i really need to do the below? this is the only way i could make it work. Would it not be better to default to all routes using the root level RouteNotFound but you can override if you want as i would think this would be the wanted behavour 99% of the time. Or am i missing another way of doing this?
|
Issue Description
I think I found a bug in how
RouteNotFound
handler is propagated to route groups.When using groups with a dedicated middleware there is no fallback to the root
RouteNotFound
handler, while that happens when no middleware is specified.Checklist
Expected behaviour
The root
RouteNotFound
handler is used.Actual behaviour
An internal(?) handler is used
Steps to reproduce
Put the code below in a test file, run
go test ./...
Working code to debug
This is a test file that reproduce the issue.
Version/commit
github.com/labstack/echo/v4 v4.11.1
The text was updated successfully, but these errors were encountered: