Skip to content

Commit

Permalink
fix http response header
Browse files Browse the repository at this point in the history
Signed-off-by: logeable <logeable@gmail.com>
  • Loading branch information
logeable committed Apr 6, 2021
1 parent 33e8e96 commit efe7ddb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/healthcheck/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/healthcheck/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package healthcheck_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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"))
}

0 comments on commit efe7ddb

Please sign in to comment.