From 137460f67b3e0e4907819ecadc3f02e5e6ed2dda Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Fri, 1 May 2020 16:27:22 +0800 Subject: [PATCH] add a test case for engine.IndentJsonIndentSpaceNum --- gin_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/gin_test.go b/gin_test.go index 11bdd79c..ccda45e5 100644 --- a/gin_test.go +++ b/gin_test.go @@ -6,13 +6,16 @@ package gin import ( "crypto/tls" + "encoding/json" "fmt" "html/template" "io/ioutil" + "math/rand" "net/http" "net/http/httptest" "reflect" "strconv" + "strings" "sync/atomic" "testing" "time" @@ -544,3 +547,35 @@ func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) func handlerTest1(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) + +} \ No newline at end of file