github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/api/internal/rest/config_test.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package rest
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/tiflow/pkg/security"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestCDCRESTClientCommonConfigs(t *testing.T) {
    24  	_, err := CDCRESTClientFromConfig(&Config{Host: "127.0.0.1"})
    25  	require.NotNil(t, err)
    26  
    27  	_, err = CDCRESTClientFromConfig(&Config{Host: "127.0.0.1", Version: "v1"})
    28  	require.NotNil(t, err)
    29  
    30  	_, err = CDCRESTClientFromConfig(&Config{Host: "127.0.0.1", APIPath: "/api"})
    31  	require.NotNil(t, err)
    32  
    33  	_, err = CDCRESTClientFromConfig(&Config{Host: "http://127.0.0.1:2379", APIPath: "/api", Version: "v1"})
    34  	require.Nil(t, err)
    35  
    36  	_, err = CDCRESTClientFromConfig(&Config{Host: "127.0.0.1:2379", APIPath: "/api", Version: "v2"})
    37  	require.Nil(t, err)
    38  }
    39  
    40  func checkTLS(config *Config) bool {
    41  	baseURL, _, err := defaultServerURLFromConfig(config)
    42  	if err != nil {
    43  		return false
    44  	}
    45  	return baseURL.Scheme == "https"
    46  }
    47  
    48  func TestCDCRESTClientUsingTLS(t *testing.T) {
    49  	testCases := []struct {
    50  		Config   *Config
    51  		UsingTLS bool
    52  	}{
    53  		{
    54  			Config:   &Config{},
    55  			UsingTLS: false,
    56  		},
    57  		{
    58  			Config: &Config{
    59  				Host: "https://127.0.0.1",
    60  			},
    61  			UsingTLS: true,
    62  		},
    63  		{
    64  			Config: &Config{
    65  				Host: "127.0.0.1",
    66  				Credential: &security.Credential{
    67  					CAPath:   "foo",
    68  					CertPath: "bar",
    69  					KeyPath:  "test",
    70  				},
    71  			},
    72  			UsingTLS: true,
    73  		},
    74  		{
    75  			Config: &Config{
    76  				Host: "///:://127.0.0.1",
    77  				Credential: &security.Credential{
    78  					CAPath:   "foo",
    79  					CertPath: "bar",
    80  					KeyPath:  "test",
    81  				},
    82  			},
    83  			UsingTLS: false,
    84  		},
    85  	}
    86  
    87  	for _, tc := range testCases {
    88  		usingTLS := checkTLS(tc.Config)
    89  		require.Equal(t, usingTLS, tc.UsingTLS)
    90  	}
    91  }