2018-11-14 09:18:03 +00:00
|
|
|
package replacepathregex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2020-03-03 15:20:05 +00:00
|
|
|
"net/http/httptest"
|
2018-11-14 09:18:03 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/replacepath"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReplacePathRegex(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2020-03-03 15:20:05 +00:00
|
|
|
desc string
|
|
|
|
path string
|
|
|
|
config dynamic.ReplacePathRegex
|
|
|
|
expectedPath string
|
|
|
|
expectedRawPath string
|
|
|
|
expectedHeader string
|
|
|
|
expectsError bool
|
2018-11-14 09:18:03 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "simple regex",
|
|
|
|
path: "/whoami/and/whoami",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.ReplacePathRegex{
|
2018-11-14 09:18:03 +00:00
|
|
|
Replacement: "/who-am-i/$1",
|
|
|
|
Regex: `^/whoami/(.*)`,
|
|
|
|
},
|
2020-03-03 15:20:05 +00:00
|
|
|
expectedPath: "/who-am-i/and/whoami",
|
|
|
|
expectedRawPath: "/who-am-i/and/whoami",
|
|
|
|
expectedHeader: "/whoami/and/whoami",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "simple replace (no regex)",
|
|
|
|
path: "/whoami/and/whoami",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.ReplacePathRegex{
|
2018-11-14 09:18:03 +00:00
|
|
|
Replacement: "/who-am-i",
|
|
|
|
Regex: `/whoami`,
|
|
|
|
},
|
2020-03-03 15:20:05 +00:00
|
|
|
expectedPath: "/who-am-i/and/who-am-i",
|
|
|
|
expectedRawPath: "/who-am-i/and/who-am-i",
|
|
|
|
expectedHeader: "/whoami/and/whoami",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no match",
|
|
|
|
path: "/whoami/and/whoami",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.ReplacePathRegex{
|
2018-11-14 09:18:03 +00:00
|
|
|
Replacement: "/whoami",
|
|
|
|
Regex: `/no-match`,
|
|
|
|
},
|
|
|
|
expectedPath: "/whoami/and/whoami",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multiple replacement",
|
|
|
|
path: "/downloads/src/source.go",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.ReplacePathRegex{
|
2018-11-14 09:18:03 +00:00
|
|
|
Replacement: "/downloads/$1-$2",
|
|
|
|
Regex: `^(?i)/downloads/([^/]+)/([^/]+)$`,
|
|
|
|
},
|
2020-03-03 15:20:05 +00:00
|
|
|
expectedPath: "/downloads/src-source.go",
|
|
|
|
expectedRawPath: "/downloads/src-source.go",
|
|
|
|
expectedHeader: "/downloads/src/source.go",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid regular expression",
|
|
|
|
path: "/invalid/regexp/test",
|
2019-07-10 07:26:04 +00:00
|
|
|
config: dynamic.ReplacePathRegex{
|
2018-11-14 09:18:03 +00:00
|
|
|
Replacement: "/valid/regexp/$1",
|
|
|
|
Regex: `^(?err)/invalid/regexp/([^/]+)$`,
|
|
|
|
},
|
|
|
|
expectedPath: "/invalid/regexp/test",
|
|
|
|
expectsError: true,
|
|
|
|
},
|
2020-03-03 15:20:05 +00:00
|
|
|
{
|
|
|
|
desc: "replacement with escaped char",
|
|
|
|
path: "/aaa/bbb",
|
|
|
|
config: dynamic.ReplacePathRegex{
|
|
|
|
Replacement: "/foo%2Fbar",
|
|
|
|
Regex: `/aaa/bbb`,
|
|
|
|
},
|
|
|
|
expectedPath: "/foo/bar",
|
|
|
|
expectedRawPath: "/foo%2Fbar",
|
|
|
|
expectedHeader: "/aaa/bbb",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path and regex with escaped char",
|
|
|
|
path: "/aaa%2Fbbb",
|
|
|
|
config: dynamic.ReplacePathRegex{
|
|
|
|
Replacement: "/foo/bar",
|
|
|
|
Regex: `/aaa%2Fbbb`,
|
|
|
|
},
|
|
|
|
expectedPath: "/foo/bar",
|
|
|
|
expectedRawPath: "/foo/bar",
|
|
|
|
expectedHeader: "/aaa%2Fbbb",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path with escaped char (no match)",
|
|
|
|
path: "/aaa%2Fbbb",
|
|
|
|
config: dynamic.ReplacePathRegex{
|
|
|
|
Replacement: "/foo/bar",
|
|
|
|
Regex: `/aaa/bbb`,
|
|
|
|
},
|
|
|
|
expectedPath: "/aaa/bbb",
|
|
|
|
expectedRawPath: "/aaa%2Fbbb",
|
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2020-03-03 15:20:05 +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
|
2020-03-03 15:20:05 +00:00
|
|
|
actualRawPath = r.URL.RawPath
|
2018-11-14 09:18:03 +00:00
|
|
|
actualHeader = r.Header.Get(replacepath.ReplacedPathHeader)
|
|
|
|
requestURI = r.RequestURI
|
|
|
|
})
|
|
|
|
|
|
|
|
handler, err := New(context.Background(), next, test.config, "foo-replace-path-regexp")
|
|
|
|
if test.expectsError {
|
|
|
|
require.Error(t, err)
|
2020-03-03 15:20:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2020-03-03 15:20:05 +00:00
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
defer server.Close()
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2020-03-03 15:20:05 +00:00
|
|
|
resp, err := http.Get(server.URL + test.path)
|
|
|
|
require.NoError(t, err, "Unexpected error while making test request")
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2020-03-03 15:20:05 +00:00
|
|
|
assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.")
|
|
|
|
assert.Equal(t, test.expectedRawPath, actualRawPath, "Unexpected raw path.")
|
|
|
|
|
|
|
|
if actualRawPath == "" {
|
2018-11-14 09:18:03 +00:00
|
|
|
assert.Equal(t, actualPath, requestURI, "Unexpected request URI.")
|
2020-03-03 15:20:05 +00:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, actualRawPath, requestURI, "Unexpected request URI.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.expectedHeader != "" {
|
|
|
|
assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", replacepath.ReplacedPathHeader)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|