github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/common/net_helper_test.go (about)

     1  package common
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestGetHostPort(t *testing.T) {
    10  	t.Run("Check Host parsing", func(t *testing.T) {
    11  		type testTableType struct {
    12  			name    string
    13  			input   string
    14  			success bool
    15  			host    string
    16  			port    int
    17  		}
    18  		for _, testconfig := range []testTableType{
    19  			{
    20  				name:    "only host",
    21  				input:   "localhost",
    22  				success: true,
    23  				host:    "localhost",
    24  				port:    0,
    25  			},
    26  			{
    27  				name:    "host and port",
    28  				input:   "localhost:1234",
    29  				success: true,
    30  				host:    "localhost",
    31  				port:    1234,
    32  			},
    33  			{
    34  				name:    "with tcp url",
    35  				input:   "tcp://docker:2375",
    36  				success: true,
    37  				host:    "docker",
    38  				port:    2375,
    39  			},
    40  			{
    41  				name:    "with http url",
    42  				input:   "http://localhost:8080/app/index.html",
    43  				success: true,
    44  				host:    "localhost",
    45  				port:    8080,
    46  			},
    47  			{
    48  				name:    "with http url without port",
    49  				input:   "http://localhost/app/index.html",
    50  				success: true,
    51  				host:    "localhost",
    52  				port:    80,
    53  			},
    54  			{
    55  				name:    "with ssh url without port",
    56  				input:   "ssh://localhost",
    57  				success: true,
    58  				host:    "localhost",
    59  				port:    22,
    60  			},
    61  			{
    62  				name:    "with ldap url and user/password",
    63  				input:   "ldap://user:password@ldapserver.de",
    64  				success: true,
    65  				host:    "ldapserver.de",
    66  				port:    389,
    67  			},
    68  			{
    69  				name:    "with ldap url without port",
    70  				input:   "ldaps://ldapserver",
    71  				success: true,
    72  				host:    "ldapserver",
    73  				port:    636,
    74  			},
    75  		} {
    76  			t.Run(testconfig.name, func(t *testing.T) {
    77  				host, port, err := GetHostPort(testconfig.input)
    78  				if testconfig.success {
    79  					assert.NoErrorf(t, err, "unexpected error %s", err)
    80  					assert.Equalf(t, testconfig.host, host, "entry returned wrong host ('%s' <>'%s)", host, testconfig.host)
    81  					assert.Equalf(t, testconfig.port, port, "entry returned wrong port ('%d' <>'%d)", port, testconfig.port)
    82  				} else {
    83  					assert.Error(t, err, "Expected error not set")
    84  				}
    85  			})
    86  		}
    87  	})
    88  }
    89  func TestSetHostPort(t *testing.T) {
    90  	t.Run("Test SetHostPort ipv4", func(t *testing.T) {
    91  		actual := SetHostPort("localhost", 1234)
    92  		assert.Equalf(t, "localhost:1234", actual, "actual not expected %s", actual)
    93  	})
    94  	t.Run("Test SetHostPort tcpv6", func(t *testing.T) {
    95  		actual := SetHostPort("fe80::3436:bd7c:3037:df6f", 1234)
    96  		assert.Equalf(t, "[fe80::3436:bd7c:3037:df6f]:1234", actual, "actual not expected: %s", actual)
    97  	})
    98  	t.Run("Test SetHostPort noport", func(t *testing.T) {
    99  		actual := SetHostPort("localhost", 0)
   100  		assert.Equalf(t, "localhost", actual, "actual not expected: %s", actual)
   101  	})
   102  }
   103  
   104  func TestGetHostname(t *testing.T) {
   105  	t.Run("Test GetHostname", func(t *testing.T) {
   106  		actual := GetHostname()
   107  		assert.NotEmpty(t, actual, "Hostname empty")
   108  		assert.Contains(t, actual, ".", "Hostname not FQDN")
   109  		t.Logf("Hostname: %s", actual)
   110  	})
   111  }