From 6404d8d7b5d371a69b08e71eed625d4845a085f5 Mon Sep 17 00:00:00 2001 From: lpxxn Date: Mon, 26 Nov 2018 11:14:21 +0800 Subject: [PATCH] test wrong content-Length panic --- gin_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/gin_test.go b/gin_test.go index 353c9be1..c219e314 100644 --- a/gin_test.go +++ b/gin_test.go @@ -14,6 +14,7 @@ import ( "reflect" "testing" "time" + "sync" "github.com/stretchr/testify/assert" ) @@ -483,3 +484,35 @@ func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) func handlerTest1(c *Context) {} func handlerTest2(c *Context) {} + +func TestContext_JSONErrorContentLength(t *testing.T) { + router := New() + wg := sync.WaitGroup{} + wg.Add(1) + var globalError HandlerFunc = func(c *Context) { + defer func() { + if err := recover(); err != nil { + // catch c.JSON panic + assert.Equal(t, err, http.ErrContentLength) + } + wg.Done() + }() + c.Next() + } + router.Use(globalError) + router.GET("/testJson", func(c *Context) { + data := map[string]string{"name": "world"} + // write wrong content length number + // c.JSON will panic + c.Writer.Header().Set("Content-Length", "1") + c.JSON(http.StatusOK, data) + }) + ts := httptest.NewServer(router) + defer ts.Close() + + _, err := http.Get(fmt.Sprintf("%s/testJson", ts.URL)) + if err != nil { + fmt.Println(err) + } + wg.Wait() +}