Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions route/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ func (t *Target) GetRedirectURL(requestURL *url.URL) *url.URL {
redirectURL.RawQuery = requestURL.RawQuery
}
}
if strings.Contains(redirectURL.Host, "$host") {
redirectURL.Host = strings.Replace(redirectURL.Host, "$host", requestURL.Host, 1)
}
return redirectURL
}
97 changes: 81 additions & 16 deletions route/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ func TestTarget_GetRedirectURL(t *testing.T) {
want string
}
tests := []struct {
desc string
route string
tests []routeTest
}{
{ // simple absolute redirect
{
desc: "simple absolute redirect",
route: "route add svc / http://bar.com/",
tests: []routeTest{
{req: "/", want: "http://bar.com/"},
Expand All @@ -23,7 +25,18 @@ func TestTarget_GetRedirectURL(t *testing.T) {
{req: "/?aaa=1", want: "http://bar.com/"},
},
},
{ // absolute redirect to deep path with query
{
desc: "simple catch-all redirect",
route: "route add svc / http://$host/",
tests: []routeTest{
{req: "/", want: "http://foo.com/"},
{req: "/abc", want: "http://foo.com/"},
{req: "/a/b/c", want: "http://foo.com/"},
{req: "/?aaa=1", want: "http://foo.com/"},
},
},
{
desc: "absolute redirect to deep path with query",
route: "route add svc / http://bar.com/a/b/c?foo=bar",
tests: []routeTest{
{req: "/", want: "http://bar.com/a/b/c?foo=bar"},
Expand All @@ -32,7 +45,18 @@ func TestTarget_GetRedirectURL(t *testing.T) {
{req: "/?aaa=1", want: "http://bar.com/a/b/c?foo=bar"},
},
},
{ // simple redirect to corresponding path
{
desc: "catch-all redirect to deep path with query",
route: "route add svc / http://$host/a/b/c?foo=bar",
tests: []routeTest{
{req: "/", want: "http://foo.com/a/b/c?foo=bar"},
{req: "/abc", want: "http://foo.com/a/b/c?foo=bar"},
{req: "/a/b/c", want: "http://foo.com/a/b/c?foo=bar"},
{req: "/?aaa=1", want: "http://foo.com/a/b/c?foo=bar"},
},
},
{
desc: "simple redirect to corresponding path",
route: "route add svc / http://bar.com/$path",
tests: []routeTest{
{req: "/", want: "http://bar.com/"},
Expand All @@ -42,7 +66,30 @@ func TestTarget_GetRedirectURL(t *testing.T) {
{req: "/abc/?aaa=1", want: "http://bar.com/abc/?aaa=1"},
},
},
{ // same as above but without / before $path
{
desc: "catch-all redirect to corresponding path",
route: "route add svc / http://$host/$path",
tests: []routeTest{
{req: "/", want: "http://foo.com/"},
{req: "/abc", want: "http://foo.com/abc"},
{req: "/a/b/c", want: "http://foo.com/a/b/c"},
{req: "/?aaa=1", want: "http://foo.com/?aaa=1"},
{req: "/abc/?aaa=1", want: "http://foo.com/abc/?aaa=1"},
},
},
{
desc: "catch-all http to https",
route: "route add svc / https://$host/$path",
tests: []routeTest{
{req: "/", want: "https://foo.com/"},
{req: "/abc", want: "https://foo.com/abc"},
{req: "/a/b/c", want: "https://foo.com/a/b/c"},
{req: "/?aaa=1", want: "https://foo.com/?aaa=1"},
{req: "/abc/?aaa=1", want: "https://foo.com/abc/?aaa=1"},
},
},
{
desc: "same as above but without / before $path",
route: "route add svc / http://bar.com$path",
tests: []routeTest{
{req: "/", want: "http://bar.com/"},
Expand All @@ -52,7 +99,8 @@ func TestTarget_GetRedirectURL(t *testing.T) {
{req: "/abc/?aaa=1", want: "http://bar.com/abc/?aaa=1"},
},
},
{ // arbitrary subdir on target with $path at end
{
desc: "arbitrary subdir on target with $path at end",
route: "route add svc / http://bar.com/bbb/$path",
tests: []routeTest{
{req: "/", want: "http://bar.com/bbb/"},
Expand All @@ -62,7 +110,8 @@ func TestTarget_GetRedirectURL(t *testing.T) {
{req: "/abc/?aaa=1", want: "http://bar.com/bbb/abc/?aaa=1"},
},
},
{ // same as above but without / before $path
{
desc: "same as above but without / before $path",
route: "route add svc / http://bar.com/bbb$path",
tests: []routeTest{
{req: "/", want: "http://bar.com/bbb/"},
Expand All @@ -72,7 +121,8 @@ func TestTarget_GetRedirectURL(t *testing.T) {
{req: "/abc/?aaa=1", want: "http://bar.com/bbb/abc/?aaa=1"},
},
},
{ // strip prefix
{
desc: "strip prefix",
route: "route add svc /stripme http://bar.com/$path opts \"strip=/stripme\"",
tests: []routeTest{
{req: "/stripme/", want: "http://bar.com/"},
Expand All @@ -82,6 +132,17 @@ func TestTarget_GetRedirectURL(t *testing.T) {
{req: "/stripme/abc/?aaa=1", want: "http://bar.com/abc/?aaa=1"},
},
},
{
desc: "catch-all with strip prefix",
route: "route add svc /stripme http://$host/$path opts \"strip=/stripme\"",
tests: []routeTest{
{req: "/stripme/", want: "http://foo.com/"},
{req: "/stripme/abc", want: "http://foo.com/abc"},
{req: "/stripme/a/b/c", want: "http://foo.com/a/b/c"},
{req: "/stripme/?aaa=1", want: "http://foo.com/?aaa=1"},
{req: "/stripme/abc/?aaa=1", want: "http://foo.com/abc/?aaa=1"},
},
},
}
firstRoute := func(tbl Table) *Route {
for _, routes := range tbl {
Expand All @@ -90,15 +151,19 @@ func TestTarget_GetRedirectURL(t *testing.T) {
return nil
}
for _, tt := range tests {
tbl, _ := NewTable(tt.route)
route := firstRoute(tbl)
target := route.Targets[0]
for _, rt := range tt.tests {
reqURL, _ := url.Parse("http://foo.com" + rt.req)
got := target.GetRedirectURL(reqURL)
if got.String() != rt.want {
t.Errorf("Got %s, wanted %s", got, rt.want)
t.Run(tt.desc, func(t *testing.T) {
tbl, _ := NewTable(tt.route)
route := firstRoute(tbl)
target := route.Targets[0]
for _, rt := range tt.tests {
t.Run("", func(t *testing.T) {
reqURL, _ := url.Parse("http://foo.com" + rt.req)
got := target.GetRedirectURL(reqURL)
if got.String() != rt.want {
t.Errorf("Got %s, wanted %s", got, rt.want)
}
})
}
}
})
}
}