From 0d6afec17e038903c3bdd499da2853f84b891771 Mon Sep 17 00:00:00 2001 From: logeable Date: Tue, 6 Apr 2021 18:17:43 +0800 Subject: [PATCH] fix http response header (#2924) Signed-off-by: logeable --- pkg/healthcheck/handler.go | 3 ++- pkg/healthcheck/handler_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/healthcheck/handler.go b/pkg/healthcheck/handler.go index 38876e08b86..1a714ea70c6 100644 --- a/pkg/healthcheck/handler.go +++ b/pkg/healthcheck/handler.go @@ -98,9 +98,10 @@ func (hc *HealthCheck) Handler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { state := hc.getState() template := hc.responses[state.status] - w.WriteHeader(template.statusCode) w.Header().Set("Content-Type", "application/json") + w.WriteHeader(template.statusCode) + w.Write(hc.createRespBody(state, template)) }) } diff --git a/pkg/healthcheck/handler_test.go b/pkg/healthcheck/handler_test.go index e1c13ebbda0..ab64e688a72 100644 --- a/pkg/healthcheck/handler_test.go +++ b/pkg/healthcheck/handler_test.go @@ -16,6 +16,8 @@ package healthcheck_test import ( + "net/http" + "net/http/httptest" "testing" "github.com/stretchr/testify/assert" @@ -49,3 +51,11 @@ func TestStatusSetGet(t *testing.T) { assert.Equal(t, Ready, hc.Get()) assert.Equal(t, map[string]string{"level": "info", "msg": "Health Check state change", "status": "ready"}, logBuf.JSONLine(0)) } + +func TestHealthCheck_Handler_ContentType(t *testing.T) { + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil)) + resp := rec.Result() + + assert.Equal(t, "application/json", resp.Header.Get("Content-Type")) +}