From 368f2ab67e54f87b0103d2ab69df96f2a3097210 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sat, 30 Apr 2016 21:56:35 -0700 Subject: [PATCH] Fixed fasthttp Request#SetBody Signed-off-by: Vishal Rana --- engine/fasthttp/request.go | 5 ++--- glide.lock | 3 ++- middleware/body_limit_test.go | 12 +++++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/engine/fasthttp/request.go b/engine/fasthttp/request.go index 892958ca..67a5143b 100644 --- a/engine/fasthttp/request.go +++ b/engine/fasthttp/request.go @@ -7,7 +7,6 @@ import ( "io" "mime/multipart" - "github.com/labstack/echo" "github.com/labstack/echo/engine" "github.com/labstack/gommon/log" "github.com/valyala/fasthttp" @@ -95,12 +94,12 @@ func (r *Request) SetURI(uri string) { // Body implements `engine.Request#Body` function. func (r *Request) Body() io.Reader { - return bytes.NewBuffer(r.PostBody()) + return bytes.NewBuffer(r.Request.Body()) } // SetBody implements `engine.Request#SetBody` function. func (r *Request) SetBody(reader io.Reader) { - r.SetBodyStream(reader, r.header.Get(echo.HeaderContentType)) + r.Request.SetBodyStream(reader, 0) } // FormValue implements `engine.Request#FormValue` function. diff --git a/glide.lock b/glide.lock index 6e6f035e..935e3146 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: 21820434709470e49c64df0f854d3352088ca664d193e29bc6cd434518c27a7c -updated: 2016-04-30T19:53:13.742557404-07:00 +updated: 2016-04-30T20:20:55.32592199-07:00 imports: - name: github.com/dgrijalva/jwt-go version: a2c85815a77d0f951e33ba4db5ae93629a1530af @@ -18,6 +18,7 @@ imports: subpackages: - color - log + - bytes - name: github.com/mattn/go-colorable version: bc69d6cebca8dd5675346a34a1c545b67dc74d5e - name: github.com/mattn/go-isatty diff --git a/middleware/body_limit_test.go b/middleware/body_limit_test.go index 0538735a..c8646420 100644 --- a/middleware/body_limit_test.go +++ b/middleware/body_limit_test.go @@ -18,7 +18,10 @@ func TestBodyLimit(t *testing.T) { rec := test.NewResponseRecorder() c := e.NewContext(req, rec) h := func(c echo.Context) error { - body, _ := ioutil.ReadAll(c.Request().Body()) + body, err := ioutil.ReadAll(c.Request().Body()) + if err != nil { + return err + } return c.String(http.StatusOK, string(body)) } @@ -28,6 +31,9 @@ func TestBodyLimit(t *testing.T) { assert.Equal(t, "Hello, World!", rec.Body.String()) // Overlimit - // BodyLimit("2B")(h)(c) - // assert.Equal(t, "Hello, World!", rec.Body.String()) + req = test.NewRequest(echo.POST, "/", bytes.NewReader([]byte("Hello, World!"))) + rec = test.NewResponseRecorder() + c = e.NewContext(req, rec) + BodyLimit("2B")(h)(c) + assert.Equal(t, http.StatusRequestEntityTooLarge, rec.Status()) }