diff --git a/api/core.go b/api/core.go index 088e9c7f..364111c0 100644 --- a/api/core.go +++ b/api/core.go @@ -519,6 +519,12 @@ type ( Decimals uint32 `serix:""` } + // NetworkHealthResponse defines the network health response. + NetworkHealthResponse struct { + // Whether the network is healthy (finalization is not delayed). + IsNetworkHealthy bool `serix:""` + } + // NetworkMetricsResponse defines the network metrics response. NetworkMetricsResponse struct { // The current rate of new blocks per second, it's updated when a commitment is committed. diff --git a/api/core_test.go b/api/core_test.go index f0c92ba7..d87e3cfd 100644 --- a/api/core_test.go +++ b/api/core_test.go @@ -52,6 +52,15 @@ func Test_CoreAPIDeSerialize(t *testing.T) { SeriErr: nil, DeSeriErr: nil, }, + { + Name: "ok - NetworkHealthResponse", + Source: &api.NetworkHealthResponse{ + IsNetworkHealthy: true, + }, + Target: &api.NetworkHealthResponse{}, + SeriErr: nil, + DeSeriErr: nil, + }, { Name: "ok - NetworkMetricsResponse", Source: &api.NetworkMetricsResponse{ @@ -434,6 +443,15 @@ func Test_CoreAPIJSONSerialization(t *testing.T) { "subunit": "glow", "decimals": 6 } +}`, + }, + { + Name: "ok - NetworkHealthResponse", + Source: &api.NetworkHealthResponse{ + IsNetworkHealthy: true, + }, + Target: `{ + "isNetworkHealthy": true }`, }, { diff --git a/api/endpoints.go b/api/endpoints.go index f8dd4676..2013cdc7 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -90,6 +90,12 @@ const ( MIMEApplicationVendorIOTASerializerV2 = "application/vnd.iota.serializer-v2" ) +// HealthResponse defines the health response. +type HealthResponse struct { + // Whether the node is healthy. + IsHealthy bool `serix:""` +} + var ( // RouteHealth is the route for querying a node's health status. RouteHealth = "/health" diff --git a/nodeclient/http_api_client_test.go b/nodeclient/http_api_client_test.go index 117381f9..927b249a 100644 --- a/nodeclient/http_api_client_test.go +++ b/nodeclient/http_api_client_test.go @@ -146,18 +146,18 @@ func nodeClient(t *testing.T) *nodeclient.Client { func TestClient_Health(t *testing.T) { defer gock.Off() - gock.New(nodeAPIUrl). - Get(api.RouteHealth). - Reply(200) + mockGetJSON(api.RouteHealth, 200, &api.HealthResponse{ + IsHealthy: true, + }) nodeAPI := nodeClient(t) healthy, err := nodeAPI.Health(context.Background()) require.NoError(t, err) require.True(t, healthy) - gock.New(nodeAPIUrl). - Get(api.RouteHealth). - Reply(503) + mockGetJSON(api.RouteHealth, 503, &api.HealthResponse{ + IsHealthy: false, + }) healthy, err = nodeAPI.Health(context.Background()) require.NoError(t, err) @@ -167,18 +167,18 @@ func TestClient_Health(t *testing.T) { func TestClient_NetworkHealth(t *testing.T) { defer gock.Off() - gock.New(nodeAPIUrl). - Get(api.CoreRouteNetworkHealth). - Reply(200) + mockGetJSON(api.CoreRouteNetworkHealth, 200, &api.NetworkHealthResponse{ + IsNetworkHealthy: true, + }) nodeAPI := nodeClient(t) healthy, err := nodeAPI.NetworkHealth(context.Background()) require.NoError(t, err) require.True(t, healthy) - gock.New(nodeAPIUrl). - Get(api.CoreRouteNetworkHealth). - Reply(503) + mockGetJSON(api.CoreRouteNetworkHealth, 503, &api.NetworkHealthResponse{ + IsNetworkHealthy: false, + }) healthy, err = nodeAPI.NetworkHealth(context.Background()) require.NoError(t, err)