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"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/testhelpers"
|
2017-05-22 22:44:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
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,
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
expectedURI := test.expectedPath
|
|
|
|
if test.expectedRawPath != "" {
|
|
|
|
// go HTTP uses the raw path when existent in the RequestURI
|
|
|
|
expectedURI = test.expectedRawPath
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedURI, requestURI, "Unexpected request URI.")
|
2017-05-22 22:44:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|