github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/rpc/client/options_test.go (about)

     1  package client
     2  
     3  import (
     4  	"crypto/tls"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestWithNetworkURIAddress(t *testing.T) {
    11  	hostPort := "frostfs.example.com:8080"
    12  	apiPort := "127.0.0.1:8080"
    13  	serverName := "testServer"
    14  
    15  	testCases := []struct {
    16  		uri       string
    17  		tlsConfig *tls.Config
    18  
    19  		wantHost string
    20  		wantTLS  bool
    21  	}{
    22  		{
    23  			uri:       grpcScheme + "://" + hostPort,
    24  			tlsConfig: nil,
    25  			wantHost:  "frostfs.example.com:8080",
    26  			wantTLS:   false,
    27  		},
    28  		{
    29  			uri:       grpcScheme + "://" + hostPort,
    30  			tlsConfig: &tls.Config{},
    31  			wantHost:  "frostfs.example.com:8080",
    32  			wantTLS:   false,
    33  		},
    34  		{
    35  			uri:       grpcTLSScheme + "://" + hostPort,
    36  			tlsConfig: nil,
    37  			wantHost:  "frostfs.example.com:8080",
    38  			wantTLS:   true,
    39  		},
    40  		{
    41  			uri:       grpcTLSScheme + "://" + hostPort,
    42  			tlsConfig: &tls.Config{ServerName: serverName},
    43  			wantHost:  "frostfs.example.com:8080",
    44  			wantTLS:   true,
    45  		},
    46  		{
    47  			uri:       "wrongScheme://" + hostPort,
    48  			tlsConfig: nil,
    49  			wantHost:  "",
    50  			wantTLS:   false,
    51  		},
    52  		{
    53  			uri:       "impossibleToParseIt",
    54  			tlsConfig: nil,
    55  			wantHost:  "impossibleToParseIt",
    56  			wantTLS:   false,
    57  		},
    58  		{
    59  			uri:       hostPort,
    60  			tlsConfig: nil,
    61  			wantHost:  hostPort,
    62  			wantTLS:   false,
    63  		},
    64  		{
    65  			uri:       apiPort,
    66  			tlsConfig: nil,
    67  			wantHost:  apiPort,
    68  			wantTLS:   false,
    69  		},
    70  	}
    71  
    72  	for _, test := range testCases {
    73  		cfg := &cfg{}
    74  		opts := WithNetworkURIAddress(test.uri, test.tlsConfig)
    75  
    76  		for _, opt := range opts {
    77  			opt(cfg)
    78  		}
    79  
    80  		require.Equal(t, test.wantHost, cfg.addr, test.uri)
    81  		require.Equal(t, test.wantTLS, cfg.tlsCfg != nil, test.uri)
    82  		// check if custom tlsConfig was applied
    83  		if test.tlsConfig != nil && test.wantTLS {
    84  			require.Equal(t, test.tlsConfig.ServerName, cfg.tlsCfg.ServerName, test.uri)
    85  		}
    86  	}
    87  }
    88  
    89  func Test_WithNetworkAddress_WithTLS_WithNetworkURIAddress(t *testing.T) {
    90  	addr1, addr2 := "example1.com:8080", "example2.com:8080"
    91  
    92  	testCases := []struct {
    93  		addr    string
    94  		withTLS bool
    95  
    96  		uri string
    97  
    98  		wantHost string
    99  		wantTLS  bool
   100  	}{
   101  		{
   102  			addr:    addr1,
   103  			withTLS: true,
   104  
   105  			uri: grpcScheme + "://" + addr2,
   106  
   107  			wantHost: addr2,
   108  			wantTLS:  false,
   109  		},
   110  		{
   111  			addr:    addr1,
   112  			withTLS: false,
   113  
   114  			uri: grpcTLSScheme + "://" + addr2,
   115  
   116  			wantHost: addr2,
   117  			wantTLS:  true,
   118  		},
   119  	}
   120  
   121  	for _, test := range testCases {
   122  		// order:
   123  		// 1. WithNetworkAddress
   124  		// 2. WithTLSCfg(if test.withTLS == true)
   125  		// 3. WithNetworkURIAddress
   126  		config := &cfg{}
   127  		opts := []Option{WithNetworkAddress(test.addr)}
   128  
   129  		if test.withTLS {
   130  			opts = append(opts, WithTLSCfg(&tls.Config{}))
   131  		}
   132  
   133  		opts = append(opts, WithNetworkURIAddress(test.uri, nil)...)
   134  
   135  		for _, opt := range opts {
   136  			opt(config)
   137  		}
   138  
   139  		require.Equal(t, test.wantHost, config.addr, test.addr)
   140  		require.Equal(t, test.wantTLS, config.tlsCfg != nil, test.addr)
   141  	}
   142  }
   143  
   144  func Test_WithNetworkURIAddress_WithTLS_WithNetworkAddress(t *testing.T) {
   145  	addr1, addr2 := "example1.com:8080", "example2.com:8080"
   146  
   147  	testCases := []struct {
   148  		addr    string
   149  		withTLS bool
   150  
   151  		uri string
   152  
   153  		wantHost string
   154  		wantTLS  bool
   155  	}{
   156  		{
   157  			uri: grpcScheme + "://" + addr1,
   158  
   159  			addr:    addr2,
   160  			withTLS: true,
   161  
   162  			wantHost: addr2,
   163  			wantTLS:  true,
   164  		},
   165  		{
   166  			uri: grpcTLSScheme + "://" + addr1,
   167  
   168  			addr:    addr2,
   169  			withTLS: false,
   170  
   171  			wantHost: addr2,
   172  			wantTLS:  true,
   173  		},
   174  	}
   175  
   176  	for _, test := range testCases {
   177  		// order:
   178  		// 1. WithNetworkURIAddress
   179  		// 2. WithNetworkAddress
   180  		// 3. WithTLSCfg(if test.withTLS == true)
   181  		config := &cfg{}
   182  		opts := WithNetworkURIAddress(test.uri, nil)
   183  
   184  		opts = append(opts, WithNetworkAddress(test.addr))
   185  
   186  		if test.withTLS {
   187  			opts = append(opts, WithTLSCfg(&tls.Config{}))
   188  		}
   189  
   190  		for _, opt := range opts {
   191  			opt(config)
   192  		}
   193  
   194  		require.Equal(t, test.wantHost, config.addr, test.uri)
   195  		require.Equal(t, test.wantTLS, config.tlsCfg != nil, test.uri)
   196  	}
   197  }