Skip to content

Commit 235709b

Browse files
author
maknahar
committed
Addressed Review Comments and a Possible Bug
1 parent 7faf53a commit 235709b

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

mux.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
"strings"
1414
)
1515

16+
var (
17+
ErrMethodMismatch = errors.New("Method is Not Allowed")
18+
)
19+
1620
// NewRouter returns a new router instance.
1721
func NewRouter() *Router {
1822
return &Router{namedRoutes: make(map[string]*Route), KeepContext: false}
@@ -106,8 +110,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
106110
req = setCurrentRoute(req, match.Route)
107111
}
108112

109-
if match.MethodMismatch {
110-
handler = MethodNotAllowedHandler()
113+
if handler == nil && match.MatchErr == ErrMethodMismatch {
114+
handler = methodNotAllowedHandler()
111115
}
112116

113117
if handler == nil {
@@ -351,9 +355,10 @@ type RouteMatch struct {
351355
Handler http.Handler
352356
Vars map[string]string
353357

354-
// MethodMismatch flag is set to true if a route is matched but there
355-
// is a mismatch in the request method and route method
356-
MethodMismatch bool
358+
// MatchErr is set to appropriate matching error
359+
// It is set to ErrMethodMismatch if there is a mismatch in
360+
// the request method and route method
361+
MatchErr error
357362
}
358363

359364
type contextKey int
@@ -556,11 +561,11 @@ func matchMapWithRegex(toCheck map[string]*regexp.Regexp, toMatch map[string][]s
556561
return true
557562
}
558563

559-
// MethodNotAllowed replies to the request with an HTTP status code 405.
560-
func MethodNotAllowed(w http.ResponseWriter, r *http.Request) {
564+
// methodNotAllowed replies to the request with an HTTP status code 405.
565+
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
561566
w.WriteHeader(http.StatusMethodNotAllowed)
562567
}
563568

564-
// MethodNotAllowedHandler returns a simple request handler
569+
// methodNotAllowedHandler returns a simple request handler
565570
// that replies to each request with a status code 405.
566-
func MethodNotAllowedHandler() http.Handler { return http.HandlerFunc(MethodNotAllowed) }
571+
func methodNotAllowedHandler() http.Handler { return http.HandlerFunc(methodNotAllowed) }

mux_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,10 @@ func TestNoMatchMethodErrors(t *testing.T) {
18871887
t.Error("Should not have matched route for methods")
18881888
}
18891889

1890+
if match.MatchErr != ErrMethodMismatch {
1891+
t.Error("Should get ErrMethodMismatch error")
1892+
}
1893+
18901894
resp := NewRecorder()
18911895
r.ServeHTTP(resp, req)
18921896
if resp.Code != 405 {

route.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
5757
for _, m := range r.matchers {
5858
if matched := m.Match(req, match); !matched {
5959
if _, ok := m.(methodMatcher); ok {
60-
match.MethodMismatch = true
60+
match.MatchErr = ErrMethodMismatch
6161
continue
6262
}
6363
return false
6464
}
6565
}
6666

67-
if match.MethodMismatch {
67+
if match.MatchErr != nil {
6868
return false
6969
}
7070

0 commit comments

Comments
 (0)