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

     1  /*
     2   *
     3   * Copyright 2021 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 bootstrap
    19  
    20  import "testing"
    21  
    22  func Test_percentEncode(t *testing.T) {
    23  	tests := []struct {
    24  		name   string
    25  		target string
    26  		want   string
    27  	}{
    28  		{
    29  			name:   "normal name",
    30  			target: "server.example.com",
    31  			want:   "server.example.com",
    32  		},
    33  		{
    34  			name:   "ipv4",
    35  			target: "0.0.0.0:8080",
    36  			want:   "0.0.0.0:8080",
    37  		},
    38  		{
    39  			name:   "ipv6",
    40  			target: "[::1]:8080",
    41  			want:   "%5B::1%5D:8080", // [ and ] are percent encoded.
    42  		},
    43  		{
    44  			name:   "/ should not be percent encoded",
    45  			target: "my/service/region",
    46  			want:   "my/service/region", // "/"s are kept.
    47  		},
    48  	}
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			if got := percentEncode(tt.target); got != tt.want {
    52  				t.Errorf("percentEncode() = %v, want %v", got, tt.want)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestPopulateResourceTemplate(t *testing.T) {
    59  	tests := []struct {
    60  		name     string
    61  		template string
    62  		target   string
    63  		want     string
    64  	}{
    65  		{
    66  			name:     "no %s",
    67  			template: "/name/template",
    68  			target:   "[::1]:8080",
    69  			want:     "/name/template",
    70  		},
    71  		{
    72  			name:     "with %s, no xdstp: prefix, ipv6",
    73  			template: "/name/template/%s",
    74  			target:   "[::1]:8080",
    75  			want:     "/name/template/[::1]:8080",
    76  		},
    77  		{
    78  			name:     "with %s, with xdstp: prefix",
    79  			template: "xdstp://authority.com/%s",
    80  			target:   "0.0.0.0:8080",
    81  			want:     "xdstp://authority.com/0.0.0.0:8080",
    82  		},
    83  		{
    84  			name:     "with %s, with xdstp: prefix, and ipv6",
    85  			template: "xdstp://authority.com/%s",
    86  			target:   "[::1]:8080",
    87  			want:     "xdstp://authority.com/%5B::1%5D:8080",
    88  		},
    89  	}
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			if got := PopulateResourceTemplate(tt.template, tt.target); got != tt.want {
    93  				t.Errorf("PopulateResourceTemplate() = %v, want %v", got, tt.want)
    94  			}
    95  		})
    96  	}
    97  }