Skip to content

Subrouters match the wrong method when more than one matches #300

Closed
@silverweed

Description

Introduced by #288.
Given the following code:

package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
)

func main() {
	router := mux.NewRouter()
	get := router.Methods("GET", "HEAD").Subrouter()
	post := router.Methods("POST").Subrouter()

	post.HandleFunc("/some/{thing}", func(rw http.ResponseWriter, req *http.Request) {
		fmt.Fprintln(rw, "post")
	})
	get.HandleFunc("/some/{thing}", func(rw http.ResponseWriter, req *http.Request) {
		fmt.Fprintln(rw, "get")
	})

	http.Handle("/", router)
	http.ListenAndServe(":8888", nil)
}

a POST request to /some/thing will always match the get subrouter:

curl localhost:8888/some/thing -X POST # -> get

The same happens when there is a subrouter with a "catch-all" route, like

get.HandleFunc("/{anything}", getAnything)

This route will match any valid POST route without parameters.

These behaviours happen no matter the calling order of the HandleFuncs.

Commenting these lines seems to solve the issue, but of course breaks #288

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions