forked from cloudfoundry/go-uaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_test.go
72 lines (63 loc) · 1.66 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
63
64
65
66
67
68
69
70
71
72
package uaa_test
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
uaa "github.com/cloudfoundry-community/go-uaa"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
func TestIsHealthy(t *testing.T) {
spec.Run(t, "IsHealthy", testIsHealthy, spec.Report(report.Terminal{}))
}
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)
}))
c := &http.Client{Transport: http.DefaultTransport}
u, _ := url.Parse(s.URL)
a = &uaa.API{
TargetURL: u,
AuthenticatedClient: c,
UnauthenticatedClient: c,
}
})
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)
w.Write([]byte("ok"))
})
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)
w.Write([]byte("ok"))
})
status, err := a.IsHealthy()
Expect(status).To(BeFalse())
Expect(err).NotTo(HaveOccurred())
})
}