-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This patch adds support to redirect a request for a matching route to another URL. If the `redirect=<code>` option is set on a route fabio will send a redirect response to the dst address with the given code. The syntax for the `urlprefix-` tag is slightly different since the destination address is usually generated from the service registration stored in Consul. The `$path` pseudo-variable can be used to include the original request URI in the destination target. # redirect /foo to https://www.foo.com/ route add svc /foo https://www.foo.com/ opts "redirect=301" # redirect /foo to https://www.foo.com/ urlprefix-/foo redirect=301,https://www.foo.com/ # redirect /foo to https://www.foo.com/foo urlprefix-/foo redirect=301,https://www.foo.com$path Fixes #87 Closes #395
- Loading branch information
1 parent
119bd53
commit 44b8986
Showing
9 changed files
with
235 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package route | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestTarget_GetRedirectURL(t *testing.T) { | ||
type routeTest struct { | ||
req string | ||
want string | ||
} | ||
tests := []struct { | ||
route string | ||
tests []routeTest | ||
}{ | ||
{ // simple absolute redirect | ||
route: "route add svc / http://bar.com/", | ||
tests: []routeTest{ | ||
{req: "/", want: "http://bar.com/"}, | ||
{req: "/abc", want: "http://bar.com/"}, | ||
{req: "/a/b/c", want: "http://bar.com/"}, | ||
{req: "/?aaa=1", want: "http://bar.com/"}, | ||
}, | ||
}, | ||
{ // 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"}, | ||
{req: "/abc", want: "http://bar.com/a/b/c?foo=bar"}, | ||
{req: "/a/b/c", want: "http://bar.com/a/b/c?foo=bar"}, | ||
{req: "/?aaa=1", want: "http://bar.com/a/b/c?foo=bar"}, | ||
}, | ||
}, | ||
{ // simple redirect to corresponding path | ||
route: "route add svc / http://bar.com/$path", | ||
tests: []routeTest{ | ||
{req: "/", want: "http://bar.com/"}, | ||
{req: "/abc", want: "http://bar.com/abc"}, | ||
{req: "/a/b/c", want: "http://bar.com/a/b/c"}, | ||
{req: "/?aaa=1", want: "http://bar.com/?aaa=1"}, | ||
{req: "/abc/?aaa=1", want: "http://bar.com/abc/?aaa=1"}, | ||
}, | ||
}, | ||
{ // same as above but without / before $path | ||
route: "route add svc / http://bar.com$path", | ||
tests: []routeTest{ | ||
{req: "/", want: "http://bar.com/"}, | ||
{req: "/abc", want: "http://bar.com/abc"}, | ||
{req: "/a/b/c", want: "http://bar.com/a/b/c"}, | ||
{req: "/?aaa=1", want: "http://bar.com/?aaa=1"}, | ||
{req: "/abc/?aaa=1", want: "http://bar.com/abc/?aaa=1"}, | ||
}, | ||
}, | ||
{ // 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/"}, | ||
{req: "/abc", want: "http://bar.com/bbb/abc"}, | ||
{req: "/a/b/c", want: "http://bar.com/bbb/a/b/c"}, | ||
{req: "/?aaa=1", want: "http://bar.com/bbb/?aaa=1"}, | ||
{req: "/abc/?aaa=1", want: "http://bar.com/bbb/abc/?aaa=1"}, | ||
}, | ||
}, | ||
{ // same as above but without / before $path | ||
route: "route add svc / http://bar.com/bbb$path", | ||
tests: []routeTest{ | ||
{req: "/", want: "http://bar.com/bbb/"}, | ||
{req: "/abc", want: "http://bar.com/bbb/abc"}, | ||
{req: "/a/b/c", want: "http://bar.com/bbb/a/b/c"}, | ||
{req: "/?aaa=1", want: "http://bar.com/bbb/?aaa=1"}, | ||
{req: "/abc/?aaa=1", want: "http://bar.com/bbb/abc/?aaa=1"}, | ||
}, | ||
}, | ||
{ // strip prefix | ||
route: "route add svc /stripme http://bar.com/$path opts \"strip=/stripme\"", | ||
tests: []routeTest{ | ||
{req: "/stripme/", want: "http://bar.com/"}, | ||
{req: "/stripme/abc", want: "http://bar.com/abc"}, | ||
{req: "/stripme/a/b/c", want: "http://bar.com/a/b/c"}, | ||
{req: "/stripme/?aaa=1", want: "http://bar.com/?aaa=1"}, | ||
{req: "/stripme/abc/?aaa=1", want: "http://bar.com/abc/?aaa=1"}, | ||
}, | ||
}, | ||
} | ||
firstRoute := func(tbl Table) *Route { | ||
for _, routes := range tbl { | ||
return routes[0] | ||
} | ||
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) | ||
} | ||
} | ||
} | ||
} |