-
Notifications
You must be signed in to change notification settings - Fork 3
/
matcher_test.go
176 lines (154 loc) · 3.82 KB
/
matcher_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package naddy
import (
"fmt"
"github.com/tj/go-redirects"
"net/url"
"testing"
)
func MustParseUrl(urlStr string) *url.URL {
u, err := url.Parse(urlStr)
if err != nil {
panic("cannot parse url " + urlStr)
}
return u
}
func TestRedirects(t *testing.T) {
rules := redirects.Must(redirects.ParseString(`
/relative/path/:paramOne /relative/path/redirected/:paramOne
/relative/* /path/changed/:splat/hello
/test/* /redirect/:splat
/accommodation/meribel/chalet-le-lys-blanc/5-bedroom-chalet-for-12/ / 410
/accommodation/morzine/chalet-chez-claude/5-bedroom-chalet-for-13/ / 410
/accommodation/meribel/chalet-camarine/5-bedroom-chalet-for-13/ / 410
/accommodation/val-disere/le-chalet-arosa/5-bedroom-for-10/ / 410
/accommodation/* /accommodation/ 200
http://one.test:2021/* http://two.test:2021/:splat
http://lch.k8.rentivo.com/* http://lakecomohomes.com/:splat
http://lakecomohomes.com/* http://www.lakecomohomes.com/:splat
one.test:/2021/no-scheme two.test:2021/:splat
`))
testCases := []struct {
RequestUrlIn *url.URL
IsMatched bool
IsHostRedirect bool
ExpectedTo string
}{
// Relative
{
MustParseUrl("http://www.one.test"),
false,
false,
"",
},
{
MustParseUrl("http://www.one.test/relative/path/foo"),
true,
false,
"/relative/path/redirected/foo",
},
{
MustParseUrl("http://www.one.test/relative/path-not-matched/bar"),
true,
false,
"/path/changed/path-not-matched/bar/hello",
},
{
MustParseUrl("http://one.test:2021/test/foobar"),
true,
false,
"/redirect/foobar",
},
// path or no path
{
MustParseUrl("http://one.test:2021/accommodation/morzine/chalet-chez-claude/5-bedroom-chalet-for-13"),
true,
false,
"/",
},
{
MustParseUrl("http://one.test:2021/accommodation/morzine/chalet-chez-claude/5-bedroom-chalet-for-13/"),
true,
false,
"/",
},
// Host
{
MustParseUrl("http://one.test:2021/redirect/foobar"),
true,
true,
"http://two.test:2021/redirect/foobar",
},
{
MustParseUrl("http://one.test:2021/test/foobar"),
true,
false,
"/redirect/foobar",
},
{
MustParseUrl("http://one.test:2021/redirect/foobar"),
true,
true,
"http://two.test:2021/redirect/foobar",
},
{
MustParseUrl("http://lch.k8.rentivo.com/example"),
true,
true,
"http://lakecomohomes.com/example",
},
{
MustParseUrl("http://lch.k8.rentivo.com/"),
true,
true,
"http://lakecomohomes.com/",
},
{
MustParseUrl("http://lch.k8.rentivo.com"),
true,
true,
"http://lakecomohomes.com/",
},
{
MustParseUrl("http://lakecomohomes.com"),
true,
true,
"http://www.lakecomohomes.com/",
},
}
ctx := &MatchContext{
Scheme: "http",
}
for _, tc := range testCases {
name := fmt.Sprintf("%s/match=%v/host=%v", tc.RequestUrlIn.String(), tc.IsMatched, tc.IsHostRedirect)
t.Run(name, func(t *testing.T) {
matchFound := false
for _, rule := range rules {
result := MatchUrlToRule(rule, tc.RequestUrlIn, ctx)
if result.Error != nil {
t.Errorf("an error occurred matching %s", result.Error.Error())
continue
}
if result.IsMatched == false {
continue // skip till we find the rule
}
matchFound = true
if result.IsHostRedirect != tc.IsHostRedirect {
t.Errorf("want host=%v, got host=%v", tc.IsHostRedirect, result.IsHostRedirect)
}
if tc.IsMatched {
if result.ResolvedTo == nil {
t.Errorf("want resolvedTo=%s, got resolvedTo=<nil>", tc.ExpectedTo)
break
} else if result.ResolvedTo.String() != tc.ExpectedTo {
t.Errorf("want resolvedTo=%s, got resolvedTo=%s", tc.ExpectedTo, result.ResolvedTo.String())
break
}
}
break
}
if matchFound == false && tc.IsMatched != false {
t.Errorf("want match=%v, got match=%v", tc.IsMatched, false)
}
})
}
}