-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |