Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes path order dependent response when a nil handler is defined for a route #517

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2803,6 +2803,48 @@ func TestSubrouterNotFound(t *testing.T) {
}
}

// testOptionsMiddleWare returns 200 on an OPTIONS request
func testOptionsMiddleWare(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
inner.ServeHTTP(w, r)
})
}

// TestRouterOrder Should Pass whichever order route is defined
func TestRouterOrder(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {}
router := NewRouter()
router.Path("/a/b").Handler(http.HandlerFunc(handler)).Methods(http.MethodGet)
router.Path("/a/{a}").Handler(nil).Methods(http.MethodOptions)
router.Use(MiddlewareFunc(testOptionsMiddleWare))

w := NewRecorder()
req := newRequest(http.MethodOptions, "/a/b")

router.ServeHTTP(w, req)

if w.Code != http.StatusOK {
t.Fatalf("Expected status code 200 (got %d)", w.Code)
}

reversedPathRouter := NewRouter()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break these into subtests - e.g.

t.Run("Test handler ordering", func(t *testing.T) { 
   // test 
})

t.Run("Test reversed handler ordering", func(t *testing.T) { 
   // test 
})

I would also add additional test cases:

  1. Test ordering where handlers have the same method & path, but a different handler (expected: the first handler added is the one executed)
  2. Same path, different methods (no path variables)
  3. Same as Host() with a port fails to match when the host is supplied in the HTTP Host: header #2, but with middleware

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have made the listed changes

reversedPathRouter.Path("/a/{a}").Handler(http.HandlerFunc(handler)).Methods(http.MethodGet)
reversedPathRouter.Path("/a/b").Handler(nil).Methods(http.MethodOptions)
reversedPathRouter.Use(MiddlewareFunc(testOptionsMiddleWare))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be before the paths are added.


w = NewRecorder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it a subtest (as above) and call newRequest again to separate it.


reversedPathRouter.ServeHTTP(w, req)

if w.Code != http.StatusOK {
t.Fatalf("Expected status code 200 (got %d)", w.Code)
}
}

// mapToPairs converts a string map to a slice of string pairs
func mapToPairs(m map[string]string) []string {
var i int
Expand Down
6 changes: 4 additions & 2 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ func (r *Route) Match(req *http.Request, match *RouteMatch) bool {
return false
}

if match.MatchErr == ErrMethodMismatch && r.handler != nil {
if match.MatchErr == ErrMethodMismatch {
coreydaley marked this conversation as resolved.
Show resolved Hide resolved
// We found a route which matches request method, clear MatchErr
match.MatchErr = nil
// Then override the mis-matched handler
match.Handler = r.handler
coreydaley marked this conversation as resolved.
Show resolved Hide resolved
if r.handler != nil {
match.Handler = r.handler
}
}

// Yay, we have a match. Let's collect some info about it.
Expand Down