1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 10:00:56 +00:00

refactored tests for health check endpoint

This commit is contained in:
Eyitayo Ogunbiyi 2020-07-07 15:21:38 +01:00
parent 5a4a082af0
commit ca0c64b69e

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect"
"strings" "strings"
"testing" "testing"
@ -111,7 +112,7 @@ func TestWriteJSON(t *testing.T) {
err := json.NewDecoder(w.Body).Decode(&decodedBody) err := json.NewDecoder(w.Body).Decode(&decodedBody)
if err != nil { if err != nil {
t.Fatal("Should be able to decode response body into decodedBody slice") t.Fatal("Could not decode response body into slice.")
} }
for i := range decodedBody { for i := range decodedBody {
@ -168,9 +169,31 @@ func TestHealthCheckHandlerReturnsJSON(t *testing.T) {
status, http.StatusOK) status, http.StatusOK)
} }
expectedResponseBody := `[{"message":"database","name":"success","status":"OK"},{"message":"cache","name":"error","status":"no cache detected"}]` decodedResponseBody := []map[string]interface{}{}
if w.Body.String() != expectedResponseBody { expectedResponseBody := []map[string]interface{}{}
expectedJSONString := []byte(`
[
{
"message":"database",
"name":"success",
"status":"OK"
},
{
"message":"cache",
"name":"error",
"status":"no cache detected"
}
]
`)
json.Unmarshal(expectedJSONString, &expectedResponseBody)
json.Unmarshal(w.Body.Bytes(), &decodedResponseBody)
if !reflect.DeepEqual(decodedResponseBody, expectedResponseBody) {
t.Errorf("handler returned unexpected body: got %v want %v", t.Errorf("handler returned unexpected body: got %v want %v",
w.Body.String(), expectedResponseBody) decodedResponseBody, expectedResponseBody)
} }
} }