Skip to content

Commit 1fcf3db

Browse files
committed
add test, remove json tags
1 parent ebdd243 commit 1fcf3db

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

xds/internal/client/client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,20 +307,20 @@ type WeightedCluster struct {
307307

308308
// HeaderMatcher represents header matchers.
309309
type HeaderMatcher struct {
310-
Name string `json:"name"`
311-
InvertMatch *bool `json:"invertMatch,omitempty"`
312-
ExactMatch *string `json:"exactMatch,omitempty"`
313-
RegexMatch *regexp.Regexp `json:"regexMatch,omitempty"`
314-
PrefixMatch *string `json:"prefixMatch,omitempty"`
315-
SuffixMatch *string `json:"suffixMatch,omitempty"`
316-
RangeMatch *Int64Range `json:"rangeMatch,omitempty"`
317-
PresentMatch *bool `json:"presentMatch,omitempty"`
310+
Name string
311+
InvertMatch *bool
312+
ExactMatch *string
313+
RegexMatch *regexp.Regexp
314+
PrefixMatch *string
315+
SuffixMatch *string
316+
RangeMatch *Int64Range
317+
PresentMatch *bool
318318
}
319319

320320
// Int64Range is a range for header range match.
321321
type Int64Range struct {
322-
Start int64 `json:"start"`
323-
End int64 `json:"end"`
322+
Start int64
323+
End int64
324324
}
325325

326326
// SecurityConfig contains the security configuration received as part of the

xds/internal/client/rds_test.go

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package client
2222

2323
import (
2424
"fmt"
25+
"regexp"
2526
"testing"
2627
"time"
2728

@@ -916,6 +917,51 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
916917
}},
917918
wantErr: false,
918919
},
920+
{
921+
name: "good with regex matchers",
922+
routes: []*v3routepb.Route{
923+
{
924+
Match: &v3routepb.RouteMatch{
925+
PathSpecifier: &v3routepb.RouteMatch_SafeRegex{SafeRegex: &v3matcherpb.RegexMatcher{Regex: "/a/"}},
926+
Headers: []*v3routepb.HeaderMatcher{
927+
{
928+
Name: "th",
929+
HeaderMatchSpecifier: &v3routepb.HeaderMatcher_SafeRegexMatch{SafeRegexMatch: &v3matcherpb.RegexMatcher{Regex: "tv"}},
930+
},
931+
},
932+
RuntimeFraction: &v3corepb.RuntimeFractionalPercent{
933+
DefaultValue: &v3typepb.FractionalPercent{
934+
Numerator: 1,
935+
Denominator: v3typepb.FractionalPercent_HUNDRED,
936+
},
937+
},
938+
},
939+
Action: &v3routepb.Route_Route{
940+
Route: &v3routepb.RouteAction{
941+
ClusterSpecifier: &v3routepb.RouteAction_WeightedClusters{
942+
WeightedClusters: &v3routepb.WeightedCluster{
943+
Clusters: []*v3routepb.WeightedCluster_ClusterWeight{
944+
{Name: "B", Weight: &wrapperspb.UInt32Value{Value: 60}},
945+
{Name: "A", Weight: &wrapperspb.UInt32Value{Value: 40}},
946+
},
947+
TotalWeight: &wrapperspb.UInt32Value{Value: 100},
948+
}}}},
949+
},
950+
},
951+
wantRoutes: []*Route{{
952+
Regex: func() *regexp.Regexp { return regexp.MustCompile("/a/") }(),
953+
Headers: []*HeaderMatcher{
954+
{
955+
Name: "th",
956+
InvertMatch: newBoolP(false),
957+
RegexMatch: func() *regexp.Regexp { return regexp.MustCompile("tv") }(),
958+
},
959+
},
960+
Fraction: newUInt32P(10000),
961+
WeightedClusters: map[string]WeightedCluster{"A": {Weight: 40}, "B": {Weight: 60}},
962+
}},
963+
wantErr: false,
964+
},
919965
{
920966
name: "query is ignored",
921967
routes: []*v3routepb.Route{
@@ -1102,7 +1148,7 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
11021148
}
11031149

11041150
cmpOpts := []cmp.Option{
1105-
cmp.AllowUnexported(Route{}, HeaderMatcher{}, Int64Range{}),
1151+
cmp.AllowUnexported(Route{}, HeaderMatcher{}, Int64Range{}, regexp.Regexp{}),
11061152
cmpopts.EquateEmpty(),
11071153
cmp.Transformer("FilterConfig", func(fc httpfilter.FilterConfig) string {
11081154
return fmt.Sprint(fc)
@@ -1113,17 +1159,15 @@ func (s) TestRoutesProtoToSlice(t *testing.T) {
11131159
t.Run(tt.name, func(t *testing.T) {
11141160
oldFI := env.FaultInjectionSupport
11151161
env.FaultInjectionSupport = !tt.disableFI
1162+
defer func() { env.FaultInjectionSupport = oldFI }()
11161163

11171164
got, err := routesProtoToSlice(tt.routes, nil, false)
11181165
if (err != nil) != tt.wantErr {
1119-
t.Errorf("routesProtoToSlice() error = %v, wantErr %v", err, tt.wantErr)
1120-
return
1166+
t.Fatalf("routesProtoToSlice() error = %v, wantErr %v", err, tt.wantErr)
11211167
}
1122-
if !cmp.Equal(got, tt.wantRoutes, cmpOpts...) {
1123-
t.Errorf("routesProtoToSlice() got = %v, want %v, diff: %v", got, tt.wantRoutes, cmp.Diff(got, tt.wantRoutes, cmpOpts...))
1168+
if diff := cmp.Diff(got, tt.wantRoutes, cmpOpts...); diff != "" {
1169+
t.Fatalf("routesProtoToSlice() returned unexpected diff (-got +want):\n%s", diff)
11241170
}
1125-
1126-
env.FaultInjectionSupport = oldFI
11271171
})
11281172
}
11291173
}

0 commit comments

Comments
 (0)