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-03-15 08:42:03 +00:00
|
|
|
"github.com/containous/traefik/pkg/config"
|
|
|
|
"github.com/containous/traefik/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
|
2018-11-14 09:18:03 +00:00
|
|
|
config config.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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
Prefixes: []string{},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/noprefixes",
|
|
|
|
expectedStatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "wildcard (.*) requests",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
Prefixes: []string{"/stat/"},
|
|
|
|
},
|
2017-05-22 22:44:02 +00:00
|
|
|
path: "/status",
|
|
|
|
expectedStatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
2018-11-14 09:18:03 +00:00
|
|
|
desc: "general prefix on matching path",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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",
|
|
|
|
config: config.StripPrefix{
|
|
|
|
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)
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|