added test for execJson

This commit is contained in:
Eyitayo Ogunbiyi 2020-07-06 16:04:29 +01:00
parent 7c575585e9
commit db547a7c84
1 changed files with 26 additions and 0 deletions

View File

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