From 2df1160061f9bcb421bc689262a3a34781d4aed7 Mon Sep 17 00:00:00 2001 From: Jake Skelcy Date: Tue, 28 Nov 2017 15:05:32 -0500 Subject: [PATCH] clean up health check tests --- health/service.go | 7 +------ health/service_test.go | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/health/service.go b/health/service.go index 51ebd35..92b1d43 100644 --- a/health/service.go +++ b/health/service.go @@ -72,15 +72,10 @@ func hostName() string { func m3ctlHealthCheck(w http.ResponseWriter, r *http.Request) { start := time.Now() host := hostName() - h := healthCheckResult{Host: host, Timestamp: start} status := healthCheck() + h := healthCheckResult{Host: host, Timestamp: start, Status: status} h.ResponseTime = time.Since(start) - h.Status = status - if status != ok { - w.WriteHeader(http.StatusInternalServerError) - } - body, err := json.Marshal(h) if err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/health/service_test.go b/health/service_test.go index 0339e71..22c18e5 100644 --- a/health/service_test.go +++ b/health/service_test.go @@ -27,6 +27,8 @@ import ( "os" "testing" + "github.com/gorilla/mux" + "github.com/m3db/m3x/instrument" "github.com/stretchr/testify/require" ) @@ -42,18 +44,18 @@ func TestHealthCheck(t *testing.T) { rr := httptest.NewRecorder() // Create a request to pass to our handler. We don't have any query parameters for now, so we'll // pass 'nil' as the third parameter. - req := http.Request{Method: "GET", RequestURI: "/health"} + req, err := http.NewRequest("GET", "/health", nil) + require.NoError(t, err) - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - m3ctlHealthCheck(w, r) - }) + opts := instrument.NewOptions() + service := NewService(opts) + mux := mux.NewRouter().PathPrefix(service.URLPrefix()).Subrouter() + service.RegisterHandlers(mux) - // Our handlers satisfy http.Handler, so we can call their ServeHTTP method - // directly and pass in our Request and ResponseRecorder. - handler.ServeHTTP(rr, &req) + mux.ServeHTTP(rr, req) rawResult := make([]byte, rr.Body.Len()) - _, err := rr.Body.Read(rawResult) + _, err = rr.Body.Read(rawResult) require.NoError(t, err, "Encountered error parsing response") var actualResult healthCheckResult