Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

buffalo allows mux to handle 405 responses fixes #978 #979

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()
Expand Down