Skip to content

Commit

Permalink
test(issue-2): added cases for liveness endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaba505 committed Sep 19, 2024
1 parent 102dcad commit fffcae6
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions rest/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,79 @@ func TestReadinessEndpoint(t *testing.T) {
})
})
}

func TestLivenessEndpoint(t *testing.T) {
t.Run("will return http 200 status code", func(t *testing.T) {
t.Run("if the health metric is healthy", func(t *testing.T) {
var m health.Binary
if !assert.True(t, m.Healthy(context.Background())) {
return
}

e := livenessEndpoint(&m)

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/health/liveness", nil)

e.ServeHTTP(w, r)

resp := w.Result()
if !assert.Equal(t, http.StatusOK, resp.StatusCode) {
return
}

b, err := io.ReadAll(resp.Body)
if !assert.Nil(t, err) {
return
}
if !assert.Empty(t, b) {
return
}
})
})

t.Run("will return http 503 status code", func(t *testing.T) {
t.Run("if the health metric is unhealthy", func(t *testing.T) {
var m health.Binary
m.Toggle()
if !assert.False(t, m.Healthy(context.Background())) {
return
}

e := livenessEndpoint(&m)

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/health/liveness", nil)

e.ServeHTTP(w, r)

resp := w.Result()
if !assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) {
return
}
if !assert.Equal(t, "application/x-protobuf", resp.Header.Get("Content-Type")) {
return
}

b, err := io.ReadAll(resp.Body)
if !assert.Nil(t, err) {
return
}

var status humuspb.Status
err = proto.Unmarshal(b, &status)
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_UNAVAILABLE, status.Code) {
return
}
if !assert.NotEmpty(t, status.Message) {
return
}
if !assert.Empty(t, status.Details) {
return
}
})
})
}

0 comments on commit fffcae6

Please sign in to comment.