|
| 1 | +package healthcheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCheckServerHealth_HealthyServer(t *testing.T) { |
| 15 | + // Start a test HTTP server |
| 16 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + w.WriteHeader(http.StatusOK) |
| 18 | + })) |
| 19 | + defer server.Close() |
| 20 | + |
| 21 | + // Parse server URL |
| 22 | + serverURL, err := url.Parse(server.URL) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("Failed to parse server URL: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + // Perform health check |
| 28 | + result := CheckServerHealth(serverURL, 3*time.Second) |
| 29 | + |
| 30 | + // Verify result |
| 31 | + if !result.Healthy { |
| 32 | + t.Errorf("Expected server to be healthy, got unhealthy") |
| 33 | + } |
| 34 | + if result.Status != HealthHealthy { |
| 35 | + t.Errorf("Expected status HealthHealthy, got %v", result.Status) |
| 36 | + } |
| 37 | + if result.Error != nil { |
| 38 | + t.Errorf("Expected no error, got: %v", result.Error) |
| 39 | + } |
| 40 | + if result.Duration <= 0 { |
| 41 | + t.Errorf("Expected positive duration, got: %v", result.Duration) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestCheckServerHealth_UnreachableServer(t *testing.T) { |
| 46 | + // Use a URL that should not be listening |
| 47 | + targetURL, err := url.Parse("http://localhost:59999") |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("Failed to parse URL: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Perform health check |
| 53 | + result := CheckServerHealth(targetURL, 1*time.Second) |
| 54 | + |
| 55 | + // Verify result |
| 56 | + if result.Healthy { |
| 57 | + t.Errorf("Expected server to be unhealthy, got healthy") |
| 58 | + } |
| 59 | + if result.Status != HealthUnreachable { |
| 60 | + t.Errorf("Expected status HealthUnreachable, got %v", result.Status) |
| 61 | + } |
| 62 | + if result.Error == nil { |
| 63 | + t.Errorf("Expected error, got nil") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestCheckServerHealth_DefaultPorts(t *testing.T) { |
| 68 | + testCases := []struct { |
| 69 | + name string |
| 70 | + urlString string |
| 71 | + expectedPort string |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "HTTP default port", |
| 75 | + urlString: "http://localhost", |
| 76 | + expectedPort: "80", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "HTTPS default port", |
| 80 | + urlString: "https://localhost", |
| 81 | + expectedPort: "443", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "Explicit port", |
| 85 | + urlString: "http://localhost:8080", |
| 86 | + expectedPort: "8080", |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + for _, tc := range testCases { |
| 91 | + t.Run(tc.name, func(t *testing.T) { |
| 92 | + targetURL, err := url.Parse(tc.urlString) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("Failed to parse URL: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + // Start a listener on the expected port to verify we're checking the right one |
| 98 | + listener, err := net.Listen("tcp", "localhost:"+tc.expectedPort) |
| 99 | + if err != nil { |
| 100 | + t.Skipf("Cannot bind to port %s: %v", tc.expectedPort, err) |
| 101 | + } |
| 102 | + defer listener.Close() |
| 103 | + |
| 104 | + // Perform health check |
| 105 | + result := CheckServerHealth(targetURL, 1*time.Second) |
| 106 | + |
| 107 | + // Should be healthy since we have a listener |
| 108 | + if !result.Healthy { |
| 109 | + t.Errorf("Expected server to be healthy on port %s, got unhealthy: %v", tc.expectedPort, result.Error) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestFormatHealthMessage_Healthy(t *testing.T) { |
| 116 | + targetURL, _ := url.Parse("http://localhost:3000") |
| 117 | + result := HealthCheckResult{ |
| 118 | + Status: HealthHealthy, |
| 119 | + Healthy: true, |
| 120 | + } |
| 121 | + |
| 122 | + msg := FormatHealthMessage(result, targetURL) |
| 123 | + |
| 124 | + if len(msg) == 0 { |
| 125 | + t.Errorf("Expected non-empty message") |
| 126 | + } |
| 127 | + if !strings.Contains(msg, "→") { |
| 128 | + t.Errorf("Expected message to contain →") |
| 129 | + } |
| 130 | + if !strings.Contains(msg, "Local server is reachable") { |
| 131 | + t.Errorf("Expected message to contain 'Local server is reachable'") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestFormatHealthMessage_Unhealthy(t *testing.T) { |
| 136 | + targetURL, _ := url.Parse("http://localhost:3000") |
| 137 | + result := HealthCheckResult{ |
| 138 | + Status: HealthUnreachable, |
| 139 | + Healthy: false, |
| 140 | + Error: net.ErrClosed, |
| 141 | + } |
| 142 | + |
| 143 | + msg := FormatHealthMessage(result, targetURL) |
| 144 | + |
| 145 | + if len(msg) == 0 { |
| 146 | + t.Errorf("Expected non-empty message") |
| 147 | + } |
| 148 | + // Should contain warning indicator |
| 149 | + if !strings.Contains(msg, "●") { |
| 150 | + t.Errorf("Expected message to contain ●") |
| 151 | + } |
| 152 | + if !strings.Contains(msg, "Warning") { |
| 153 | + t.Errorf("Expected message to contain 'Warning'") |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func TestFormatHealthMessage_NilError(t *testing.T) { |
| 158 | + targetURL, _ := url.Parse("http://localhost:3000") |
| 159 | + result := HealthCheckResult{ |
| 160 | + Status: HealthUnreachable, |
| 161 | + Healthy: false, |
| 162 | + Error: nil, // Nil error should not cause panic |
| 163 | + } |
| 164 | + |
| 165 | + msg := FormatHealthMessage(result, targetURL) |
| 166 | + |
| 167 | + if len(msg) == 0 { |
| 168 | + t.Errorf("Expected non-empty message") |
| 169 | + } |
| 170 | + if !strings.Contains(msg, "unknown error") { |
| 171 | + t.Errorf("Expected message to contain 'unknown error' when error is nil") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestCheckServerHealth_PortInURL(t *testing.T) { |
| 176 | + // Create a server on a non-standard port |
| 177 | + listener, err := net.Listen("tcp", "localhost:0") |
| 178 | + if err != nil { |
| 179 | + t.Fatalf("Failed to create listener: %v", err) |
| 180 | + } |
| 181 | + defer listener.Close() |
| 182 | + |
| 183 | + // Get the actual port assigned by the OS |
| 184 | + addr := listener.Addr().(*net.TCPAddr) |
| 185 | + targetURL, _ := url.Parse(fmt.Sprintf("http://localhost:%d/path", addr.Port)) |
| 186 | + |
| 187 | + // Perform health check |
| 188 | + result := CheckServerHealth(targetURL, 3*time.Second) |
| 189 | + |
| 190 | + // Verify that the health check succeeded |
| 191 | + // This confirms that when a port is already in the URL, we don't append |
| 192 | + // a default port (which would cause localhost:8080 to become localhost:8080:80) |
| 193 | + if !result.Healthy { |
| 194 | + t.Errorf("Expected healthy=true for server with port in URL, got false: %v", result.Error) |
| 195 | + } |
| 196 | + if result.Error != nil { |
| 197 | + t.Errorf("Expected no error for server with port in URL, got: %v", result.Error) |
| 198 | + } |
| 199 | +} |
0 commit comments