gorilla/mux

When I use the subrouter() method, Methods() only works on the last endpoint, and other than the last endpoint, the rest of the endpoints give a 404 instead of a 405 with the unrelated http method.

iarsham opened this issue · 3 comments

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When I use the subrouter() method, Methods() only works on the last endpoint, and other than the last endpoint, the rest of the endpoints give a 404 instead of a 405 with the unrelated http method.

image

last endpoint :
image

other endpoints:
image

Expected Behavior

No response

Steps To Reproduce

No response

Anything else?

No response

Does #748 fix this for you?

Does #748 fix this for you?

no!

This appears to have been broken by #712, which clears a 405 error when a subsequent route matches in any part. The logic here is tricky: the desire is that if

Route 1: POST "/users/{id}".
Route 2: GET "/users/{id}", parameters: "id": "[0-9]+".

are configured, then GET "/users/-2" returns a 404, not a 405, because while it matches route 1 with a method error, it is a "better" match to route 2 with a parameter validation error.

But this logic then also applies to prefix matching in a sub router: the prefix match is the first matcher in the sequence, and a subsequent route that matches the prefix but is otherwise clearly not a match will clear an existing 405. Unfortunately I don't know the mux internals well enough to suggest a fix, but it seems like if the check from #712 is desired, it shouldn't apply to subroute matchers.