-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhealth_test.go
45 lines (37 loc) · 980 Bytes
/
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
package rpc_test
import (
"errors"
"net/http/httptest"
"testing"
"github.com/tj/assert"
"github.com/apex/rpc"
)
// healthChecker implementation.
type healthChecker struct {
err error
}
// Health implementation.
func (h healthChecker) Health() error {
return h.err
}
// Test health checks.
func TestWriteHealth(t *testing.T) {
t.Run("without a HealthChecker", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteHealth(w, nil)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "OK\n", w.Body.String())
})
t.Run("with a HealthChecker passing", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteHealth(w, healthChecker{})
assert.Equal(t, 200, w.Code)
assert.Equal(t, "OK\n", w.Body.String())
})
t.Run("with a HealthChecker failing", func(t *testing.T) {
w := httptest.NewRecorder()
rpc.WriteHealth(w, healthChecker{errors.New("boom")})
assert.Equal(t, 500, w.Code)
assert.Equal(t, "Health check failed\n", w.Body.String())
})
}