forked from alexliesenfeld/health
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
123 lines (97 loc) · 3.18 KB
/
handler_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package health
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type checkerMock struct {
mock.Mock
}
func (ck *checkerMock) Start() {
ck.Called()
}
func (ck *checkerMock) Stop() {
ck.Called()
}
func (ck *checkerMock) Check(ctx context.Context) CheckerResult {
return ck.Called(ctx).Get(0).(CheckerResult)
}
func (ck *checkerMock) GetRunningPeriodicCheckCount() int {
return ck.Called().Get(0).(int)
}
func (ck *checkerMock) IsStarted() bool {
return ck.Called().Get(0).(bool)
}
type resultWriterMock struct {
mock.Mock
}
func (ck *resultWriterMock) Write(result *CheckerResult, statusCode int, w http.ResponseWriter, r *http.Request) error {
return ck.Called(result, statusCode, w, r).Get(0).(error)
}
func doTestHandler(t *testing.T, statusCodeUp, statusCodeDown int, expectedStatus CheckerResult, expectedStatusCode int) {
// Arrange
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "https://localhost/foo", nil)
ckr := checkerMock{}
ckr.On("IsStarted").Return(false)
ckr.On("Start")
ckr.On("Check", mock.Anything).Return(expectedStatus)
handler := NewHandler(&ckr, WithStatusCodeUp(statusCodeUp), WithStatusCodeDown(statusCodeDown))
// Act
handler.ServeHTTP(response, request)
// Assert
ckr.Mock.AssertNumberOfCalls(t, "Check", 1)
assert.Equal(t, response.Header().Get("content-type"), "application/json; charset=utf-8")
assert.Equal(t, response.Result().StatusCode, expectedStatusCode)
result := CheckerResult{}
_ = json.Unmarshal(response.Body.Bytes(), &result)
log.Printf("returned %+v, want %+v", result.Details, expectedStatus.Details)
assert.True(t, reflect.DeepEqual(result, expectedStatus))
}
func TestHandlerIfCheckFailThenRespondWithNotAvailable(t *testing.T) {
status := CheckerResult{
Status: StatusUnknown,
Details: map[string]CheckResult{
"check1": {Status: StatusDown, Timestamp: time.Now().UTC(), Error: fmt.Errorf("hello")},
"check2": {Status: StatusUp, Timestamp: time.Now().UTC(), Error: nil},
},
}
doTestHandler(t, http.StatusNoContent, http.StatusTeapot, status, http.StatusTeapot)
}
func TestHandlerIfCheckSucceedsThenRespondWithAvailable(t *testing.T) {
status := CheckerResult{
Status: StatusUp,
Details: map[string]CheckResult{
"check1": {Status: StatusUp, Timestamp: time.Now().UTC(), Error: nil},
},
}
doTestHandler(t, http.StatusNoContent, http.StatusTeapot, status, http.StatusNoContent)
}
func TestHandlerIfAuthFailsThenReturnNoDetails(t *testing.T) {
status := CheckerResult{
Status: StatusDown,
Details: map[string]CheckResult{
"check1": {Status: StatusDown, Timestamp: time.Now().UTC(), Error: fmt.Errorf("an error message")},
},
}
doTestHandler(t, http.StatusNoContent, http.StatusTeapot, status, http.StatusTeapot)
}
func TestWhenChecksEmptyThenHandlerResultContainNoChecksMap(t *testing.T) {
// Arrange
r := httptest.NewRequest(http.MethodGet, "/health", nil)
w := httptest.NewRecorder()
// Act
NewHandler(NewChecker()).ServeHTTP(w, r)
// Assert
if w.Body.String() != "{\"status\":\"up\"}" {
t.Errorf("response does not contain the expected result")
}
}