fix(http): fix /ready response content type
parent
89773ba30b
commit
7ca821a9ad
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue