-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteward_test.go
97 lines (80 loc) · 2.53 KB
/
steward_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package steward
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/franela/goblin"
"github.com/gorilla/mux"
. "github.com/onsi/gomega"
)
func TestStewardEndpoint(t *testing.T) {
g := goblin.Goblin(t)
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })
g.Describe("Status Endpoint", func() {
var r *mux.Router
var server *httptest.Server
var wasHit bool
var client http.Client
g.BeforeEach(func() {
r = mux.NewRouter()
wasHit = false
r.Path(endpoint).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
wasHit = true
HandleStatus(w, req)
})
server = httptest.NewServer(r)
client = http.Client{}
})
g.AfterEach(func() {
server.Close()
})
g.It("should return a StatusOK for a healthy state", func() {
req, err := http.NewRequest("GET", server.URL+endpoint, nil)
Expect(err).NotTo(HaveOccurred())
res, err := client.Do(req)
Expect(err).NotTo(HaveOccurred())
defer res.Body.Close()
Expect(res.StatusCode).To(Equal(http.StatusOK))
Expect(wasHit).To(BeTrue())
bodyBytes, err := ioutil.ReadAll(res.Body)
Expect(err).NotTo(HaveOccurred())
Expect(bodyBytes).To(ContainSubstring("null"))
})
g.It("should return a custom status response payload", func() {
SetStatusResponse(&Response{Application: "Foo", Version: "1.0.0", BuildTime: "now"})
req, err := http.NewRequest("GET", server.URL+endpoint, nil)
Expect(err).NotTo(HaveOccurred())
res, err := client.Do(req)
Expect(err).NotTo(HaveOccurred())
defer res.Body.Close()
Expect(res.StatusCode).To(Equal(http.StatusOK))
Expect(wasHit).To(BeTrue())
bodyBytes, err := ioutil.ReadAll(res.Body)
Expect(err).NotTo(HaveOccurred())
Expect(bodyBytes).To(ContainSubstring(`"app": "Foo",`))
Expect(bodyBytes).To(ContainSubstring(`"version": "1.0.0",`))
Expect(bodyBytes).To(ContainSubstring(`"buildTime": "now"`))
})
g.It("should return an internal error for a bad payload", func() {
bad := func() {}
SetStatusResponse(bad)
req, err := http.NewRequest("GET", server.URL+endpoint, nil)
Expect(err).NotTo(HaveOccurred())
res, err := client.Do(req)
Expect(err).NotTo(HaveOccurred())
defer res.Body.Close()
Expect(res.StatusCode).To(Equal(http.StatusInternalServerError))
})
g.It("should set a custom status endpoint", func() {
e := "/foo/bar"
SetStatusEndpoint(e)
Expect(endpoint).To(Equal(e))
})
})
}
type Response struct {
Application string `json:"app"`
Version string `json:"version"`
BuildTime string `json:"buildTime"`
}