Skip to content

Commit

Permalink
health check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohce committed Jan 27, 2025
1 parent 1ffb7f3 commit f457220
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions healthcheck/health_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package healthcheck

import (
"encoding/json"
"errors"
"net/http/httptest"
"testing"
)
Expand Down Expand Up @@ -44,3 +46,31 @@ func TestHealthWithoutDependencies(t *testing.T) {
handler.ServeHTTP(rr, rq)

}

type errService struct{}

func (s *errService) Ping() error {
return errors.New("bad service")
}

func TestHealthNotOK(t *testing.T) {

handler := HealthCheck(&errService{}, &mockDB{}, &mockCache{}, &mockSomeService{})

rq := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()

handler.ServeHTTP(rr, rq)

response := struct {
Status string `json:"status"`
Errors []string `json:"errors"`
}{}

json.NewDecoder(rr.Result().Body).Decode(&response)

if response.Status != "error" {
t.Errorf("status: got %s want error", response.Status)
}

}

0 comments on commit f457220

Please sign in to comment.