From daf8793934747a449e00a2a7707681a6993269a5 Mon Sep 17 00:00:00 2001 From: Roy Kim Date: Fri, 11 Oct 2024 12:38:22 -0500 Subject: [PATCH] address Issue-751 with method mismatch flag in Route.Match() --- route.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/route.go b/route.go index d10401e9..4eba5343 100644 --- a/route.go +++ b/route.go @@ -50,12 +50,15 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool { } var matchErr error + methodMismatch := false // Track method mismatch // Match everything. for _, m := range r.matchers { if matched := m.Match(req, match); !matched { + // Detect mismatch due to HTTP method if _, ok := m.(methodMatcher); ok { matchErr = ErrMethodMismatch + methodMismatch = true // Flag method mismatch continue } @@ -88,6 +91,13 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool { } } + // If flagged method mismatch, return 405 + if methodMismatch { + match.MatchErr = ErrMethodMismatch + return false + } + + // No match errors, but handle method matches if matchErr != nil { match.MatchErr = matchErr return false