-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthcheck_test.go
49 lines (47 loc) · 996 Bytes
/
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
package mux
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestHealthCheck(t *testing.T) {
tests := []struct {
checks HealthCheck
status int
}{
{
HealthCheck(nil),
http.StatusOK,
},
{
HealthCheck([]HealthChecker{}),
http.StatusOK,
},
{
HealthCheck([]HealthChecker{HealthCheckerFunc(func() error { return nil })}),
http.StatusOK,
},
{
HealthCheck([]HealthChecker{HealthCheckerFunc(func() error { return errors.New("test") })}),
http.StatusInternalServerError,
},
}
for _, tt := range tests {
h := New()
h.Handle("/", tt.checks, WithMethod(http.MethodGet))
server := httptest.NewServer(h)
defer server.Close()
client := server.Client()
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp.Body.Close()
assertStatus(t, resp, tt.status)
}
}