(t *testing.T)
| 327 | } |
| 328 | |
| 329 | func TestReverseProxyHealthCheck(t *testing.T) { |
| 330 | // Start lightweight backend servers so they're ready before Caddy's |
| 331 | // active health checker runs; this avoids a startup race where the |
| 332 | // health checker probes backends that haven't yet begun accepting |
| 333 | // connections and marks them unhealthy. |
| 334 | // |
| 335 | // This mirrors how health checks are typically used in practice (to a separate |
| 336 | // backend service) and avoids probing the same Caddy instance while it's still |
| 337 | // provisioning and not ready to accept connections. |
| 338 | |
| 339 | // backend server that responds to proxied requests |
| 340 | helloSrv := &http.Server{ |
| 341 | Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 342 | _, _ = w.Write([]byte("Hello, World!")) |
| 343 | }), |
| 344 | } |
| 345 | ln0, err := net.Listen("tcp", "127.0.0.1:2020") |
| 346 | if err != nil { |
| 347 | t.Fatalf("failed to listen on 127.0.0.1:2020: %v", err) |
| 348 | } |
| 349 | go helloSrv.Serve(ln0) |
| 350 | t.Cleanup(func() { helloSrv.Close(); ln0.Close() }) |
| 351 | |
| 352 | // backend server that serves health checks |
| 353 | healthSrv := &http.Server{ |
| 354 | Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 355 | _, _ = w.Write([]byte("ok")) |
| 356 | }), |
| 357 | } |
| 358 | ln1, err := net.Listen("tcp", "127.0.0.1:2021") |
| 359 | if err != nil { |
| 360 | t.Fatalf("failed to listen on 127.0.0.1:2021: %v", err) |
| 361 | } |
| 362 | go healthSrv.Serve(ln1) |
| 363 | t.Cleanup(func() { healthSrv.Close(); ln1.Close() }) |
| 364 | |
| 365 | tester := caddytest.NewTester(t) |
| 366 | tester.InitServer(` |
| 367 | { |
| 368 | skip_install_trust |
| 369 | admin localhost:2999 |
| 370 | http_port 9080 |
| 371 | https_port 9443 |
| 372 | grace_period 1ns |
| 373 | } |
| 374 | http://localhost:9080 { |
| 375 | reverse_proxy { |
| 376 | to localhost:2020 |
| 377 | |
| 378 | health_uri /health |
| 379 | health_port 2021 |
| 380 | health_interval 10ms |
| 381 | health_timeout 100ms |
| 382 | health_passes 1 |
| 383 | health_fails 1 |
| 384 | } |
| 385 | } |
| 386 | `, "caddyfile") |
nothing calls this directly
no test coverage detected