forked from kuskoman/logstash-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck_test.go
70 lines (57 loc) · 1.89 KB
/
healthcheck_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
package server
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestHealthCheck(t *testing.T) {
runTest := func(mockStatus int, expectedStatus int) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(mockStatus)
}))
defer mockServer.Close()
handler := getHealthCheck(mockServer.URL)
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("Error creating request: %v", err)
}
rr := httptest.NewRecorder()
handler(rr, req)
if status := rr.Code; status != expectedStatus {
t.Errorf("Handler returned wrong status code: got %v want %v", status, expectedStatus)
}
}
t.Run("500 status", func(t *testing.T) {
runTest(http.StatusInternalServerError, http.StatusInternalServerError)
})
t.Run("200 status", func(t *testing.T) {
runTest(http.StatusOK, http.StatusOK)
})
t.Run("404 status", func(t *testing.T) {
runTest(http.StatusNotFound, http.StatusInternalServerError)
})
t.Run("no response", func(t *testing.T) {
handler := getHealthCheck("http://localhost:12345")
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("Error creating request: %v", err)
}
rr := httptest.NewRecorder()
handler(rr, req)
if status := rr.Code; status != http.StatusInternalServerError {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError)
}
})
t.Run("invalid url", func(t *testing.T) {
handler := getHealthCheck("http://localhost:96010:invalidurl")
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatalf("Error creating request: %v", err)
}
rr := httptest.NewRecorder()
handler(rr, req)
if status := rr.Code; status != http.StatusInternalServerError {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError)
}
})
}