mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
test wrong content-Length panic
This commit is contained in:
parent
cb61c8ce15
commit
6404d8d7b5
33
gin_test.go
33
gin_test.go
@ -14,6 +14,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@ -483,3 +484,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) {}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user