File tree Expand file tree Collapse file tree 3 files changed +31
-4
lines changed Expand file tree Collapse file tree 3 files changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ func NewRouter() *Router {
43
43
type Router struct {
44
44
// Configurable Handler to be used when no route matches.
45
45
NotFoundHandler http.Handler
46
+
47
+ // Configurable Handler to be used request has method mismatch with route
48
+ MethodNotAllowedHandler http.Handler
49
+
46
50
// Parent route, if this is a subrouter.
47
51
parent parentRoute
48
52
// Routes to be matched, in order.
@@ -69,6 +73,11 @@ func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
69
73
}
70
74
}
71
75
76
+ if match .MatchErr == ErrMethodMismatch && r .MethodNotAllowedHandler != nil {
77
+ match .Handler = r .MethodNotAllowedHandler
78
+ return true
79
+ }
80
+
72
81
// Closest match for a router (includes sub-routers)
73
82
if r .NotFoundHandler != nil {
74
83
match .Handler = r .NotFoundHandler
Original file line number Diff line number Diff line change @@ -1876,8 +1876,7 @@ func TestNoMatchMethodErrorHandler(t *testing.T) {
1876
1876
func1 := func (w http.ResponseWriter , r * http.Request ) {}
1877
1877
1878
1878
r := NewRouter ()
1879
- s := r .Methods ("GET" , "POST" ).Subrouter ()
1880
- s .HandleFunc ("/" , func1 ).Name ("func1" )
1879
+ r .HandleFunc ("/" , func1 ).Methods ("GET" , "POST" )
1881
1880
1882
1881
req , _ := http .NewRequest ("PUT" , "http://localhost/" , nil )
1883
1882
match := new (RouteMatch )
@@ -1896,4 +1895,18 @@ func TestNoMatchMethodErrorHandler(t *testing.T) {
1896
1895
if resp .Code != 405 {
1897
1896
t .Errorf ("Expecting code %v" , 405 )
1898
1897
}
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
+ }
1899
1912
}
Original file line number Diff line number Diff line change @@ -53,21 +53,26 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
53
53
return false
54
54
}
55
55
56
+ var matchErr error
57
+
56
58
// Match everything.
57
59
for _ , m := range r .matchers {
58
60
if matched := m .Match (req , match ); ! matched {
59
61
if _ , ok := m .(methodMatcher ); ok {
60
- match . MatchErr = ErrMethodMismatch
62
+ matchErr = ErrMethodMismatch
61
63
continue
62
64
}
65
+ matchErr = nil
63
66
return false
64
67
}
65
68
}
66
69
67
- if match .MatchErr != nil {
70
+ if matchErr != nil {
71
+ match .MatchErr = matchErr
68
72
return false
69
73
}
70
74
75
+ match .MatchErr = nil
71
76
// Yay, we have a match. Let's collect some info about it.
72
77
if match .Route == nil {
73
78
match .Route = r
You can’t perform that action at this time.
0 commit comments