From 976b536446a77de8de2d5559c78f612970fb5e37 Mon Sep 17 00:00:00 2001 From: BlasterAlex Date: Fri, 22 Dec 2023 02:27:58 +0400 Subject: [PATCH] Fixed processing of multiple queries for the same path --- mux_test.go | 18 ++++++++++++++++++ route.go | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mux_test.go b/mux_test.go index 7f123f02..0845d7f7 100644 --- a/mux_test.go +++ b/mux_test.go @@ -2116,6 +2116,24 @@ func TestMultipleDefinitionOfSamePathWithDifferentMethods(t *testing.T) { } +func TestMultipleDefinitionOfSamePathWithDifferentQueries(t *testing.T) { + emptyHandler := func(w http.ResponseWriter, r *http.Request) {} + + r := NewRouter() + r.HandleFunc("/api", emptyHandler).Queries("foo", "{foo:[0-9]+}").Methods(http.MethodGet) + r.HandleFunc("/api", emptyHandler).Queries("bar", "{bar:[0-9]+}").Methods(http.MethodGet) + + req := newRequest(http.MethodGet, "/api?bar=4") + match := new(RouteMatch) + matched := r.Match(req, match) + if !matched { + t.Error("Should have matched route for methods") + } + if match.MatchErr != nil { + t.Error("Should have no error. Found:", match.MatchErr) + } +} + func TestErrMatchNotFound(t *testing.T) { emptyHandler := func(w http.ResponseWriter, r *http.Request) {} diff --git a/route.go b/route.go index cb89ebb8..b6582dae 100644 --- a/route.go +++ b/route.go @@ -87,7 +87,7 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool { return false } - if match.MatchErr == ErrMethodMismatch && r.handler != nil { + if match.MatchErr != nil && r.handler != nil { // We found a route which matches request method, clear MatchErr match.MatchErr = nil // Then override the mis-matched handler