traefik/pkg/middlewares/stripprefix/strip_prefix_test.go

179 lines
4.7 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"
"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"
)
func TestStripPrefix(t *testing.T) {
2018-11-14 09:18:03 +00:00
testCases := []struct {
desc string
config dynamic.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: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
Prefixes: []string{},
},
path: "/noprefixes",
2019-09-03 18:32:03 +00:00
expectedStatusCode: http.StatusOK,
expectedPath: "/noprefixes",
},
{
2018-11-14 09:18:03 +00:00
desc: "wildcard (.*) requests",
config: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
Prefixes: []string{"/"},
},
path: "/",
expectedStatusCode: http.StatusOK,
expectedPath: "",
expectedHeader: "/",
},
{
2018-11-14 09:18:03 +00:00
desc: "prefix and path matching",
config: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
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: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
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: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
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: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
Prefixes: []string{"/stat/"},
},
path: "/status",
2019-09-03 18:32:03 +00:00
expectedStatusCode: http.StatusOK,
expectedPath: "/status",
},
{
2018-11-14 09:18:03 +00:00
desc: "general prefix on matching path",
config: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
Prefixes: []string{"/stat"},
},
path: "/stat/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
2018-11-14 09:18:03 +00:00
desc: "earlier prefix matching",
config: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
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: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
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: dynamic.StripPrefix{
2018-11-14 09:18:03 +00:00
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: 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",
},
}
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
})
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-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
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
expectedRequestURI = test.expectedRawPath
}
if test.expectedPath == "" {
expectedRequestURI = "/"
2017-11-21 13:28:03 +00:00
}
assert.Equal(t, expectedRequestURI, requestURI, "Unexpected request URI.")
})
}
}