Skip to content

Commit

Permalink
Added handler_test
Browse files Browse the repository at this point in the history
  • Loading branch information
eranharel committed Jan 24, 2019
1 parent d3a580d commit 8403db2
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 3 deletions.
3 changes: 2 additions & 1 deletion checks/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package checks

import (
"testing"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"net/http"
"strings"
"fmt"
"time"

"github.com/stretchr/testify/assert"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ type result struct {
// the time of the last health check
Timestamp time.Time `json:"timestamp"`
// the number of failures that occurred in a row
ContiguousFailures int64 `json:"num_failures"`
ContiguousFailures int64 `json:"contiguousFailures"`
// the time of the initial transitional failure
TimeOfFirstFailure *time.Time `json:"first_failure_time"`
TimeOfFirstFailure *time.Time `json:"timeOfFirstFailure"`
}

func (r *result) IsHealthy() bool {
Expand Down
147 changes: 147 additions & 0 deletions http/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package http

import (
"testing"
"net/http"
"net/http/httptest"
"io/ioutil"
"encoding/json"
"io"
"fmt"
"time"

"github.com/stretchr/testify/assert"
"gitlab.appsflyer.com/Architecture/af-go-health"
"gitlab.appsflyer.com/Architecture/af-go-health/checks"
)

func TestHandleHealthJSON_longFormatNoChecks(t *testing.T) {
h := health.New()
resp := execReq(h, true)
body, _ := ioutil.ReadAll(resp.Body)

assert.Equal(t, http.StatusOK, resp.StatusCode, "status when no checks are registered")
assert.Equal(t, "{}\n", string(body), "body when no checks are registered")
}

func TestHandleHealthJSON_shortFormatNoChecks(t *testing.T) {
h := health.New()
resp := execReq(h, false)
body, _ := ioutil.ReadAll(resp.Body)

assert.Equal(t, http.StatusOK, resp.StatusCode, "status when no checks are registered")
assert.Equal(t, "{}\n", string(body), "body when no checks are registered")
}

func TestHandleHealthJSON_longFormatPassingCheck(t *testing.T) {
h := health.New()

h.RegisterCheck(createCheck("check1", true, 10*time.Millisecond))

resp := execReq(h, true)
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode, "status before first run")

var respMsg = unmarshalLongFormat(resp.Body)
const freshCheckMsg = "didn't run yet"
expectedResponse := response{
Check1: checkResult{
Message: freshCheckMsg,
Error: Err{
Message: freshCheckMsg,
},
ContiguousFailures: 1,
},
}
assert.Equal(t, &expectedResponse, respMsg, "body when no checks are registered")

time.Sleep(11 * time.Millisecond)
resp = execReq(h, true)
assert.Equal(t, http.StatusOK, resp.StatusCode, "status before first run")

respMsg = unmarshalLongFormat(resp.Body)
expectedResponse = response{
Check1: checkResult{
Message: "pass",
ContiguousFailures: 0,
},
}
assert.Equal(t, &expectedResponse, respMsg, "body after first run")
}

func TestHandleHealthJSON_shortFormatPassingCheck(t *testing.T) {
h := health.New()

h.RegisterCheck(createCheck("check1", true, 10*time.Millisecond))

resp := execReq(h, false)
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode, "status before first run")

var respMsg = unmarshalShortFormat(resp.Body)
expectedResponse := map[string]string{"check1": "FAIL"}
assert.Equal(t, expectedResponse, respMsg, "body when no checks are registered")

time.Sleep(11 * time.Millisecond)
resp = execReq(h, false)
assert.Equal(t, http.StatusOK, resp.StatusCode, "status before first run")

respMsg = unmarshalShortFormat(resp.Body)
expectedResponse = map[string]string{"check1": "PASS"}
assert.Equal(t, expectedResponse, respMsg, "body after first run")
}

func unmarshalShortFormat(r io.Reader) map[string]string {
respMsg := make(map[string]string)
json.NewDecoder(r).Decode(&respMsg)
return respMsg
}

func unmarshalLongFormat(r io.Reader) *response {
var respMsg response
json.NewDecoder(r).Decode(&respMsg)
return &respMsg
}

func createCheck(name string, passing bool, delay time.Duration) *health.Config {
return &health.Config{
InitialDelay: delay,
ExecutionPeriod: delay,
Check: &checks.CustomCheck{
CheckName: name,
CheckFunc: func() (details interface{}, err error) {
if passing {
return "pass", nil
}
return "failing", fmt.Errorf("failing")
},
},
}
}

func execReq(h health.Health, longFormat bool) (*http.Response) {
var path = "/meh"
if !longFormat {
path = fmt.Sprintf("%s?type=%s", path, ReportTypeShort)
}

handler := HandleHealthJSON(h)

req := httptest.NewRequest(http.MethodGet, path, nil)
w := httptest.NewRecorder()

handler.ServeHTTP(w, req)
return w.Result()
}

type response struct {
Check1 checkResult `json:"check1"`
}

type checkResult struct {
Message string `json:"message"`
Error Err `json:"error"`
ContiguousFailures int64 `json:"contiguousFailures"`
}

type Err struct {
Message string `json:"message"`
}

0 comments on commit 8403db2

Please sign in to comment.