Refactor error handling tests to use structured test cases and add address validation in server start method

This commit is contained in:
Andy Oknen 2025-07-30 07:44:44 +00:00
parent 170e369a53
commit 51e1ef3ed0
2 changed files with 43 additions and 31 deletions

View file

@ -14,24 +14,44 @@ func TestWebUIServer_InvalidListenAddress(t *testing.T) {
logger := createTestLogger()
// Test various invalid listen addresses
invalidAddresses := []string{
"invalid:address",
"256.256.256.256:8080",
"localhost:-1",
"localhost:99999",
"not-a-valid-address",
"",
testCases := []struct {
addr string
shouldFail bool
description string
}{
{"invalid:address", true, "Invalid address format"},
{"256.256.256.256:8080", true, "Invalid IP address"},
{"localhost:-1", true, "Negative port"},
{"localhost:99999", true, "Port out of range"},
{"not-a-valid-address", true, "Completely invalid address"},
}
for _, addr := range invalidAddresses {
t.Run(fmt.Sprintf("Address_%s", addr), func(t *testing.T) {
server := Server(addr, logger)
for _, tc := range testCases {
t.Run(fmt.Sprintf("Address_%s_%s", tc.addr, tc.description), func(t *testing.T) {
server := Server(tc.addr, logger)
// Start should fail for invalid addresses
err := server.Start()
if err == nil {
// Use a timeout to prevent hanging on addresses that might partially work
done := make(chan error, 1)
go func() {
done <- server.Start()
}()
select {
case err := <-done:
if tc.shouldFail && err == nil {
_ = server.Stop() // Clean up if it somehow started
t.Errorf("Expected Start() to fail for invalid address %s", addr)
t.Errorf("Expected Start() to fail for invalid address %s", tc.addr)
} else if !tc.shouldFail && err != nil {
t.Errorf("Expected Start() to succeed for address %s, got error: %v", tc.addr, err)
}
case <-time.After(2 * time.Second):
// If it times out, the server might be listening, stop it
_ = server.Stop()
if tc.shouldFail {
t.Errorf("Start() did not fail quickly enough for invalid address %s", tc.addr)
} else {
t.Logf("Start() timed out for address %s, assuming it started successfully", tc.addr)
}
}
})
}
@ -246,22 +266,6 @@ func TestWebUIServer_LoggerNil(t *testing.T) {
}
}
func TestWebUIServer_EmptyListenAddress(t *testing.T) {
logger := createTestLogger()
// Test with empty listen address
server := Server("", logger)
// This might fail when trying to start
err := server.Start()
if err == nil {
_ = server.Stop()
t.Log("Note: Server started with empty listen address")
} else {
t.Logf("Expected behavior: Start() failed with empty address: %v", err)
}
}
func TestWebUIServer_RapidStartStop(t *testing.T) {
logger := createTestLogger()

View file

@ -2,6 +2,7 @@ package webui
import (
"fmt"
"net"
"net/http"
"time"
@ -22,6 +23,13 @@ func Server(listen string, log core.Logger) *WebUIServer {
}
func (w *WebUIServer) Start() error {
// Validate listen address before starting
if w.listen != "" {
if _, _, err := net.SplitHostPort(w.listen); err != nil {
return fmt.Errorf("invalid listen address: %v", err)
}
}
mux := http.NewServeMux()
// Setup static files handler (implementation varies by build)