Skip to content

Commit 9ce1e45

Browse files
author
maknahar
committed
Added custom MethodNotAllowedHandler in Router
1 parent a86d565 commit 9ce1e45

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

mux.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func NewRouter() *Router {
4343
type Router struct {
4444
// Configurable Handler to be used when no route matches.
4545
NotFoundHandler http.Handler
46+
47+
// Configurable Handler to be used request has method mismatch with route
48+
MethodNotAllowedHandler http.Handler
49+
4650
// Parent route, if this is a subrouter.
4751
parent parentRoute
4852
// Routes to be matched, in order.
@@ -69,6 +73,11 @@ func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
6973
}
7074
}
7175

76+
if match.MatchErr == ErrMethodMismatch && r.MethodNotAllowedHandler != nil {
77+
match.Handler = r.MethodNotAllowedHandler
78+
return true
79+
}
80+
7281
// Closest match for a router (includes sub-routers)
7382
if r.NotFoundHandler != nil {
7483
match.Handler = r.NotFoundHandler

mux_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,8 +1876,7 @@ func TestNoMatchMethodErrorHandler(t *testing.T) {
18761876
func1 := func(w http.ResponseWriter, r *http.Request) {}
18771877

18781878
r := NewRouter()
1879-
s := r.Methods("GET", "POST").Subrouter()
1880-
s.HandleFunc("/", func1).Name("func1")
1879+
r.HandleFunc("/", func1).Methods("GET", "POST")
18811880

18821881
req, _ := http.NewRequest("PUT", "http://localhost/", nil)
18831882
match := new(RouteMatch)
@@ -1896,4 +1895,18 @@ func TestNoMatchMethodErrorHandler(t *testing.T) {
18961895
if resp.Code != 405 {
18971896
t.Errorf("Expecting code %v", 405)
18981897
}
1898+
1899+
//Add matching route now
1900+
r.HandleFunc("/", func1).Methods("PUT")
1901+
1902+
match = new(RouteMatch)
1903+
matched = r.Match(req, match)
1904+
1905+
if !matched {
1906+
t.Error("Should have matched route for methods")
1907+
}
1908+
1909+
if match.MatchErr != nil {
1910+
t.Error("Should not have any matching error. Found:", match.MatchErr)
1911+
}
18991912
}

route.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,26 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
5353
return false
5454
}
5555

56+
var matchErr error
57+
5658
// Match everything.
5759
for _, m := range r.matchers {
5860
if matched := m.Match(req, match); !matched {
5961
if _, ok := m.(methodMatcher); ok {
60-
match.MatchErr = ErrMethodMismatch
62+
matchErr = ErrMethodMismatch
6163
continue
6264
}
65+
matchErr = nil
6366
return false
6467
}
6568
}
6669

67-
if match.MatchErr != nil {
70+
if matchErr != nil {
71+
match.MatchErr = matchErr
6872
return false
6973
}
7074

75+
match.MatchErr = nil
7176
// Yay, we have a match. Let's collect some info about it.
7277
if match.Route == nil {
7378
match.Route = r

0 commit comments

Comments
 (0)