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

SkipClean does not work on Subrouters or any subroutes #460

Closed
gavbaa opened this issue Mar 13, 2019 · 3 comments
Closed

SkipClean does not work on Subrouters or any subroutes #460

gavbaa opened this issue Mar 13, 2019 · 3 comments
Labels

Comments

@gavbaa
Copy link

gavbaa commented Mar 13, 2019

What version of Go are you running? (Paste the output of go version)
go version go1.11.5 darwin/amd64

What version of gorilla/mux are you at? (Paste the output of git rev-parse HEAD inside $GOPATH/src/github.com/gorilla/mux)
15a353a

Describe your problem (and what you have tried so far)
SkipClean when applied only on subrouters does not check for cleaned URLs on subroutes. It only applies to the entire routing event, on or off.

What we expect to happen: On routes that are declared with SkipClean(false), those routes are checked against the cleaned URL. On routes that are declared with SkipClean(true), those routes are checked against the original URL.

Or else, SkipClean should only be allowed on the parent Router, and not on any subrouters, since it's currently ineffective.

(This probably opens the door for also processing those routes as an internal redirect instead of a public 302 as an option, but I think that should be a separate ticket and isn't required for this behavior).

Paste a minimal, runnable, reproduction of your issue below (use backticks to format it)

func TestSkipCleanSubrouter(t *testing.T) {
	func1 := func(w http.ResponseWriter, r *http.Request) {}
	func2 := func(w http.ResponseWriter, r *http.Request) {}

	r := NewRouter()
	r.HandleFunc("/api/", func1).Name("func2")
	r.SkipClean(true)
	subRouter := r.PathPrefix("/v0").Subrouter().SkipClean(false)
	subRouter.HandleFunc("/action/do/", func2).Name("func2")

	req, _ := http.NewRequest("GET", "http://localhost/api/?abc=def", nil)
	res := NewRecorder()
	r.ServeHTTP(res, req)

	if len(res.HeaderMap["Location"]) != 0 {
		t.Errorf("Req 1: Shouldn't redirect since route is already clean")
	}

	req, _ = http.NewRequest("GET", "http://localhost//api/?abc=def", nil)
	res = NewRecorder()
	r.ServeHTTP(res, req)

	if len(res.HeaderMap["Location"]) != 0 {
		t.Errorf("Req 2: Shouldn't redirect since skip clean is disabled")
	}

	req, _ = http.NewRequest("GET", "http://localhost/v0/action//do/?ghi=jkl", nil)
	res = NewRecorder()
	r.ServeHTTP(res, req)

	if len(res.HeaderMap["Location"]) == 0 {
		t.Errorf("Req 3: Should redirect since skip clean is enabled for subroute")
	}
}

Expected:
Tests pass.

Actual:

--- FAIL: TestSkipCleanSubrouter (0.00s)
    mux_test.go:2029: Req 3: Should redirect since skip clean is enabled for subroute

I'm happy to provide a patch that would address this in whichever direction should be supported. I'm of the opinion that subroutes should be able to determine whether or not they should be tested against the original or the cleaned URL.

@elithrar
Copy link
Contributor

elithrar commented Mar 13, 2019 via email

@gavbaa
Copy link
Author

gavbaa commented Mar 21, 2019

PR provided in #463 . New tests added, all existing tests pass. Would welcome a review.

@stale
Copy link

stale bot commented May 20, 2019

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@stale stale bot added the stale label May 20, 2019
@stale stale bot closed this as completed May 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants