Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions op/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
)

func newHealthCheckHandler(hc *Status) http.Handler {
if len(hc.checkers) == 0 {
return http.NotFoundHandler()
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(hc.checkers) == 0 {
http.NotFound(w, r)
return
}
w.Header().Add("Content-Type", "application/json")
if err := newEncoder(w).Encode(hc.Check()); err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -24,11 +24,12 @@ func newHealthCheckHandler(hc *Status) http.Handler {
}

func newReadyHandler(hc *Status) http.Handler {
if hc.ready == nil {
return http.NotFoundHandler()
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if hc.ready == nil {
http.NotFound(w, r)
return
}

if hc.ready() {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
Expand All @@ -40,7 +41,6 @@ func newReadyHandler(hc *Status) http.Handler {
}

func newAboutHandler(os *Status) http.Handler {

j, err := json.MarshalIndent(os.About(), " ", " ")
if err != nil {
panic(err)
Expand Down
49 changes: 47 additions & 2 deletions op/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ func TestHealthCheckHandler(t *testing.T) {
assert.Equal(expectedHealth, rr.Body.String())
}

func TestHealthCheckHandler_PostServeAddChecker(t *testing.T) {
assert := assert.New(t)

st := NewStatus("name", "desc")
h := newHealthCheckHandler(st)

req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()

h.ServeHTTP(rr, req)

assert.Equal(http.StatusNotFound, rr.Code, "Expected 404 when no checkers are registered")

st.AddChecker("check1", func(cr *CheckResponse) {
cr.Healthy("output1")
})

rr2 := httptest.NewRecorder()
h.ServeHTTP(rr2, req)

assert.Equal(http.StatusOK, rr2.Code, "Expected 200 after adding a healthy checker")
}

func TestReadyHandlerReady(t *testing.T) {
assert := assert.New(t)

Expand Down Expand Up @@ -162,7 +189,6 @@ func TestReadyHandlerNotReady(t *testing.T) {
}

func TestReadyHandlerNone(t *testing.T) {

h := newReadyHandler(NewStatus("", "").ReadyNone())

req, err := http.NewRequest("GET", "/", nil)
Expand All @@ -179,8 +205,27 @@ func TestReadyHandlerNone(t *testing.T) {
}
}

func TestReadyHandlerDefaults(t *testing.T) {
func TestReadyHandlerNone_PostServeHandlerReady(t *testing.T) {
st := NewStatus("", "").ReadyNone()
h := newReadyHandler(st)

req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code, "Expected 404 when readiness is set to none")

st.ReadyAlways()

rr2 := httptest.NewRecorder()
h.ServeHTTP(rr2, req)
assert.Equal(t, http.StatusOK, rr2.Code, "Expected 200 after changing readiness to always")
}

func TestReadyHandlerDefaults(t *testing.T) {
h := newReadyHandler(&Status{})

req, err := http.NewRequest("GET", "/", nil)
Expand Down