dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/client/bootstrap/template_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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  /*
    19   *
    20   * Copyright 2019 gRPC authors.
    21   *
    22   */
    23  
    24  package bootstrap
    25  
    26  import (
    27  	"testing"
    28  )
    29  
    30  func Test_percentEncode(t *testing.T) {
    31  	tests := []struct {
    32  		name   string
    33  		target string
    34  		want   string
    35  	}{
    36  		{
    37  			name:   "normal name",
    38  			target: "server.example.com",
    39  			want:   "server.example.com",
    40  		},
    41  		{
    42  			name:   "ipv4",
    43  			target: "0.0.0.0:8080",
    44  			want:   "0.0.0.0:8080",
    45  		},
    46  		{
    47  			name:   "ipv6",
    48  			target: "[::1]:8080",
    49  			want:   "%5B::1%5D:8080", // [ and ] are percent encoded.
    50  		},
    51  		{
    52  			name:   "/ should not be percent encoded",
    53  			target: "my/service/region",
    54  			want:   "my/service/region", // "/"s are kept.
    55  		},
    56  	}
    57  	for _, tt := range tests {
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			if got := percentEncode(tt.target); got != tt.want {
    60  				t.Errorf("percentEncode() = %v, want %v", got, tt.want)
    61  			}
    62  		})
    63  	}
    64  }
    65  
    66  func TestPopulateResourceTemplate(t *testing.T) {
    67  	tests := []struct {
    68  		name     string
    69  		template string
    70  		target   string
    71  		want     string
    72  	}{
    73  		{
    74  			name:     "no %s",
    75  			template: "/name/template",
    76  			target:   "[::1]:8080",
    77  			want:     "/name/template",
    78  		},
    79  		{
    80  			name:     "with %s, no xdstp: prefix, ipv6",
    81  			template: "/name/template/%s",
    82  			target:   "[::1]:8080",
    83  			want:     "/name/template/[::1]:8080",
    84  		},
    85  		{
    86  			name:     "with %s, with xdstp: prefix",
    87  			template: "xdstp://authority.com/%s",
    88  			target:   "0.0.0.0:8080",
    89  			want:     "xdstp://authority.com/0.0.0.0:8080",
    90  		},
    91  		{
    92  			name:     "with %s, with xdstp: prefix, and ipv6",
    93  			template: "xdstp://authority.com/%s",
    94  			target:   "[::1]:8080",
    95  			want:     "xdstp://authority.com/%5B::1%5D:8080",
    96  		},
    97  	}
    98  	for _, tt := range tests {
    99  		t.Run(tt.name, func(t *testing.T) {
   100  			if got := PopulateResourceTemplate(tt.template, tt.target); got != tt.want {
   101  				t.Errorf("PopulateResourceTemplate() = %v, want %v", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }