-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhealth_test.go
62 lines (54 loc) · 1.46 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
61
62
package uaa_test
import (
"net/http"
"net/http/httptest"
"testing"
uaa "github.com/cloudfoundry-community/go-uaa"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
)
func testIsHealthy(t *testing.T, when spec.G, it spec.S) {
var (
s *httptest.Server
handler http.Handler
called int
a *uaa.API
)
it.Before(func() {
RegisterTestingT(t)
called = 0
s = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
called = called + 1
Expect(handler).NotTo(BeNil())
handler.ServeHTTP(w, req)
}))
a, _ = uaa.New(s.URL, uaa.WithNoAuthentication())
})
it.After(func() {
if s != nil {
s.Close()
}
})
it("is healthy when a 200 response is received", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.URL.Path).To(Equal("/healthz"))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("ok"))
Expect(err).NotTo(HaveOccurred())
})
status, err := a.IsHealthy()
Expect(status).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
})
it("is unhealthy when a non-200 response is received", func() {
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
Expect(req.URL.Path).To(Equal("/healthz"))
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte("ok"))
Expect(err).NotTo(HaveOccurred())
})
status, err := a.IsHealthy()
Expect(status).To(BeFalse())
Expect(err).NotTo(HaveOccurred())
})
}