diff --git a/app.go b/app.go index 78dca64b5..e147a0bc1 100644 --- a/app.go +++ b/app.go @@ -157,6 +157,12 @@ func New(opts Options) *App { a.ErrorHandlers.Get(404)(404, err, c) }) + a.router.MethodNotAllowedHandler = http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + c := a.newContext(RouteInfo{}, res, req) + err := errors.Errorf("method not found: %s %s", req.Method, req.URL.Path) + a.ErrorHandlers.Get(405)(405, err, c) + }) + if a.MethodOverride == nil { a.MethodOverride = MethodOverride } diff --git a/router_test.go b/router_test.go index 30527de75..c04ce2b7f 100644 --- a/router_test.go +++ b/router_test.go @@ -35,6 +35,13 @@ func testApp() *App { rt.DELETE("/", h) rt.OPTIONS("/", h) rt.PATCH("/", h) + + a.ErrorHandlers[405] = func(status int, err error, c Context) error { + res := c.Response() + res.WriteHeader(status) + res.Write([]byte("my custom 405")) + return nil + } return a } @@ -50,6 +57,14 @@ func otherTestApp() *App { return a } +func Test_MethodNotFoundError(t *testing.T) { + r := require.New(t) + w := willie.New(testApp()) + res := w.HTML("/bar").Post(nil) + r.Equal(405, res.Code) + r.Contains(res.Body.String(), "my custom 405") +} + func Test_Mount_Buffalo(t *testing.T) { r := require.New(t) a := testApp()