github.com/lingyao2333/mo-zero@v1.4.1/zrpc/resolver/internal/targets/endpoints_test.go (about)

     1  package targets
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"google.golang.org/grpc/resolver"
     9  )
    10  
    11  func TestGetAuthority(t *testing.T) {
    12  	tests := []struct {
    13  		name string
    14  		url  string
    15  		want string
    16  	}{
    17  		{
    18  			name: "test",
    19  			url:  "direct://my_authority/localhost",
    20  			want: "my_authority",
    21  		},
    22  		{
    23  			name: "test with port",
    24  			url:  "direct://my_authority/localhost:8080",
    25  			want: "my_authority",
    26  		},
    27  		{
    28  			name: "test with multiple hosts",
    29  			url:  "direct://my_authority1,my_authority2/localhost,localhost",
    30  			want: "my_authority1,my_authority2",
    31  		},
    32  		{
    33  			name: "test with multiple hosts with port",
    34  			url:  "direct://my_authority1:3000,my_authority2:3001/localhost:8080,localhost:8081",
    35  			want: "my_authority1:3000,my_authority2:3001",
    36  		},
    37  	}
    38  
    39  	for _, test := range tests {
    40  		t.Run(test.name, func(t *testing.T) {
    41  			uri, err := url.Parse(test.url)
    42  			assert.Nil(t, err)
    43  			target := resolver.Target{
    44  				URL: *uri,
    45  			}
    46  			assert.Equal(t, test.want, GetAuthority(target))
    47  		})
    48  	}
    49  }
    50  
    51  func TestGetEndpoints(t *testing.T) {
    52  	tests := []struct {
    53  		name string
    54  		url  string
    55  		want string
    56  	}{
    57  		{
    58  			name: "test",
    59  			url:  "direct:///localhost",
    60  			want: "localhost",
    61  		},
    62  		{
    63  			name: "test with port",
    64  			url:  "direct:///localhost:8080",
    65  			want: "localhost:8080",
    66  		},
    67  		{
    68  			name: "test with multiple hosts",
    69  			url:  "direct:///localhost,localhost",
    70  			want: "localhost,localhost",
    71  		},
    72  		{
    73  			name: "test with multiple hosts with port",
    74  			url:  "direct:///localhost:8080,localhost:8081",
    75  			want: "localhost:8080,localhost:8081",
    76  		},
    77  	}
    78  
    79  	for _, test := range tests {
    80  		t.Run(test.name, func(t *testing.T) {
    81  			uri, err := url.Parse(test.url)
    82  			assert.Nil(t, err)
    83  			target := resolver.Target{
    84  				URL: *uri,
    85  			}
    86  			assert.Equal(t, test.want, GetEndpoints(target))
    87  		})
    88  	}
    89  }