diff --git a/http/ready.go b/http/ready.go index c0f98ced57..a70c32404e 100644 --- a/http/ready.go +++ b/http/ready.go @@ -13,6 +13,7 @@ import ( func ReadyHandler() http.Handler { up := time.Now() fn := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) var status = struct { diff --git a/http/ready_test.go b/http/ready_test.go new file mode 100644 index 0000000000..948d27c5c2 --- /dev/null +++ b/http/ready_test.go @@ -0,0 +1,40 @@ +package http + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestReadyHandler(t *testing.T) { + w := httptest.NewRecorder() + r := httptest.NewRequest(http.MethodGet, "/health", nil) + ReadyHandler().ServeHTTP(w, r) + res := w.Result() + contentType := res.Header.Get("Content-Type") + body, _ := ioutil.ReadAll(res.Body) + + if res.StatusCode != 200 { + t.Errorf("TestReadyHandler. ReadyHandler() StatusCode = %v, want 200", res.StatusCode) + } + if !strings.HasPrefix(contentType, "application/json") { + t.Errorf("TestReadyHandler. ReadyHandler() Content-Type = %v, want application/json", contentType) + } + var content map[string]interface{} + if err := json.Unmarshal(body, &content); err != nil { + t.Errorf("TestReadyHandler. ReadyHandler() error unmarshaling json body %v", err) + return + } + if val := content["status"]; val != "ready" { + t.Errorf("TestReadyHandler. ReadyHandler() .status = %v, want 'ready'", val) + } + if val := content["started"]; val == nil { + t.Errorf("TestReadyHandler. ReadyHandler() .started is not returned") + } + if val := content["up"]; val == nil { + t.Errorf("TestReadyHandler. ReadyHandler() .up is not returned") + } +}