mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 03:30:57 +00:00
added test for execJson
This commit is contained in:
parent
7c575585e9
commit
db547a7c84
@ -1,7 +1,9 @@
|
|||||||
package beego
|
package beego
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -75,3 +77,27 @@ func oldMap() M {
|
|||||||
m["BConfig.Log.Outputs"] = BConfig.Log.Outputs
|
m["BConfig.Log.Outputs"] = BConfig.Log.Outputs
|
||||||
return m
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user