From 516da81e919d2a4492aa1e108cc16c55512473d4 Mon Sep 17 00:00:00 2001 From: oidq Date: Tue, 11 Nov 2025 22:19:57 +0100 Subject: [PATCH] test: convert TestSplitHostPort to table driven test --- ocis-pkg/handlers/checker_test.go | 104 +++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 16 deletions(-) diff --git a/ocis-pkg/handlers/checker_test.go b/ocis-pkg/handlers/checker_test.go index 25e9ec468a6..871c413f867 100644 --- a/ocis-pkg/handlers/checker_test.go +++ b/ocis-pkg/handlers/checker_test.go @@ -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) + }) + } }