-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_test.go
107 lines (86 loc) · 2.82 KB
/
health_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package health
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/dhawal55/health/mocks"
"github.com/stretchr/testify/assert"
)
func empty() []string {
return []string{}
}
func getServer(service *healthService) *httptest.Server {
mux := service.registerRoute()
return httptest.NewServer(mux)
}
func Test_Get(t *testing.T) {
checker := mocks.HealthChecker{}
messages := []string{"foo: bar"}
checker.Mock.On("IsHealthy").Return(true, nil, messages)
version := "1.0"
checksum := "checksum"
server := getServer(&healthService{
healthCheckers: []HealthChecker{&checker},
version: version,
checksum: checksum,
})
res, _ := http.Get(server.URL + "/health")
body, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
expected := healthCheckResponse{
OverallHealth: "Healthy",
Version: version,
Checksum: checksum,
Items: []healthCheckItem{healthCheckItem{Name: "*mocks.HealthChecker", Status: "Healthy", Error: "", Messages: messages}},
}
var actual healthCheckResponse
json.Unmarshal(body, &actual)
assert.Equal(t, expected.OverallHealth, "Healthy")
assert.Equal(t, expected.Version, version)
assert.Equal(t, expected.Items, actual.Items)
}
func Test_Get_WithError(t *testing.T) {
checker := mocks.HealthChecker{}
errMessage := "I failed"
checker.Mock.On("IsHealthy").Return(false, errors.New(errMessage), nil)
server := getServer(&healthService{healthCheckers: []HealthChecker{&checker}})
res, _ := http.Get(server.URL + "/health")
body, _ := ioutil.ReadAll(res.Body)
defer res.Body.Close()
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
expected := healthCheckResponse{
OverallHealth: "Unhealthy",
Items: []healthCheckItem{healthCheckItem{Name: "*mocks.HealthChecker", Status: "Unhealthy", Error: errMessage, Messages: nil}},
}
var actual healthCheckResponse
json.Unmarshal(body, &actual)
assert.Equal(t, expected.OverallHealth, "Unhealthy")
assert.Equal(t, expected.Items, actual.Items)
}
func Test_Get_Cors(t *testing.T) {
server := getServer(&healthService{})
defer server.Close()
client := &http.Client{}
req, _ := http.NewRequest("OPTIONS", server.URL+"/health", nil)
req.Header.Add("Origin", "bar.com")
res, err := client.Do(req)
if err != nil {
assert.Fail(t, "No error expected. Got error: %s", err.Error())
}
assertHeaders(t, res.Header, map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
})
}
func assertHeaders(t *testing.T, resHeaders http.Header, reqHeaders map[string]string) {
for name, value := range reqHeaders {
if actual := strings.Join(resHeaders[name], ", "); actual != value {
t.Errorf("Invalid header `%s', wanted `%s', got `%s'", name, value, actual)
}
}
}