add a test case for engine.IndentJsonIndentSpaceNum

This commit is contained in:
bestgopher 2020-05-01 16:27:22 +08:00
parent 4ff7862895
commit 137460f67b

View File

@ -6,13 +6,16 @@ package gin
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json"
"fmt" "fmt"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"math/rand"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
@ -544,3 +547,35 @@ func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo)
func handlerTest1(c *Context) {} func handlerTest1(c *Context) {}
func handlerTest2(c *Context) {} func handlerTest2(c *Context) {}
// Engine.IndentJsonIndentSpaceNum
func TestEngine_IndentJsonIndentSpaceNum(t *testing.T) {
rand.Seed(time.Now().UnixNano())
spaceNum := rand.Intn(10)
router := Default()
defaultResponse := H{"name": "test", "age": 20 }
router.IndentJsonIndentSpaceNum(spaceNum)
router.GET("/test", func(c *Context) {
c.IndentedJSON(200, defaultResponse)
})
s := httptest.NewServer(router)
defer s.Close()
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatal("error code: ", rr.Code)
}
except, _ := json.MarshalIndent(defaultResponse, "", strings.Repeat(spaceString, spaceNum))
assert.Equal(t, rr.Body.Bytes(), except)
}