1
// Copyright Istio Authors
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
20
matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
21
"github.com/google/go-cmp/cmp"
22
"google.golang.org/protobuf/testing/protocmp"
25
func TestMetadataStringMatcher(t *testing.T) {
26
m := &matcher.StringMatcher{
27
MatchPattern: &matcher.StringMatcher_Exact{
31
actual := MetadataStringMatcher("istio_authn", "key", m)
32
expect := &matcher.MetadataMatcher{
33
Filter: "istio_authn",
34
Path: []*matcher.MetadataMatcher_PathSegment{
36
Segment: &matcher.MetadataMatcher_PathSegment_Key{
41
Value: &matcher.ValueMatcher{
42
MatchPattern: &matcher.ValueMatcher_StringMatch{
48
if !cmp.Equal(actual, expect, protocmp.Transform()) {
49
t.Errorf("want %s, got %s", expect.String(), actual.String())
53
func TestMetadataListMatcher(t *testing.T) {
54
getWant := func(regex string) *matcher.MetadataMatcher {
55
return &matcher.MetadataMatcher{
56
Filter: "istio_authn",
57
Path: []*matcher.MetadataMatcher_PathSegment{
59
Segment: &matcher.MetadataMatcher_PathSegment_Key{
64
Segment: &matcher.MetadataMatcher_PathSegment_Key{
69
Value: &matcher.ValueMatcher{
70
MatchPattern: &matcher.ValueMatcher_ListMatch{
71
ListMatch: &matcher.ListMatcher{
72
MatchPattern: &matcher.ListMatcher_OneOf{
73
OneOf: &matcher.ValueMatcher{
74
MatchPattern: &matcher.ValueMatcher_StringMatch{
75
StringMatch: &matcher.StringMatcher{
76
MatchPattern: &matcher.StringMatcher_SafeRegex{
77
SafeRegex: &matcher.RegexMatcher{
91
testCases := []struct {
100
for _, tc := range testCases {
101
t.Run(tc.name, func(t *testing.T) {
102
want := getWant(tc.want)
103
actual := MetadataListMatcher("istio_authn", []string{"key1", "key2"}, StringMatcher("*"), false)
104
if !cmp.Equal(want, actual, protocmp.Transform()) {
105
t.Errorf("want %s, but got %s", want.String(), actual.String())