Repository: nacos-group/nacos-sdk-go (go)

Issue:
生成读取配置的url时，未使用传入的scheme配置，导致无法使用https协议
生成读取配置的url时，未使用传入的scheme配置，导致无法使用https协议
https://github.com/nacos-group/nacos-sdk-go/blob/fb45465888781748568f906fe55137f718a31af7/common/nacos_server/nacos_server.go#L315

Pull request:
fix ServerConfig scheme was ignored in nacos server
Fixes #149 

Patch:
diff --git a/README.md b/README.md
index 640760f..4be1d95 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,7 @@ constant.ServerConfig{
 	ContextPath string // the nacos server context path
 	IpAddr      string // the nacos server address
 	Port        uint64 // the nacos server port
+	Scheme      string // the nacos server scheme
 }
 ```
 
@@ -76,11 +77,13 @@ serverConfigs := []constant.ServerConfig{
         IpAddr:      "console1.nacos.io",
         ContextPath: "/nacos",
         Port:        80,
+        Scheme:      "http",
     },
     {
     	IpAddr:      "console2.nacos.io",
     	ContextPath: "/nacos",
     	Port:        80,
+        Scheme:      "http",
     },
 }
 
diff --git a/README_CN.md b/README_CN.md
index e519b6d..a95c03e 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -51,6 +51,7 @@ constant.ServerConfig{
 	ContextPath string // Nacos的ContextPath
 	IpAddr      string // Nacos的服务地址
 	Port        uint64 // Nacos的服务端口
+	Scheme      string // Nacos的服务地址前缀
 }
 ```
 
@@ -76,11 +77,13 @@ serverConfigs := []constant.ServerConfig{
         IpAddr:      "console1.nacos.io",
         ContextPath: "/nacos",
         Port:        80,
+        Scheme:      "http",
     },
     {
     	IpAddr:      "console2.nacos.io",
     	ContextPath: "/nacos",
     	Port:        80,
+        Scheme:      "http",
     },
 }
 
diff --git a/clients/nacos_client/nacos_client.go b/clients/nacos_client/nacos_client.go
index 03484b5..badd5cf 100644
--- a/clients/nacos_client/nacos_client.go
+++ b/clients/nacos_client/nacos_client.go
@@ -93,6 +93,9 @@ func (client *NacosClient) SetServerConfig(configs []constant.ServerConfig) (err
 		if len(configs[i].ContextPath) <= 0 {
 			configs[i].ContextPath = constant.DEFAULT_CONTEXT_PATH
 		}
+		if len(configs[i].Scheme) <= 0 {
+			configs[i].Scheme = constant.DEFAULT_SERVER_SCHEME
+		}
 	}
 	client.serverConfigs = configs
 	client.serverConfigsValid = true
diff --git a/common/constant/const.go b/common/constant/const.go
index 10f63fa..89f7b22 100644
--- a/common/constant/const.go
+++ b/common/constant/const.go
@@ -73,4 +73,5 @@ const (
 	DEFAULT_GROUP               = "DEFAULT_GROUP"
 	NAMING_INSTANCE_ID_SPLITTER = "#"
 	DefaultClientErrorCode      = "SDK.NacosError"
+	DEFAULT_SERVER_SCHEME       = "http"
 )
diff --git a/common/nacos_server/nacos_server.go b/common/nacos_server/nacos_server.go
index 445ee80..9922bf5 100644
--- a/common/nacos_server/nacos_server.go
+++ b/common/nacos_server/nacos_server.go
@@ -85,10 +85,6 @@ func (server *NacosServer) callConfigServer(api string, params map[string]string
 
 	signHeaders := getSignHeaders(params, newHeaders)
 
-	//接受配置协议头（http,https）
-	if strings.Index(curServer, "//") <= -1 {
-		curServer = "http://" + curServer
-	}
 	url := curServer + contextPath + api
 
 	headers := map[string][]string{}
@@ -139,10 +135,6 @@ func (server *NacosServer) callServer(api string, params map[string]string, meth
 		contextPath = constant.WEB_CONTEXT
 	}
 
-	//接受配置协议头（http,https）
-	if strings.Index(curServer, "//") <= -1 {
-		curServer = "http://" + curServer
-	}
 	url := curServer + contextPath + api
 
 	headers := map[string][]string{}
@@ -313,7 +305,10 @@ func injectSecurityInfo(server *NacosServer, param map[string]string) {
 }
 
 func getAddress(cfg constant.ServerConfig) string {
-	return cfg.IpAddr + ":" + strconv.Itoa(int(cfg.Port))
+	if strings.Index(cfg.IpAddr, "http://") >= 0 || strings.Index(cfg.IpAddr, "https://") >= 0 {
+		return cfg.IpAddr + ":" + strconv.Itoa(int(cfg.Port))
+	}
+	return cfg.Scheme + "://" + cfg.IpAddr + ":" + strconv.Itoa(int(cfg.Port))
 }
 
 func getSignHeaders(params map[string]string, newHeaders map[string]string) map[string]string {


Test patch:
diff --git a/clients/config_client/config_client_test.go b/clients/config_client/config_client_test.go
index 30de0b2..04f6306 100644
--- a/clients/config_client/config_client_test.go
+++ b/clients/config_client/config_client_test.go
@@ -88,7 +88,7 @@ var serverConfigsTest = []constant.ServerConfig{serverConfigTest}
 
 var httpAgentTest = mock.MockIHttpAgent{}
 
-func cretateConfigClientTest() ConfigClient {
+func createConfigClientTest() ConfigClient {
 	nc := nacos_client.NacosClient{}
 	nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
 	nc.SetClientConfig(clientConfigTest)
@@ -97,7 +97,7 @@ func cretateConfigClientTest() ConfigClient {
 	return client
 }
 
-func cretateConfigClientTestWithTenant() ConfigClient {
+func createConfigClientTestWithTenant() ConfigClient {
 	nc := nacos_client.NacosClient{}
 	nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
 	nc.SetClientConfig(clientConfigTestWithTenant)
@@ -106,7 +106,7 @@ func cretateConfigClientTestWithTenant() ConfigClient {
 	return client
 }
 
-func cretateConfigClientHttpTest(mockHttpAgent http_agent.IHttpAgent) ConfigClient {
+func createConfigClientHttpTest(mockHttpAgent http_agent.IHttpAgent) ConfigClient {
 	nc := nacos_client.NacosClient{}
 	nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
 	nc.SetClientConfig(clientConfigTest)
@@ -115,7 +115,7 @@ func cretateConfigClientHttpTest(mockHttpAgent http_agent.IHttpAgent) ConfigClie
 	return client
 }
 
-func cretateConfigClientHttpTestWithTenant(mockHttpAgent http_agent.IHttpAgent) ConfigClient {
+func createConfigClientHttpTestWithTenant(mockHttpAgent http_agent.IHttpAgent) ConfigClient {
 	nc := nacos_client.NacosClient{}
 	nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
 	nc.SetClientConfig(clientConfigTestWithTenant)
@@ -126,7 +126,7 @@ func cretateConfigClientHttpTestWithTenant(mockHttpAgent http_agent.IHttpAgent)
 
 func Test_GetConfig(t *testing.T) {
 
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	success, err := client.PublishConfig(vo.ConfigParam{
 		DataId:  "dataId",
 		Group:   "group",
@@ -144,7 +144,7 @@ func Test_GetConfig(t *testing.T) {
 }
 
 func Test_SearchConfig(t *testing.T) {
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	configPage, err := client.SearchConfig(vo.SearchConfigParm{
 		Search:   "accurate",
 		DataId:   "",
@@ -161,7 +161,7 @@ func Test_GetConfigWithErrorResponse_401(t *testing.T) {
 	controller := gomock.NewController(t)
 	defer controller.Finish()
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodGet),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
 		gomock.AssignableToTypeOf(http.Header{}),
@@ -177,7 +177,7 @@ func Test_GetConfigWithErrorResponse_404(t *testing.T) {
 	controller := gomock.NewController(t)
 	defer controller.Finish()
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodGet),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
 		gomock.AssignableToTypeOf(http.Header{}),
@@ -194,7 +194,7 @@ func Test_GetConfigWithErrorResponse_403(t *testing.T) {
 	controller := gomock.NewController(t)
 	defer controller.Finish()
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodGet),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
 		gomock.AssignableToTypeOf(http.Header{}),
@@ -212,7 +212,7 @@ func Test_GetConfigWithCache(t *testing.T) {
 	defer controller.Finish()
 
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodGet),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
@@ -238,7 +238,7 @@ func Test_GetConfigWithCache(t *testing.T) {
 // PublishConfig
 
 func Test_PublishConfigWithoutDataId(t *testing.T) {
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	_, err := client.PublishConfig(vo.ConfigParam{
 		DataId:  "",
 		Group:   "group",
@@ -248,7 +248,7 @@ func Test_PublishConfigWithoutDataId(t *testing.T) {
 }
 
 func Test_PublishConfigWithoutGroup(t *testing.T) {
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	_, err := client.PublishConfig(vo.ConfigParam{
 		DataId:  "dataId",
 		Group:   "",
@@ -258,7 +258,7 @@ func Test_PublishConfigWithoutGroup(t *testing.T) {
 }
 
 func Test_PublishConfigWithoutContent(t *testing.T) {
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	_, err := client.PublishConfig(vo.ConfigParam{
 		DataId:  "dataId",
 		Group:   "group",
@@ -269,7 +269,7 @@ func Test_PublishConfigWithoutContent(t *testing.T) {
 
 func Test_PublishConfig(t *testing.T) {
 
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 
 	success, err := client.PublishConfig(vo.ConfigParam{
 		DataId:  "dataId",
@@ -285,7 +285,7 @@ func Test_PublishConfigWithErrorResponse(t *testing.T) {
 	defer controller.Finish()
 
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodPost),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
 		gomock.AssignableToTypeOf(http.Header{}),
@@ -302,7 +302,7 @@ func Test_PublishConfigWithErrorResponse_200(t *testing.T) {
 	defer controller.Finish()
 
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodPost),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
 		gomock.AssignableToTypeOf(http.Header{}),
@@ -318,7 +318,7 @@ func Test_PublishConfigWithErrorResponse_200(t *testing.T) {
 
 func Test_DeleteConfig(t *testing.T) {
 
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 
 	success, err := client.PublishConfig(vo.ConfigParam{
 		DataId:  "dataId",
@@ -341,7 +341,7 @@ func Test_DeleteConfigWithErrorResponse_200(t *testing.T) {
 	defer controller.Finish()
 
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodDelete),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
@@ -359,7 +359,7 @@ func Test_DeleteConfigWithErrorResponse_401(t *testing.T) {
 	defer controller.Finish()
 
 	mockHttpAgent := mock.NewMockIHttpAgent(controller)
-	client := cretateConfigClientHttpTest(mockHttpAgent)
+	client := createConfigClientHttpTest(mockHttpAgent)
 
 	mockHttpAgent.EXPECT().Request(gomock.Eq(http.MethodDelete),
 		gomock.Eq("http://console.nacos.io:80/nacos/v1/cs/configs"),
@@ -373,7 +373,7 @@ func Test_DeleteConfigWithErrorResponse_401(t *testing.T) {
 }
 
 func Test_DeleteConfigWithoutDataId(t *testing.T) {
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	success, err := client.DeleteConfig(vo.ConfigParam{
 		DataId: "",
 		Group:  "group",
@@ -383,7 +383,7 @@ func Test_DeleteConfigWithoutDataId(t *testing.T) {
 }
 
 func Test_DeleteConfigWithoutGroup(t *testing.T) {
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	success, err := client.DeleteConfig(vo.ConfigParam{
 		DataId: "dataId",
 		Group:  "",
@@ -396,7 +396,7 @@ func Test_DeleteConfigWithoutGroup(t *testing.T) {
 func TestListen(t *testing.T) {
 	// ListenConfig
 	t.Run("TestListenConfig", func(t *testing.T) {
-		client := cretateConfigClientTest()
+		client := createConfigClientTest()
 		key := util.GetConfigCacheKey(localConfigTest.DataId, localConfigTest.Group, clientConfigTest.NamespaceId)
 		cache.WriteConfigToFile(key, client.configCacheDir, "")
 		var err error
@@ -438,13 +438,13 @@ func TestListen(t *testing.T) {
 			OnChange: func(namespace, group, dataId, data string) {
 			},
 		}
-		client := cretateConfigClientTest()
+		client := createConfigClientTest()
 		err := client.ListenConfig(listenConfigParam)
 		assert.Error(t, err)
 	})
 	// ListenConfig no change
 	t.Run("TestListenConfigNoChange", func(t *testing.T) {
-		client := cretateConfigClientTest()
+		client := createConfigClientTest()
 		key := util.GetConfigCacheKey("ConfigNoChange", localConfigTest.Group, clientConfigTest.NamespaceId)
 		cache.WriteConfigToFile(key, client.configCacheDir, localConfigTest.Content)
 		var err error
@@ -484,12 +484,12 @@ func TestListen(t *testing.T) {
 				ch <- data
 			},
 		}
-		client := cretateConfigClientTest()
+		client := createConfigClientTest()
 		key := util.GetConfigCacheKey(listenConfigParam.DataId, listenConfigParam.Group, clientConfigTest.NamespaceId)
 		cache.WriteConfigToFile(key, client.configCacheDir, "")
 		client.ListenConfig(listenConfigParam)
 
-		client1 := cretateConfigClientTest()
+		client1 := createConfigClientTest()
 		client1.ListenConfig(listenConfigParam)
 
 		success, err := client.PublishConfig(vo.ConfigParam{
@@ -519,12 +519,12 @@ func TestListen(t *testing.T) {
 				ch <- data
 			},
 		}
-		client := cretateConfigClientTest()
+		client := createConfigClientTest()
 		key := util.GetConfigCacheKey(listenConfigParam.DataId, listenConfigParam.Group, clientConfigTest.NamespaceId)
 		cache.WriteConfigToFile(key, client.configCacheDir, "")
 		client.ListenConfig(listenConfigParam)
 
-		client1 := cretateConfigClientTest()
+		client1 := createConfigClientTest()
 		client1.ListenConfig(listenConfigParam)
 
 		success, err := client.PublishConfig(vo.ConfigParam{
@@ -549,7 +549,7 @@ func TestListen(t *testing.T) {
 func TestCancelListenConfig(t *testing.T) {
 	//Multiple listeners listen for different configurations, cancel one
 	t.Run("TestMultipleListenersCancelOne", func(t *testing.T) {
-		client := cretateConfigClientTest()
+		client := createConfigClientTest()
 		var err error
 		var success bool
 		var context string
@@ -607,7 +607,7 @@ func TestCancelListenConfig(t *testing.T) {
 		var context string
 		var err error
 		ch := make(chan string)
-		client := cretateConfigClientTest()
+		client := createConfigClientTest()
 		//
 		key := util.GetConfigCacheKey(localConfigTest.DataId, localConfigTest.Group, clientConfigTest.NamespaceId)
 		cache.WriteConfigToFile(key, client.configCacheDir, "")
@@ -652,7 +652,7 @@ func TestCancelListenConfig(t *testing.T) {
 func TestGetConfigWithSpecialSymbol(t *testing.T) {
 	contentStr := "hello world!!@#$%^&&*()"
 
-	client := cretateConfigClientTest()
+	client := createConfigClientTest()
 	success, err := client.PublishConfig(vo.ConfigParam{
 		DataId:  "special_symbol",
 		Group:   localConfigTest.Group,
diff --git a/common/nacos_server/nacos_server_test.go b/common/nacos_server/nacos_server_test.go
new file mode 100644
index 0000000..010d6e3
--- /dev/null
+++ b/common/nacos_server/nacos_server_test.go
@@ -0,0 +1,48 @@
+/*
+ * Copyright 1999-2020 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package nacos_server
+
+import (
+	"testing"
+
+	"github.com/nacos-group/nacos-sdk-go/common/constant"
+	"github.com/stretchr/testify/assert"
+)
+
+func Test_getAddressWithScheme(t *testing.T) {
+	var serverConfigTest = constant.ServerConfig{
+		ContextPath: "/nacos",
+		Port:        80,
+		IpAddr:      "console.nacos.io",
+		Scheme:      "https",
+	}
+	address := getAddress(serverConfigTest)
+	assert.Equal(t, "https://console.nacos.io:80", address)
+}
+
+func Test_getAddressWithoutScheme(t *testing.T) {
+	serverConfigTest := constant.ServerConfig{
+		ContextPath: "/nacos",
+		Port:        80,
+		IpAddr:      "http://console.nacos.io",
+	}
+	assert.Equal(t, "http://console.nacos.io:80", getAddress(serverConfigTest))
+
+	serverConfigTest.IpAddr = "https://console.nacos.io"
+	assert.Equal(t, "https://console.nacos.io:80", getAddress(serverConfigTest))
+
+}