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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dimiro1/health

go 1.11
go 1.13

require (
github.com/DATA-DOG/go-sqlmock v1.3.3
Expand Down
53 changes: 37 additions & 16 deletions url/checker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package url

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"time"

Expand All @@ -9,13 +12,16 @@ import (

// Checker is a checker that check a given URL
type Checker struct {
URL string
Timeout time.Duration
URL string
Timeout time.Duration
Method string // Defaults to GET
ExpectBodyContains string // Disabled if empty
ExpectStatusCode int // if 0 defaults to 200
}

// NewChecker returns a new url.Checker with the given URL
func NewChecker(url string) Checker {
return Checker{URL: url, Timeout: 5 * time.Second}
return NewCheckerWithTimeout(url, 5*time.Second)
}

// NewCheckerWithTimeout returns a new url.Checker with the given URL and given timeout
Expand All @@ -27,31 +33,46 @@ func NewCheckerWithTimeout(url string, timeout time.Duration) Checker {
// If the request returns something different than StatusOK,
// The status is set to StatusBadRequest and the URL is considered Down
func (u Checker) Check() health.Health {
client := http.Client{
Timeout: u.Timeout,
}

ctxWithTimeout, cancelFn := context.WithTimeout(context.Background(), u.Timeout)
defer cancelFn()
health := health.NewHealth()
body := new(bytes.Buffer)
req, err := http.NewRequestWithContext(ctxWithTimeout, u.Method, u.URL, body)
if err != nil {
health.Down().AddInfo("code", http.StatusBadRequest).AddInfo("error", err.Error())
return health
}

resp, err := client.Head(u.URL)

resp, err := http.DefaultClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}

if err != nil {
health.Down().AddInfo("code", http.StatusBadRequest)

health.Down().AddInfo("code", http.StatusBadRequest).AddInfo("error", err.Error())
return health
}

if resp.StatusCode == http.StatusOK {
health.Up()
if u.ExpectStatusCode == 0 {
u.ExpectStatusCode = http.StatusOK
}

if resp.StatusCode == u.ExpectStatusCode {
health.Up().AddInfo("code", resp.StatusCode)
} else {
health.Down()
health.Down().AddInfo("code", resp.StatusCode).AddInfo("expectedCode", u.ExpectStatusCode)
return health
}

health.AddInfo("code", resp.StatusCode)
if u.ExpectBodyContains != "" {
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
health.Down().AddInfo("error", err.Error())
return health
}
if !bytes.ContainsAny(respbody, u.ExpectBodyContains) {
health.Down().AddInfo("body", "does not contain: "+u.ExpectBodyContains)
}
}

return health
}
50 changes: 48 additions & 2 deletions url/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,52 @@ func Test_Checker_Check_Up(t *testing.T) {
check(t, resp, wants, http.StatusOK)
}

func Test_Checker_Check_Up_BodyCheck(t *testing.T) {
mux := http.NewServeMux()

server := httptest.NewServer(mux)

checker := NewChecker(fmt.Sprintf("%s/up/", server.URL))
checker.ExpectBodyContains = "pong"

handler := health.NewHandler()
handler.AddChecker("Up", checker)

mux.Handle("/health/", handler)
mux.HandleFunc("/up/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "pong")
})

resp, _ := http.Get(fmt.Sprintf("%s/health/", server.URL))

wants := `{"Up":{"code":200,"status":"UP"},"status":"UP"}`

check(t, resp, wants, http.StatusOK)
}

func Test_Checker_Check_Down_BodyCheck(t *testing.T) {
mux := http.NewServeMux()

server := httptest.NewServer(mux)

checker := NewChecker(fmt.Sprintf("%s/up/", server.URL))
checker.ExpectBodyContains = "pong"

handler := health.NewHandler()
handler.AddChecker("Down", checker)

mux.Handle("/health/", handler)
mux.HandleFunc("/up/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "fail")
})

resp, _ := http.Get(fmt.Sprintf("%s/health/", server.URL))

wants := `{"Down":{"body":"does not contain: pong","code":200,"status":"DOWN"},"status":"DOWN"}`

check(t, resp, wants, http.StatusServiceUnavailable)
}

func Test_Checker_Check_Down(t *testing.T) {
mux := http.NewServeMux()

Expand All @@ -67,7 +113,7 @@ func Test_Checker_Check_Down(t *testing.T) {

resp, _ := http.Get(fmt.Sprintf("%s/health/", server.URL))

wants := `{"Down":{"code":500,"status":"DOWN"},"status":"DOWN"}`
wants := `{"Down":{"code":500,"expectedCode":200,"status":"DOWN"},"status":"DOWN"}`

check(t, resp, wants, http.StatusServiceUnavailable)
}
Expand All @@ -86,7 +132,7 @@ func Test_Checker_Check_Down_invalid(t *testing.T) {

resp, _ := http.Get(fmt.Sprintf("%s/health/", server.URL))

wants := `{"Down":{"code":400,"status":"DOWN"},"status":"DOWN"}`
wants := `{"Down":{"code":400,"error":"Get : unsupported protocol scheme \"\"","status":"DOWN"},"status":"DOWN"}`
check(t, resp, wants, http.StatusServiceUnavailable)
}

Expand Down