dubbo.apache.org/dubbo-go/v3@v3.1.1/common/host_util_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package common
    19  
    20  import (
    21  	"os"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    31  )
    32  
    33  func TestGetLocalIp(t *testing.T) {
    34  	assert.NotNil(t, GetLocalIp())
    35  }
    36  
    37  func TestGetLocalHostName(t *testing.T) {
    38  	assert.NotNil(t, GetLocalHostName())
    39  }
    40  
    41  func TestHandleRegisterIPAndPort(t *testing.T) {
    42  	url := NewURLWithOptions(WithIp("1.2.3.4"), WithPort("20000"))
    43  	HandleRegisterIPAndPort(url)
    44  	assert.Equal(t, "1.2.3.4", url.Ip)
    45  	assert.Equal(t, "20000", url.Port)
    46  }
    47  
    48  func TestHandleRegisterIPAndPortBlank(t *testing.T) {
    49  	url, _ := NewURL("")
    50  	HandleRegisterIPAndPort(url)
    51  	assert.Equal(t, GetLocalIp(), url.Ip)
    52  	assert.Equal(t, constant.DubboDefaultPortToRegistry, url.Port)
    53  }
    54  
    55  func TestHandleRegisterIPAndPortWithEnv(t *testing.T) {
    56  	url, _ := NewURL("")
    57  	_ = os.Setenv(constant.DubboIpToRegistryKey, "1.2.3.4")
    58  	_ = os.Setenv(constant.DubboPortToRegistryKey, "20000")
    59  	HandleRegisterIPAndPort(url)
    60  	assert.Equal(t, "1.2.3.4", url.Ip)
    61  	assert.Equal(t, "20000", url.Port)
    62  }
    63  
    64  func TestHandleRegisterIPAndPortWithEnvInvalidPort(t *testing.T) {
    65  	url, _ := NewURL("")
    66  	_ = os.Setenv(constant.DubboIpToRegistryKey, "1.2.3.4")
    67  	_ = os.Setenv(constant.DubboPortToRegistryKey, "0")
    68  	HandleRegisterIPAndPort(url)
    69  	assert.Equal(t, "1.2.3.4", url.Ip)
    70  	assert.Equal(t, constant.DubboDefaultPortToRegistry, url.Port)
    71  }
    72  
    73  func TestIsValidPort(t *testing.T) {
    74  	assert.Equal(t, false, isValidPort(""))
    75  	assert.Equal(t, false, isValidPort("abc"))
    76  	assert.Equal(t, false, isValidPort("0"))
    77  	assert.Equal(t, false, isValidPort("65536"))
    78  	assert.Equal(t, true, isValidPort("20000"))
    79  }