-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthz_test.go
61 lines (48 loc) · 1.32 KB
/
healthz_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
package healthz
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestAlwaysUp(t *testing.T) {
var (
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/healthz", nil)
)
AlwaysUp(w, r)
if w.Code != http.StatusOK {
t.Errorf("HTTP Status Code expected %d, got %d", http.StatusOK, w.Code)
}
if strings.Index(w.Body.String(), "background-color: green") <= 0 {
t.Error("A green background is expected")
}
}
func TestCheck(t *testing.T) {
t.Run("Up", func(t *testing.T) {
var (
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/healthz", nil)
)
Check(func() bool { return true }).ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("HTTP Status Code expected %d, got %d", http.StatusOK, w.Code)
}
if strings.Index(w.Body.String(), "background-color: green") <= 0 {
t.Error("A green background is expected")
}
})
t.Run("Down", func(t *testing.T) {
var (
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/healthz", nil)
)
Check(func() bool { return false }).ServeHTTP(w, r)
if w.Code != http.StatusServiceUnavailable {
t.Errorf("HTTP Status Code expected %d, got %d", http.StatusServiceUnavailable, w.Code)
}
if strings.Index(w.Body.String(), "background-color: red") <= 0 {
t.Error("A red background is expected")
}
})
}