Skip to content

Status is 404 Not Found instead of 405 Method Not Allowed #437

@g-w

Description

@g-w

What version of Go are you running?

go version go1.11.4 linux/amd64

What version of gorilla/mux are you at?

f3ff42f

Describe your problem

Currently the router returns 404 instead of 405 if the path matches but not the method. This is only the case if another not matching route was registered after the matching one.

This issues seems to be introduced through the changes in ef912dd .

Paste a minimal, runnable, reproduction of your issue below

func TestMethodNotAllowed(t *testing.T) {
	router := NewRouter()
	router.HandleFunc("/thing", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }).Methods(http.MethodGet)
	router.HandleFunc("/something", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }).Methods(http.MethodPost)

	w := NewRecorder()
	req := newRequest(http.MethodPut, "/thing")

	router.ServeHTTP(w, req)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions