traefik/pkg/middlewares/replacepath/replace_path_test.go

46 lines
1.2 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package replacepath
2017-04-25 18:13:39 +00:00
import (
2018-11-14 09:18:03 +00:00
"context"
2017-04-25 18:13:39 +00:00
"net/http"
"testing"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/testhelpers"
"github.com/stretchr/testify/assert"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/require"
2017-04-25 18:13:39 +00:00
)
func TestReplacePath(t *testing.T) {
var replacementConfig = dynamic.ReplacePath{
2018-11-14 09:18:03 +00:00
Path: "/replacement-path",
}
2017-04-25 18:13:39 +00:00
paths := []string{
"/example",
"/some/really/long/path",
}
for _, path := range paths {
t.Run(path, func(t *testing.T) {
var expectedPath, actualHeader, requestURI string
2018-11-14 09:18:03 +00:00
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedPath = r.URL.Path
actualHeader = r.Header.Get(ReplacedPathHeader)
requestURI = r.RequestURI
})
handler, err := New(context.Background(), next, replacementConfig, "foo-replace-path")
require.NoError(t, err)
2017-04-25 18:13:39 +00:00
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+path, nil)
2017-04-25 18:13:39 +00:00
handler.ServeHTTP(nil, req)
2018-11-14 09:18:03 +00:00
assert.Equal(t, expectedPath, replacementConfig.Path, "Unexpected path.")
assert.Equal(t, path, actualHeader, "Unexpected '%s' header.", ReplacedPathHeader)
assert.Equal(t, expectedPath, requestURI, "Unexpected request URI.")
2017-04-25 18:13:39 +00:00
})
}
}