github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/xdsclient/xdsresource/matcher_path_test.go (about)

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package xdsresource
    19  
    20  import (
    21  	"regexp"
    22  	"testing"
    23  )
    24  
    25  func (s) TestPathFullMatcherMatch(t *testing.T) {
    26  	tests := []struct {
    27  		name            string
    28  		fullPath        string
    29  		caseInsensitive bool
    30  		path            string
    31  		want            bool
    32  	}{
    33  		{name: "match", fullPath: "/s/m", path: "/s/m", want: true},
    34  		{name: "case insensitive match", fullPath: "/s/m", caseInsensitive: true, path: "/S/m", want: true},
    35  		{name: "case insensitive match 2", fullPath: "/s/M", caseInsensitive: true, path: "/S/m", want: true},
    36  		{name: "not match", fullPath: "/s/m", path: "/a/b", want: false},
    37  		{name: "case insensitive not match", fullPath: "/s/m", caseInsensitive: true, path: "/a/b", want: false},
    38  	}
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			fpm := newPathExactMatcher(tt.fullPath, tt.caseInsensitive)
    42  			if got := fpm.match(tt.path); got != tt.want {
    43  				t.Errorf("{%q}.match(%q) = %v, want %v", tt.fullPath, tt.path, got, tt.want)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func (s) TestPathPrefixMatcherMatch(t *testing.T) {
    50  	tests := []struct {
    51  		name            string
    52  		prefix          string
    53  		caseInsensitive bool
    54  		path            string
    55  		want            bool
    56  	}{
    57  		{name: "match", prefix: "/s/", path: "/s/m", want: true},
    58  		{name: "case insensitive match", prefix: "/s/", caseInsensitive: true, path: "/S/m", want: true},
    59  		{name: "case insensitive match 2", prefix: "/S/", caseInsensitive: true, path: "/s/m", want: true},
    60  		{name: "not match", prefix: "/s/", path: "/a/b", want: false},
    61  		{name: "case insensitive not match", prefix: "/s/", caseInsensitive: true, path: "/a/b", want: false},
    62  	}
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			fpm := newPathPrefixMatcher(tt.prefix, tt.caseInsensitive)
    66  			if got := fpm.match(tt.path); got != tt.want {
    67  				t.Errorf("{%q}.match(%q) = %v, want %v", tt.prefix, tt.path, got, tt.want)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func (s) TestPathRegexMatcherMatch(t *testing.T) {
    74  	tests := []struct {
    75  		name      string
    76  		regexPath string
    77  		path      string
    78  		want      bool
    79  	}{
    80  		{name: "match", regexPath: "^/s+/m.*$", path: "/sss/me", want: true},
    81  		{name: "not match", regexPath: "^/s+/m*$", path: "/sss/b", want: false},
    82  		{name: "no match because only part of path matches with regex", regexPath: "^a+$", path: "ab", want: false},
    83  		{name: "match because full path matches with regex", regexPath: "^a+$", path: "aa", want: true},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			fpm := newPathRegexMatcher(regexp.MustCompile(tt.regexPath))
    88  			if got := fpm.match(tt.path); got != tt.want {
    89  				t.Errorf("{%q}.match(%q) = %v, want %v", tt.regexPath, tt.path, got, tt.want)
    90  			}
    91  		})
    92  	}
    93  }