gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/util/addr/addr_test.go (about)

     1  package addr
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  )
     7  
     8  func TestExtractor(t *testing.T) {
     9  	testData := []struct {
    10  		addr   string
    11  		expect string
    12  		parse  bool
    13  	}{
    14  		{"127.0.0.1", "127.0.0.1", false},
    15  		{"10.0.0.1", "10.0.0.1", false},
    16  		{"", "", true},
    17  		{"0.0.0.0", "", true},
    18  		{"[::]", "", true},
    19  	}
    20  
    21  	for _, d := range testData {
    22  		addr, err := Extract(d.addr)
    23  		if err != nil {
    24  			t.Errorf("Unexpected error %v", err)
    25  		}
    26  
    27  		if d.parse {
    28  			ip := net.ParseIP(addr)
    29  			if ip == nil {
    30  				t.Error("Unexpected nil IP")
    31  			}
    32  
    33  		} else if addr != d.expect {
    34  			t.Errorf("Expected %s got %s", d.expect, addr)
    35  		}
    36  	}
    37  
    38  }