2018-11-14 09:18:03 +00:00
|
|
|
package stripprefix
|
2017-05-22 22:44:02 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2017-05-22 22:44:02 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/testhelpers"
|
2017-05-22 22:44:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStripPrefix(t *testing.T) {
|
2018-11-14 09:18:03 +00:00
|
|
|
testCases := []struct {
|
2017-05-22 22:44:02 +00:00
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
config dynamic.StripPrefix
|
2017-05-22 22:44:02 +00:00
|
|
|
path string
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedPath string
|
2017-11-21 13:28:03 +00:00
|
|
|
expectedRawPath string
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader string
|
2017-05-22 22:44:02 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "no prefixes configured",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/noprefixes",
|
2019-09-03 18:32:03 +00:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/noprefixes",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "wildcard (.*) requests",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
2019-11-14 09:32:05 +00:00
|
|
|
expectedPath: "",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "prefix and path matching",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/stat",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
2019-11-14 09:32:05 +00:00
|
|
|
expectedPath: "",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "path prefix on exactly matching path",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat/"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/stat/",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
2019-11-14 09:32:05 +00:00
|
|
|
expectedPath: "",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat/",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "path prefix on matching longer path",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat/"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/stat/us",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/us",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat/",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "path prefix on mismatching path",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat/"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/status",
|
2019-09-03 18:32:03 +00:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/status",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "general prefix on matching path",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/stat/",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "earlier prefix matching",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat", "/stat/us"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/stat/us",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/us",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "later prefix matching",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/mismatch", "/stat"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/stat",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
2019-11-14 09:32:05 +00:00
|
|
|
expectedPath: "",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
2017-10-06 09:34:03 +00:00
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "prefix matching within slash boundaries",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat"},
|
|
|
|
},
|
2017-10-06 09:34:03 +00:00
|
|
|
path: "/status",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/us",
|
|
|
|
expectedHeader: "/stat",
|
|
|
|
},
|
2017-11-21 13:28:03 +00:00
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "raw path is also stripped",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.StripPrefix{
|
2018-11-14 09:18:03 +00:00
|
|
|
Prefixes: []string{"/stat"},
|
|
|
|
},
|
2017-11-21 13:28:03 +00:00
|
|
|
path: "/stat/a%2Fb",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/a/b",
|
|
|
|
expectedRawPath: "/a%2Fb",
|
|
|
|
expectedHeader: "/stat",
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
for _, test := range testCases {
|
2017-05-22 22:44:02 +00:00
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-05-28 03:50:03 +00:00
|
|
|
|
2017-11-21 13:28:03 +00:00
|
|
|
var actualPath, actualRawPath, actualHeader, requestURI string
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
actualPath = r.URL.Path
|
|
|
|
actualRawPath = r.URL.RawPath
|
|
|
|
actualHeader = r.Header.Get(ForwardedPrefixHeader)
|
|
|
|
requestURI = r.RequestURI
|
|
|
|
})
|
|
|
|
|
2024-01-29 16:32:05 +00:00
|
|
|
pointer := func(v bool) *bool { return &v }
|
|
|
|
test.config.ForceSlash = pointer(false)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
handler, err := New(context.Background(), next, test.config, "foo-strip-prefix")
|
|
|
|
require.NoError(t, err)
|
2017-05-22 22:44:02 +00:00
|
|
|
|
2017-06-03 12:58:35 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
|
2019-09-03 18:32:03 +00:00
|
|
|
req.RequestURI = req.URL.RequestURI()
|
|
|
|
|
2017-06-01 20:09:36 +00:00
|
|
|
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
|
2017-06-03 12:58:35 +00:00
|
|
|
|
2017-06-01 20:09:36 +00:00
|
|
|
handler.ServeHTTP(resp, req)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expectedStatusCode, resp.Code, "Unexpected status code.")
|
|
|
|
assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.")
|
2017-11-21 13:28:03 +00:00
|
|
|
assert.Equal(t, test.expectedRawPath, actualRawPath, "Unexpected raw path.")
|
2017-06-01 20:09:36 +00:00
|
|
|
assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", ForwardedPrefixHeader)
|
2017-11-21 13:28:03 +00:00
|
|
|
|
2019-11-14 09:32:05 +00:00
|
|
|
expectedRequestURI := test.expectedPath
|
2017-11-21 13:28:03 +00:00
|
|
|
if test.expectedRawPath != "" {
|
|
|
|
// go HTTP uses the raw path when existent in the RequestURI
|
2019-11-14 09:32:05 +00:00
|
|
|
expectedRequestURI = test.expectedRawPath
|
|
|
|
}
|
|
|
|
if test.expectedPath == "" {
|
|
|
|
expectedRequestURI = "/"
|
2017-11-21 13:28:03 +00:00
|
|
|
}
|
2019-11-14 09:32:05 +00:00
|
|
|
assert.Equal(t, expectedRequestURI, requestURI, "Unexpected request URI.")
|
2017-05-22 22:44:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|