2017-05-22 22:44:02 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2017-06-03 12:58:35 +00:00
|
|
|
"github.com/containous/traefik/testhelpers"
|
2017-05-22 22:44:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStripPrefix(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
prefixes []string
|
|
|
|
path string
|
|
|
|
expectedStatusCode int
|
|
|
|
expectedPath string
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader string
|
2017-05-22 22:44:02 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no prefixes configured",
|
|
|
|
prefixes: []string{},
|
|
|
|
path: "/noprefixes",
|
|
|
|
expectedStatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "wildcard (.*) requests",
|
|
|
|
prefixes: []string{"/"},
|
|
|
|
path: "/",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "prefix and path matching",
|
|
|
|
prefixes: []string{"/stat"},
|
|
|
|
path: "/stat",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path prefix on exactly matching path",
|
|
|
|
prefixes: []string{"/stat/"},
|
|
|
|
path: "/stat/",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat/",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path prefix on matching longer path",
|
|
|
|
prefixes: []string{"/stat/"},
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path prefix on mismatching path",
|
|
|
|
prefixes: []string{"/stat/"},
|
|
|
|
path: "/status",
|
|
|
|
expectedStatusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "general prefix on matching path",
|
|
|
|
prefixes: []string{"/stat"},
|
|
|
|
path: "/stat/",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "earlier prefix matching",
|
|
|
|
prefixes: []string{"/stat", "/stat/us"},
|
|
|
|
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
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "later prefix matching",
|
|
|
|
prefixes: []string{"/mismatch", "/stat"},
|
|
|
|
path: "/stat",
|
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
expectedPath: "/",
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedHeader: "/stat",
|
2017-05-22 22:44:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-05-28 03:50:03 +00:00
|
|
|
|
|
|
|
var actualPath, actualHeader string
|
2017-06-01 20:09:36 +00:00
|
|
|
handler := &StripPrefix{
|
2017-05-22 22:44:02 +00:00
|
|
|
Prefixes: test.prefixes,
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-05-28 03:50:03 +00:00
|
|
|
actualPath = r.URL.Path
|
|
|
|
actualHeader = r.Header.Get(ForwardedPrefixHeader)
|
2017-05-22 22:44:02 +00:00
|
|
|
}),
|
2017-06-01 20:09:36 +00:00
|
|
|
}
|
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.")
|
|
|
|
assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", ForwardedPrefixHeader)
|
2017-05-22 22:44:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|