From db547a7c84aa7957b65b84d33f92d79893d3c7ae Mon Sep 17 00:00:00 2001 From: Eyitayo Ogunbiyi Date: Mon, 6 Jul 2020 16:04:29 +0100 Subject: [PATCH] added test for execJson --- admin_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/admin_test.go b/admin_test.go index 71cc209e..b7a9af9c 100644 --- a/admin_test.go +++ b/admin_test.go @@ -1,7 +1,9 @@ package beego import ( + "encoding/json" "fmt" + "net/http/httptest" "testing" ) @@ -75,3 +77,27 @@ func oldMap() M { m["BConfig.Log.Outputs"] = BConfig.Log.Outputs return m } + +func TestExecJSON(t *testing.T) { + t.Log("Testing the adding of JSON to the response") + + w := httptest.NewRecorder() + originalBody := []int{1, 2, 3} + + res, _ := json.Marshal(originalBody) + + execJSON(w, res) + + decodedBody := []int{} + err := json.NewDecoder(w.Body).Decode(&decodedBody) + + if err != nil { + t.Fatal("Should be able to decode response body into decodedBody slice") + } + + for i := range decodedBody { + if decodedBody[i] != originalBody[i] { + t.Fatalf("Expected %d but got %d in decoded body slice", originalBody[i], decodedBody[i]) + } + } +}