2017-06-06 11:19:01 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/containous/traefik/testhelpers"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-06 11:19:01 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddPrefix(t *testing.T) {
|
2018-11-14 09:18:03 +00:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2017-12-15 02:50:07 +00:00
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
prefix string
|
|
|
|
path string
|
|
|
|
expectedPath string
|
|
|
|
expectedRawPath string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "regular path",
|
|
|
|
prefix: "/a",
|
|
|
|
path: "/b",
|
|
|
|
expectedPath: "/a/b",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "raw path is supported",
|
|
|
|
prefix: "/a",
|
|
|
|
path: "/b%2Fc",
|
|
|
|
expectedPath: "/a/b/c",
|
|
|
|
expectedRawPath: "/a/b%2Fc",
|
|
|
|
},
|
|
|
|
}
|
2017-06-06 11:19:01 +00:00
|
|
|
|
2017-12-15 02:50:07 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-06-06 11:19:01 +00:00
|
|
|
|
2017-12-15 02:50:07 +00:00
|
|
|
var actualPath, actualRawPath, requestURI string
|
|
|
|
handler := &AddPrefix{
|
|
|
|
Prefix: test.prefix,
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
actualPath = r.URL.Path
|
|
|
|
actualRawPath = r.URL.RawPath
|
|
|
|
requestURI = r.RequestURI
|
|
|
|
}),
|
|
|
|
}
|
2017-06-06 11:19:01 +00:00
|
|
|
|
2017-12-15 02:50:07 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
|
2017-06-06 11:19:01 +00:00
|
|
|
|
2017-12-15 02:50:07 +00:00
|
|
|
handler.ServeHTTP(nil, req)
|
2017-06-06 11:19:01 +00:00
|
|
|
|
2017-12-15 02:50:07 +00:00
|
|
|
assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.")
|
|
|
|
assert.Equal(t, test.expectedRawPath, actualRawPath, "Unexpected raw path.")
|
|
|
|
|
|
|
|
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-06-06 11:19:01 +00:00
|
|
|
}
|