add prefix from X-Forwarded-Prefix in redirectTrailingSlash (#1238)

* add prefix from X-Forwarded-Prefix in redirectTrailingSlash

* added test

* fix path import
This commit is contained in:
Tudor Roman
2019-02-27 13:56:29 +02:00
committed by 田欧
parent e207a3ce65
commit ccb105dbcb
2 changed files with 25 additions and 6 deletions
+16 -1
View File
@@ -16,8 +16,16 @@ import (
"github.com/stretchr/testify/assert"
)
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
type header struct {
Key string
Value string
}
func performRequest(r http.Handler, method, path string, headers ...header) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
for _, h := range headers {
req.Header.Add(h.Key, h.Value)
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
@@ -170,6 +178,13 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
w = performRequest(router, "PUT", "/path4/")
assert.Equal(t, http.StatusOK, w.Code)
w = performRequest(router, "GET", "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
assert.Equal(t, 301, w.Code)
w = performRequest(router, "GET", "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
assert.Equal(t, 200, w.Code)
router.RedirectTrailingSlash = false
w = performRequest(router, "GET", "/path/")