fix(http): fix /ready response content type

pull/19908/head
Pavel Zavora 2020-11-05 18:41:36 +01:00
parent 89773ba30b
commit 7ca821a9ad
2 changed files with 41 additions and 0 deletions

View File

@ -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 {

40
http/ready_test.go Normal file
View File

@ -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")
}
}