Replace balancer panic with deferred error; test HealthChecker

A malformed endpoint URL panicked inside Transport, crashing the host app
(often at startup from external config). Capture the parse error and surface
it from the transport on first use instead. Add the previously untested
HealthChecker coverage (initial probe, recovery, Stop termination, unknown
endpoint), raising balancer coverage from ~41% to ~87%. Default the health
probe path to /healthz to match this library's own server.
This commit is contained in:
2026-05-23 13:47:33 +03:00
parent b07d487e63
commit 01478be0dc
4 changed files with 134 additions and 3 deletions

View File

@@ -61,6 +61,27 @@ func TestTransport_PicksEndpointAndReplacesURL(t *testing.T) {
}
}
func TestTransport_InvalidEndpointURLReturnsError(t *testing.T) {
base := mockTransport(func(req *http.Request) (*http.Response, error) {
t.Fatal("base transport should not be reached for an invalid endpoint")
return nil, nil
})
// A malformed URL must not panic; the error surfaces on first use.
mw, closer := Transport([]Endpoint{{URL: "://missing-scheme"}})
defer closer.Close()
rt := mw(base)
req, err := http.NewRequest(http.MethodGet, "https://original.example.com/", nil)
if err != nil {
t.Fatal(err)
}
if _, err := rt.RoundTrip(req); err == nil {
t.Fatal("expected an error for invalid endpoint URL, got nil")
}
}
func TestTransport_ErrNoHealthyWhenNoEndpoints(t *testing.T) {
var endpoints []Endpoint
base := mockTransport(func(req *http.Request) (*http.Response, error) {