Skip to content

Commit

Permalink
Merge pull request #713 from iotaledger/feat/api-health-json-responses
Browse files Browse the repository at this point in the history
Add json responses for health endpoints
  • Loading branch information
muXxer authored Mar 21, 2024
2 parents 236e7ef + d618189 commit 9695e14
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
6 changes: 6 additions & 0 deletions api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions api/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}`,
},
{
Expand Down
6 changes: 6 additions & 0 deletions api/routes.go → api/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
)

type (
// HealthResponse defines the health response.
HealthResponse struct {
// Whether the node is healthy.
IsHealthy bool `serix:""`
}

// RoutesResponse defines the response of a GET routes REST API call.
RoutesResponse struct {
Routes []iotago.PrefixedStringUint8 `serix:",lenPrefix=uint8"`
Expand Down
20 changes: 18 additions & 2 deletions api/routes_test.go → api/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ import (
"github.com/iotaledger/iota.go/v4/tpkg/frameworks"
)

func Test_RoutesAPIDeSerialize(t *testing.T) {
func Test_RootAPIDeSerialize(t *testing.T) {
tests := []*frameworks.DeSerializeTest{
{
Name: "ok - HealthResponse",
Source: &api.HealthResponse{
IsHealthy: true,
},
Target: &api.HealthResponse{},
},
{
Name: "ok - RoutesResponse",
Source: &api.RoutesResponse{
Expand All @@ -24,8 +31,17 @@ func Test_RoutesAPIDeSerialize(t *testing.T) {
}
}

func Test_RoutesAPIJSONSerialization(t *testing.T) {
func Test_RootAPIJSONSerialization(t *testing.T) {
tests := []*frameworks.JSONEncodeTest{
{
Name: "ok - HealthResponse",
Source: &api.HealthResponse{
IsHealthy: true,
},
Target: `{
"isHealthy": true
}`,
},
{
Name: "ok - RoutesResponse",
Source: &api.RoutesResponse{
Expand Down
24 changes: 12 additions & 12 deletions nodeclient/http_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9695e14

Please sign in to comment.