Skip to content

URL is escaped twice on trailing slash redirect #59

@Backfighter

Description

@Backfighter

The following test uncovers an issue with the current implementation of httptreemux:

func TestRedirectEscapedPath(t *testing.T) {
	router := NewContextMux()

	testHandler := func(w http.ResponseWriter, r *http.Request) {}

	router.GET("/:escaped/", testHandler)

	w := httptest.NewRecorder()
	u, err := url.Parse("/Test P@th")
	if err != nil {
		t.Error(err)
		return
	}
	r := httptest.NewRequest(http.MethodGet, u.String(), nil)

	router.ServeHTTP(w, r)

	path, err := w.Result().Location()
	if err != nil {
		t.Error(err)
		return
	}
	expected := "/Test%20P@th/"
	if path.String() != expected {
		t.Errorf("Given path wasn't escaped correctly.\n" +
			"Expected: %q\nBut got: %q", expected, path.String())
	}
}

This is caused since in lookup, path is used for redirects but it still contains escaped characters. Then in redirect newUrl.String() is called which results in double encoded characters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions