21
routepb "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
22
matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
23
"github.com/google/go-cmp/cmp"
24
"google.golang.org/protobuf/testing/protocmp"
27
func TestHeaderMatcher(t *testing.T) {
28
testCases := []struct {
32
Expect *routepb.HeaderMatcher
38
Expect: &routepb.HeaderMatcher{
40
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
41
StringMatch: StringMatcherExact("/productpage", false),
49
Expect: &routepb.HeaderMatcher{
51
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
52
StringMatch: StringMatcherSuffix("/productpage*", false),
60
Expect: &routepb.HeaderMatcher{
62
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
63
StringMatch: StringMatcherPrefix("/productpage", false),
71
Expect: &routepb.HeaderMatcher{
73
HeaderMatchSpecifier: &routepb.HeaderMatcher_PresentMatch{
80
for _, tc := range testCases {
81
t.Run(tc.Name, func(t *testing.T) {
82
actual := HeaderMatcher(tc.K, tc.V)
83
if !cmp.Equal(tc.Expect, actual, protocmp.Transform()) {
84
t.Errorf("expecting %v, but got %v", tc.Expect, actual)
90
func TestHostMatcherWithRegex(t *testing.T) {
91
testCases := []struct {
95
Expect *routepb.HeaderMatcher
98
Name: "present match",
101
Expect: &routepb.HeaderMatcher{
103
HeaderMatchSpecifier: &routepb.HeaderMatcher_PresentMatch{PresentMatch: true},
107
Name: "prefix match",
110
Expect: &routepb.HeaderMatcher{
112
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
113
StringMatch: StringMatcherRegex(`(?i).*\.example\.com`),
118
Name: "suffix match",
121
Expect: &routepb.HeaderMatcher{
123
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
124
StringMatch: StringMatcherRegex(`(?i)example\..*`),
132
Expect: &routepb.HeaderMatcher{
134
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
135
StringMatch: StringMatcherRegex(`(?i)example\.com`),
141
for _, tc := range testCases {
142
t.Run(tc.Name, func(t *testing.T) {
143
actual := HostMatcherWithRegex(tc.K, tc.V)
144
if re := actual.GetStringMatch().GetSafeRegex().GetRegex(); re != "" {
145
_, err := regexp.Compile(re)
147
t.Errorf("failed to compile regex %s: %v", re, err)
150
if !cmp.Equal(tc.Expect, actual, protocmp.Transform()) {
151
t.Errorf("expecting %v, but got %v", tc.Expect, actual)
157
func TestHostMatcher(t *testing.T) {
158
testCases := []struct {
162
Expect *routepb.HeaderMatcher
165
Name: "present match",
168
Expect: &routepb.HeaderMatcher{
170
HeaderMatchSpecifier: &routepb.HeaderMatcher_PresentMatch{
176
Name: "suffix match",
179
Expect: &routepb.HeaderMatcher{
181
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
182
StringMatch: StringMatcherSuffix(".example.com", true),
187
Name: "prefix match",
190
Expect: &routepb.HeaderMatcher{
192
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
193
StringMatch: StringMatcherPrefix("example.", true),
201
Expect: &routepb.HeaderMatcher{
203
HeaderMatchSpecifier: &routepb.HeaderMatcher_StringMatch{
204
StringMatch: StringMatcherExact("example.com", true),
210
for _, tc := range testCases {
211
t.Run(tc.Name, func(t *testing.T) {
212
actual := HostMatcher(tc.K, tc.V)
213
if !cmp.Equal(tc.Expect, actual, protocmp.Transform()) {
214
t.Errorf("expecting %v, but got %v", tc.Expect, actual)
220
func TestPathMatcher(t *testing.T) {
221
testCases := []struct {
224
Expect *matcher.PathMatcher
229
Expect: &matcher.PathMatcher{
230
Rule: &matcher.PathMatcher_Path{
231
Path: StringMatcherExact("/productpage", false),
236
Name: "prefix match",
238
Expect: &matcher.PathMatcher{
239
Rule: &matcher.PathMatcher_Path{
240
Path: StringMatcherPrefix("/prefix", false),
245
Name: "suffix match",
247
Expect: &matcher.PathMatcher{
248
Rule: &matcher.PathMatcher_Path{
249
Path: StringMatcherSuffix("suffix", false),
254
Name: "wildcard match",
256
Expect: &matcher.PathMatcher{
257
Rule: &matcher.PathMatcher_Path{
258
Path: StringMatcherRegex(".+"),
264
for _, tc := range testCases {
265
t.Run(tc.Name, func(t *testing.T) {
266
actual := PathMatcher(tc.V)
267
if !cmp.Equal(tc.Expect, actual, protocmp.Transform()) {
268
t.Errorf("expecting %v, but got %v", tc.Expect, actual)