test: convert TestSplitHostPort to table driven test

This commit is contained in:
oidq committed 2025-11-11 22:19:57 +01:00
1 parent 356e98e2bf
commit 516da81e91
1 file changed
+88 -16
+88 -16
View File
@@ -126,20 +126,92 @@ func TestCheckHandler(t *testing.T) {
})
}
func TestSplitHostPort(t *testing.T) {
address, port := handlers.SplitHostPort("10.10.10.10")
require.Equal(t, address, "10.10.10.10")
require.Equal(t, port, "")
address, port = handlers.SplitHostPort("10.10.10.10:20")
require.Equal(t, address, "10.10.10.10")
require.Equal(t, port, "20")
address, port = handlers.SplitHostPort("2a01::1")
require.Equal(t, address, "2a01::1")
require.Equal(t, port, "")
address, port = handlers.SplitHostPort("[2a01::1]:10")
require.Equal(t, address, "2a01::1")
require.Equal(t, port, "10")
var splitHostPortScenarios = []struct {
name string
input string
address string
port string
}{
{
"ipv4 host",
"10.10.10.10",
"10.10.10.10",
"",
},
{
"ipv4 host with port",
"10.10.10.10:123",
"10.10.10.10",
"123",
},
{
"port only",
":123",
"",
"123",
},
{
"ipv6 host",
"2a01::1",
"2a01::1",
"",
},
{
"ipv6 localhost",
"::1",
"::1",
"",
},
{
"ipv6 host in brackets",
"[2a01::1]",
"2a01::1",
"",
},
{
"ipv6 host with port",
"[2a01::1]:123",
"2a01::1",
"123",
},
{
"localhost",
"localhost",
"localhost",
"",
},
{
"localhost with port",
"localhost:123",
"localhost",
"123",
},
{
"localhost with port",
"localhost:123",
"localhost",
"123",
},
{
"number only",
"123",
"123",
"",
},
{
"double bracket",
"[aa::aa]]:123",
"[aa::aa]]:123",
"",
},
}
func TestSplitHostPort(t *testing.T) {
for _, scenario := range splitHostPortScenarios {
t.Run(scenario.name, func(t *testing.T) {
address, port := handlers.SplitHostPort(scenario.input)
require.Equal(t, scenario.address, address)
require.Equal(t, scenario.port, port)
})
}
}