-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhealth_test.go
60 lines (47 loc) · 1.18 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
package systatus
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHealthEndpointDefaultHandler(t *testing.T) {
mux := http.NewServeMux()
opts := SystatusOptions{
Prefix: "",
Mux: mux,
}
Enable(opts)
ts := httptest.NewServer(mux)
defer ts.Close()
res, _ := http.Get(fmt.Sprintf("%s/health", ts.URL))
assert.Equal(t, 200, res.StatusCode)
var h HealthResponse
err := json.NewDecoder(res.Body).Decode(&h)
assert.NoError(t, err)
assert.Equal(t, "HEALTHY", h.Status)
}
func TestHealthEndpointCustomHandler(t *testing.T) {
mux := http.NewServeMux()
opts := SystatusOptions{
Prefix: "",
Mux: mux,
HealthHandlerOpts: HealthHandlerOpts{
Healthcheck: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
json.NewEncoder(w).Encode(HealthResponse{Status: "HEALTHY-CUSTOM"})
},
},
}
Enable(opts)
ts := httptest.NewServer(mux)
defer ts.Close()
res, _ := http.Get(fmt.Sprintf("%s/health", ts.URL))
assert.Equal(t, 200, res.StatusCode)
var h HealthResponse
err := json.NewDecoder(res.Body).Decode(&h)
assert.NoError(t, err)
assert.Equal(t, "HEALTHY-CUSTOM", h.Status)
}