提交 db547a7c 编写于 作者: E Eyitayo Ogunbiyi

added test for execJson

上级 7c575585
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])
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册