Added support for replacement containing escaped characters

Co-authored-by: Ludovic Fernandez <ldez@users.noreply.github.com>
This commit is contained in:
robotte 2020-03-03 16:20:05 +01:00 committed by GitHub
parent a7495f711b
commit 353bd3d06f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 180 additions and 46 deletions

View file

@ -3,23 +3,24 @@ package replacepathregex
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/middlewares/replacepath"
"github.com/containous/traefik/v2/pkg/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReplacePathRegex(t *testing.T) {
testCases := []struct {
desc string
path string
config dynamic.ReplacePathRegex
expectedPath string
expectedHeader string
expectsError bool
desc string
path string
config dynamic.ReplacePathRegex
expectedPath string
expectedRawPath string
expectedHeader string
expectsError bool
}{
{
desc: "simple regex",
@ -28,8 +29,9 @@ func TestReplacePathRegex(t *testing.T) {
Replacement: "/who-am-i/$1",
Regex: `^/whoami/(.*)`,
},
expectedPath: "/who-am-i/and/whoami",
expectedHeader: "/whoami/and/whoami",
expectedPath: "/who-am-i/and/whoami",
expectedRawPath: "/who-am-i/and/whoami",
expectedHeader: "/whoami/and/whoami",
},
{
desc: "simple replace (no regex)",
@ -38,8 +40,9 @@ func TestReplacePathRegex(t *testing.T) {
Replacement: "/who-am-i",
Regex: `/whoami`,
},
expectedPath: "/who-am-i/and/who-am-i",
expectedHeader: "/whoami/and/whoami",
expectedPath: "/who-am-i/and/who-am-i",
expectedRawPath: "/who-am-i/and/who-am-i",
expectedHeader: "/whoami/and/whoami",
},
{
desc: "no match",
@ -57,8 +60,9 @@ func TestReplacePathRegex(t *testing.T) {
Replacement: "/downloads/$1-$2",
Regex: `^(?i)/downloads/([^/]+)/([^/]+)$`,
},
expectedPath: "/downloads/src-source.go",
expectedHeader: "/downloads/src/source.go",
expectedPath: "/downloads/src-source.go",
expectedRawPath: "/downloads/src-source.go",
expectedHeader: "/downloads/src/source.go",
},
{
desc: "invalid regular expression",
@ -70,13 +74,46 @@ func TestReplacePathRegex(t *testing.T) {
expectedPath: "/invalid/regexp/test",
expectsError: true,
},
{
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",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
var actualPath, actualHeader, requestURI string
var actualPath, actualRawPath, actualHeader, requestURI string
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
actualPath = r.URL.Path
actualRawPath = r.URL.RawPath
actualHeader = r.Header.Get(replacepath.ReplacedPathHeader)
requestURI = r.RequestURI
})
@ -84,19 +121,29 @@ func TestReplacePathRegex(t *testing.T) {
handler, err := New(context.Background(), next, test.config, "foo-replace-path-regexp")
if test.expectsError {
require.Error(t, err)
} else {
require.NoError(t, err)
return
}
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
req.RequestURI = test.path
require.NoError(t, err)
handler.ServeHTTP(nil, req)
server := httptest.NewServer(handler)
defer server.Close()
assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.")
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)
assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.")
assert.Equal(t, test.expectedRawPath, actualRawPath, "Unexpected raw path.")
if actualRawPath == "" {
assert.Equal(t, actualPath, requestURI, "Unexpected request URI.")
if test.expectedHeader != "" {
assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", replacepath.ReplacedPathHeader)
}
} else {
assert.Equal(t, actualRawPath, requestURI, "Unexpected request URI.")
}
if test.expectedHeader != "" {
assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", replacepath.ReplacedPathHeader)
}
})
}