forked from dimiro1/health
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
80 lines (60 loc) · 1.77 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
package health
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func Test_NewHandler(t *testing.T) {
// How can I test a function that returns a Struct ?
// A better Idea? Please tell me!
h := NewHandler()
handler := &h
if handler == nil {
t.Error("&NewHandler() == nil, wants !nil")
}
}
func Test_Handler_ServeHTTP_Down(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h := Handler{}
h.AddChecker("DownChecker", downTestChecker{})
h.ServeHTTP(w, r)
jsonbytes, _ := ioutil.ReadAll(w.Body)
jsonstring := strings.TrimSpace(string(jsonbytes))
wants := `{"DownChecker":{"status":"DOWN"},"status":"DOWN"}`
if jsonstring != wants {
t.Errorf("jsonReturned == %s, wants %s", jsonstring, wants)
}
contentType := w.Header().Get("Content-Type")
wants = "application/json"
if contentType != wants {
t.Errorf("type == %s, wants %s", contentType, wants)
}
if w.Code != http.StatusServiceUnavailable {
t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusServiceUnavailable)
}
}
func Test_Handler_ServeHTTP_Up(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h := Handler{}
h.AddChecker("UpChecker", upTestChecker{})
h.AddInfo("custom", "info")
h.ServeHTTP(w, r)
jsonbytes, _ := ioutil.ReadAll(w.Body)
jsonstring := strings.TrimSpace(string(jsonbytes))
wants := `{"UpChecker":{"status":"UP"},"custom":"info","status":"UP"}`
if jsonstring != wants {
t.Errorf("jsonstring == %s, wants %s", jsonstring, wants)
}
contentType := w.Header().Get("Content-Type")
wants = "application/json"
if contentType != wants {
t.Errorf("type == %s, wants %s", contentType, wants)
}
if w.Code != http.StatusOK {
t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusOK)
}
}