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

Ignore ErrNotFound while matching Subrouters #438

Merged
merged 1 commit into from
Jan 8, 2019

Conversation

g-w
Copy link
Contributor

@g-w g-w commented Jan 7, 2019

MatchErr is set by the router to ErrNotFound if no route matches. If
no route of a Subrouter matches the error can by safely ignored. This
implementation only ignores these errors and does not ignore other
errors like ErrMethodMismatch.

@kisielk
Copy link
Contributor

kisielk commented Jan 7, 2019

What if you have a router whose only routes are subrouters, and all of them return ErrNotFound?

@g-w
Copy link
Contributor Author

g-w commented Jan 7, 2019

In this case no route should match and https://github.com/gorilla/mux/blob/master/mux.go#L167 should be reached. This sets MatchErr to ErrNotFound.

I've checked this through this test:

func TestMethodSubrouterNotFound(t *testing.T) {
	handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }
	router := NewRouter()
	router.Path("/a").Subrouter().HandleFunc("/thing", handler).Methods(http.MethodGet)
	router.Path("/b").Subrouter().HandleFunc("/something", handler).Methods(http.MethodGet)

	w := NewRecorder()
	req := newRequest(http.MethodPut, "/not-present")

	router.ServeHTTP(w, req)

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

If you think it useful, I will add it to my PR.

BTW: This fixes #437

@kisielk
Copy link
Contributor

kisielk commented Jan 7, 2019

Yes I think adding this test is useful just to verify that this and any future changes won't break this.

MatchErr is set by the router to ErrNotFound if no route matches. If
no route of a Subrouter matches the error can by safely ignored. This
implementation only ignores these errors and does not ignore other
errors like ErrMethodMismatch.
@g-w
Copy link
Contributor Author

g-w commented Jan 7, 2019

I've added the test.

Copy link
Contributor

@elithrar elithrar left a comment

Choose a reason for hiding this comment

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

LGTM

@elithrar elithrar merged commit 08e7f80 into gorilla:master Jan 8, 2019
@elithrar
Copy link
Contributor

elithrar commented Jan 8, 2019

Thanks @g-w!

@g-w g-w deleted the fix-method-not-allowed branch January 8, 2019 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants