Skip to content

Commit

Permalink
support set nacos ContextPath (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin0325 authored Dec 10, 2021
1 parent 8f207c3 commit d411e11
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
13 changes: 11 additions & 2 deletions remoting/nacos/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ func GetNacosConfig(url *common.URL) ([]nacosConstant.ServerConfig, nacosConstan
return []nacosConstant.ServerConfig{}, nacosConstant.ClientConfig{},
perrors.WithMessagef(err, "split [%s] ", addr)
}
port, _ := strconv.Atoi(portStr)
serverConfigs = append(serverConfigs, nacosConstant.ServerConfig{IpAddr: ip, Port: uint64(port)})
portContextPath := strings.Split(portStr, constant.PathSeparator)
port, err := strconv.Atoi(portContextPath[0])
if err != nil {
return []nacosConstant.ServerConfig{}, nacosConstant.ClientConfig{},
perrors.WithMessagef(err, "port [%s] ", portContextPath[0])
}
var contextPath string
if len(portContextPath) > 1 {
contextPath = constant.PathSeparator + strings.Join(portContextPath[1:], constant.PathSeparator)
}
serverConfigs = append(serverConfigs, nacosConstant.ServerConfig{IpAddr: ip, Port: uint64(port), ContextPath: contextPath})
}

timeout := url.GetParamDuration(constant.TimeoutKey, constant.DefaultRegTimeout)
Expand Down
58 changes: 38 additions & 20 deletions remoting/nacos/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,44 @@ import (
)

func TestNewNacosClient(t *testing.T) {
rc := &config.RemoteConfig{}
rc.Protocol = "nacos"
rc.Username = "nacos"
client, err := NewNacosClient(rc)

// address is nil
assert.Nil(t, client)
assert.NotNil(t, err)

rc.Address = "console.nacos.io:80:123"
client, err = NewNacosClient(rc)
// invalid address
assert.Nil(t, client)
assert.NotNil(t, err)

rc.Address = "console.nacos.io:80"
rc.Timeout = "10s"
client, err = NewNacosClient(rc)
assert.NotNil(t, client)
assert.Nil(t, err)
t.Run("AddressIsNil", func(t *testing.T) {
rc := &config.RemoteConfig{}
rc.Protocol = "nacos"
rc.Username = "nacos"
client, err := NewNacosClient(rc)

// address is nil
assert.Nil(t, client)
assert.NotNil(t, err)
})

t.Run("InvalidAddress", func(t *testing.T) {
rc := &config.RemoteConfig{}
rc.Address = "console.nacos.io:80:123"
client, err := NewNacosClient(rc)
// invalid address
assert.Nil(t, client)
assert.NotNil(t, err)
})

t.Run("Normal", func(t *testing.T) {
rc := &config.RemoteConfig{}
rc.Address = "console.nacos.io:80"
rc.Protocol = "nacos"
rc.Timeout = "10s"
client, err := NewNacosClient(rc)
assert.NotNil(t, client)
assert.Nil(t, err)
})

t.Run("NormalHasContextPath", func(t *testing.T) {
rc := &config.RemoteConfig{}
rc.Address = "console.nacos.io:80/nacos"
rc.Protocol = "nacos"
client, err := NewNacosClient(rc)
assert.NotNil(t, client)
assert.Nil(t, err)
})
}

func TestGetNacosConfig(t *testing.T) {
Expand Down

0 comments on commit d411e11

Please sign in to comment.