traefik/pkg/middlewares/stripprefix/strip_prefix_test.go

170 lines
4.4 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package stripprefix
import (
2018-11-14 09:18:03 +00:00
"context"
"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"
"github.com/stretchr/testify/assert"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/require"
)
func TestStripPrefix(t *testing.T) {
2018-11-14 09:18:03 +00:00
testCases := []struct {
desc string
2018-11-14 09:18:03 +00:00
config config.StripPrefix
path string
expectedStatusCode int
expectedPath string
2017-11-21 13:28:03 +00:00
expectedRawPath string
expectedHeader string
}{
{
2018-11-14 09:18:03 +00:00
desc: "no prefixes configured",
config: config.StripPrefix{
Prefixes: []string{},
},
path: "/noprefixes",
expectedStatusCode: http.StatusNotFound,
},
{
2018-11-14 09:18:03 +00:00
desc: "wildcard (.*) requests",
config: config.StripPrefix{
Prefixes: []string{"/"},
},
path: "/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/",
},
{
2018-11-14 09:18:03 +00:00
desc: "prefix and path matching",
config: config.StripPrefix{
Prefixes: []string{"/stat"},
},
path: "/stat",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
2018-11-14 09:18:03 +00:00
desc: "path prefix on exactly matching path",
config: config.StripPrefix{
Prefixes: []string{"/stat/"},
},
path: "/stat/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat/",
},
{
2018-11-14 09:18:03 +00:00
desc: "path prefix on matching longer path",
config: config.StripPrefix{
Prefixes: []string{"/stat/"},
},
path: "/stat/us",
expectedStatusCode: http.StatusOK,
expectedPath: "/us",
expectedHeader: "/stat/",
},
{
2018-11-14 09:18:03 +00:00
desc: "path prefix on mismatching path",
config: config.StripPrefix{
Prefixes: []string{"/stat/"},
},
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"},
},
path: "/stat/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
2018-11-14 09:18:03 +00:00
desc: "earlier prefix matching",
config: config.StripPrefix{
Prefixes: []string{"/stat", "/stat/us"},
},
path: "/stat/us",
expectedStatusCode: http.StatusOK,
expectedPath: "/us",
expectedHeader: "/stat",
},
{
2018-11-14 09:18:03 +00:00
desc: "later prefix matching",
config: config.StripPrefix{
Prefixes: []string{"/mismatch", "/stat"},
},
path: "/stat",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
2018-11-14 09:18:03 +00:00
desc: "prefix matching within slash boundaries",
config: config.StripPrefix{
Prefixes: []string{"/stat"},
},
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",
},
}
2018-11-14 09:18:03 +00:00
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
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-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.")
})
}
}