Skip to content

Commit

Permalink
fix: add body to readiness endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Feb 27, 2024
1 parent b905de1 commit cedbe10
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
23 changes: 20 additions & 3 deletions pkg/api/readiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@

package api

import "net/http"
import (
"net/http"

"github.com/ethersphere/bee"
"github.com/ethersphere/bee/pkg/jsonhttp"
)

type ReadyStatusResponse healthStatusResponse

func (s *Service) readinessHandler(w http.ResponseWriter, _ *http.Request) {
if s.probe.Ready() == ProbeStatusOK {
w.WriteHeader(http.StatusOK)
jsonhttp.OK(w, ReadyStatusResponse{
Status: "ready",
Version: bee.Version,
APIVersion: Version,
DebugAPIVersion: DebugVersion,
})
} else {
w.WriteHeader(http.StatusBadRequest)
jsonhttp.BadRequest(w, ReadyStatusResponse{
Status: "notReady",
Version: bee.Version,
APIVersion: Version,
DebugAPIVersion: DebugVersion,
})
}
}
16 changes: 14 additions & 2 deletions pkg/api/readiness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,22 @@ func TestReadiness(t *testing.T) {

// When we set readiness probe to OK it should indicate that API is ready
probe.SetReady(api.ProbeStatusOK)
jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusOK)
jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(api.ReadyStatusResponse{
Status: "ready",
Version: "-dev",
APIVersion: "0.0.0",
DebugAPIVersion: "0.0.0",
}))

// When we set readiness probe to NOK it should indicate that API is not ready
probe.SetReady(api.ProbeStatusNOK)
jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest)
jsonhttptest.Request(t, testServer, http.MethodGet, "/readiness", http.StatusBadRequest,
jsonhttptest.WithExpectedJSONResponse(api.ReadyStatusResponse{
Status: "notReady",
Version: "-dev",
APIVersion: "0.0.0",
DebugAPIVersion: "0.0.0",
}))
})
}

0 comments on commit cedbe10

Please sign in to comment.