Add HTTPUrlRewrite Filter in Gateway API
This commit is contained in:
parent
3ca667a3d4
commit
a696f7c654
15 changed files with 754 additions and 110 deletions
126
pkg/middlewares/gatewayapi/urlrewrite/url_rewrite_test.go
Normal file
126
pkg/middlewares/gatewayapi/urlrewrite/url_rewrite_test.go
Normal file
|
@ -0,0 +1,126 @@
|
|||
package urlrewrite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
||||
"k8s.io/utils/ptr"
|
||||
)
|
||||
|
||||
func TestURLRewriteHandler(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
config dynamic.URLRewrite
|
||||
url string
|
||||
wantURL string
|
||||
wantHost string
|
||||
}{
|
||||
{
|
||||
desc: "replace path",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace path without trailing slash",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar/",
|
||||
wantURL: "http://foo.com/baz",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace path with trailing slash",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz/"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "only host",
|
||||
config: dynamic.URLRewrite{
|
||||
Hostname: ptr.To("bar.com"),
|
||||
},
|
||||
url: "http://foo.com/foo/",
|
||||
wantURL: "http://foo.com/foo/",
|
||||
wantHost: "bar.com",
|
||||
},
|
||||
{
|
||||
desc: "host and path",
|
||||
config: dynamic.URLRewrite{
|
||||
Hostname: ptr.To("bar.com"),
|
||||
Path: ptr.To("/baz/"),
|
||||
},
|
||||
url: "http://foo.com/foo/",
|
||||
wantURL: "http://foo.com/baz/",
|
||||
wantHost: "bar.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
PathPrefix: ptr.To("/foo"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/bar",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path with trailing slash",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
PathPrefix: ptr.To("/foo"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar/",
|
||||
wantURL: "http://foo.com/baz/bar/",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path without slash prefix",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("baz"),
|
||||
PathPrefix: ptr.To("/foo"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/bar",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
{
|
||||
desc: "replace prefix path without slash prefix",
|
||||
config: dynamic.URLRewrite{
|
||||
Path: ptr.To("/baz"),
|
||||
PathPrefix: ptr.To("/foo/"),
|
||||
},
|
||||
url: "http://foo.com/foo/bar",
|
||||
wantURL: "http://foo.com/baz/bar",
|
||||
wantHost: "foo.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
||||
|
||||
handler := NewURLRewrite(context.Background(), next, test.config, "traefikTest")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, test.url, nil)
|
||||
handler.ServeHTTP(recorder, req)
|
||||
|
||||
assert.Equal(t, test.wantURL, req.URL.String())
|
||||
assert.Equal(t, test.wantHost, req.Host)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue