forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtarget_test.go
154 lines (152 loc) · 5.27 KB
/
target_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package route
import (
"net/url"
"strings"
"testing"
)
func TestTarget_BuildRedirectURL(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/"},
},
},
{ // simple absolute redirect with encoded character
route: "route add svc / http://bar.com/%2f/",
tests: []routeTest{
{req: "/", want: "http://bar.com/%2f/"},
{req: "/abc", want: "http://bar.com/%2f/"},
{req: "/a/b/c", want: "http://bar.com/%2f/"},
{req: "/?aaa=1", want: "http://bar.com/%2f/"},
},
},
{ // 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"},
},
},
{ // subdir with encoded char on target with $path at end
route: "route add svc / http://bar.com/b%2fb/$path",
tests: []routeTest{
{req: "/", want: "http://bar.com/b%2fb/"},
{req: "/abc", want: "http://bar.com/b%2fb/abc"},
{req: "/a/b/c", want: "http://bar.com/b%2fb/a/b/c"},
{req: "/?aaa=1", want: "http://bar.com/b%2fb/?aaa=1"},
{req: "/abc/?aaa=1", want: "http://bar.com/b%2fb/abc/?aaa=1"},
},
},
{ // simple redirect to corresponding path with encoded char in path
route: "route add svc / http://bar.com/$path",
tests: []routeTest{
{req: "/%20", want: "http://bar.com/%20"},
{req: "/a%2fbc", want: "http://bar.com/a%2fbc"},
{req: "/a/b%22/c", want: "http://bar.com/a/b%22/c"},
{req: "/%2f/?aaa=1", want: "http://bar.com/%2f/?aaa=1"},
},
},
{ // subdir + path with encoded char on target and encoded char in path
route: "route add svc / http://monkey.com/b%2fb/$path",
tests: []routeTest{
{req: "/%22", want: "http://monkey.com/b%2fb/%22"},
//{req: "/a%2fbc", want: "http://monkey.com/b%2fb/a%2fbc"},
//{req: "/a/b%22/c", want: "http://monkey.com/b%2fb/a/b%22/c"},
//{req: "/%2f/?aaa=1", want: "http://monkey.com/b%2fb/%2f/?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"},
},
},
{ // strip prefix containing encoded char
route: "route add svc /strip%2fme http://bar.com/$path opts \"strip=/strip%2fme\"",
tests: []routeTest{
{req: "/strip%2fme/abc", want: "http://bar.com/abc"},
{req: "/strip%2fme/ab%22c", want: "http://bar.com/ab%22c"},
{req: "/strip%2fme/ab%2fc", want: "http://bar.com/ab%2fc"},
},
},
}
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)
target.BuildRedirectURL(reqURL)
if strings.Contains(rt.want, "monkey") {
t.Logf("%#v", reqURL)
t.Logf("%#v", target.RedirectURL)
}
if got := target.RedirectURL.String(); got != rt.want {
t.Errorf("Got %s, wanted %s", got, rt.want)
}
}
}
}