From ca0c64b69e3357ff8ce94a0e52f445bdf471d4b5 Mon Sep 17 00:00:00 2001 From: Eyitayo Ogunbiyi Date: Tue, 7 Jul 2020 15:21:38 +0100 Subject: [PATCH] refactored tests for health check endpoint --- admin_test.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/admin_test.go b/admin_test.go index 1bf8700a..4a614721 100644 --- a/admin_test.go +++ b/admin_test.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "reflect" "strings" "testing" @@ -111,7 +112,7 @@ func TestWriteJSON(t *testing.T) { err := json.NewDecoder(w.Body).Decode(&decodedBody) 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 { @@ -168,9 +169,31 @@ func TestHealthCheckHandlerReturnsJSON(t *testing.T) { status, http.StatusOK) } - expectedResponseBody := `[{"message":"database","name":"success","status":"OK"},{"message":"cache","name":"error","status":"no cache detected"}]` - if w.Body.String() != expectedResponseBody { + decodedResponseBody := []map[string]interface{}{} + 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", - w.Body.String(), expectedResponseBody) + decodedResponseBody, expectedResponseBody) } + }