Skip to content
Permalink
Browse files

an not read request body multiple times fixes #1480 (#1486)

  • Loading branch information...
markbates committed Dec 5, 2018
1 parent 39b0970 commit c72131cd2080bb022ea79ca0f9f230f66ac1d267
Showing with 65 additions and 4 deletions.
  1. +2 −2 go.mod
  2. +4 −2 go.sum
  3. +51 −0 request_test.go
  4. +8 −0 server.go
4 go.mod
@@ -19,7 +19,7 @@ require (
github.com/gobuffalo/packd v0.0.0-20181124090624-311c6248e5fb
github.com/gobuffalo/packr v1.21.5
github.com/gobuffalo/packr/v2 v2.0.0-rc.9
github.com/gobuffalo/plush v3.7.30+incompatible
github.com/gobuffalo/plush v3.7.31+incompatible
github.com/gobuffalo/plushgen v0.0.0-20181203163832-9fc4964505c2
github.com/gobuffalo/pop v4.9.3+incompatible
github.com/gobuffalo/tags v2.0.14+incompatible
@@ -46,6 +46,6 @@ require (
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f
golang.org/x/tools v0.0.0-20181203210056-e5f3ab76ea4b
golang.org/x/tools v0.0.0-20181205014116-22934f0fdb62
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc
)
6 go.sum
@@ -201,8 +201,9 @@ github.com/gobuffalo/plush v3.7.20+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5s
github.com/gobuffalo/plush v3.7.21+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI=
github.com/gobuffalo/plush v3.7.22+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI=
github.com/gobuffalo/plush v3.7.23+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI=
github.com/gobuffalo/plush v3.7.30+incompatible h1:CN+rfRcKAn4yL11/R1vl+tbJ8kjcf+1ntf1Sr17yXVE=
github.com/gobuffalo/plush v3.7.30+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI=
github.com/gobuffalo/plush v3.7.31+incompatible h1:Ebkd0uigNk7XdlpZgUrbvgpXGAcAIYL8xgOzDOdA/bk=
github.com/gobuffalo/plush v3.7.31+incompatible/go.mod h1:rQ4zdtUUyZNqULlc6bqd5scsPfLKfT0+TGMChgduDvI=
github.com/gobuffalo/plushgen v0.0.0-20181128164830-d29dcb966cb2/go.mod h1:r9QwptTFnuvSaSRjpSp4S2/4e2D3tJhARYbvEBcKSb4=
github.com/gobuffalo/plushgen v0.0.0-20181203163832-9fc4964505c2 h1:EOI/kgK0bc4F3eqlqaeWTKFLVDbY2chZap54sYz7NGg=
github.com/gobuffalo/plushgen v0.0.0-20181203163832-9fc4964505c2/go.mod h1:opEdT33AA2HdrIwK1aibqnTJDVVKXC02Bar/GT1YRVs=
@@ -479,8 +480,9 @@ golang.org/x/tools v0.0.0-20181122213734-04b5d21e00f1/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20181127195227-b4e97c0ed882/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181127232545-e782529d0ddd/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181201035826-d0ca3933b724/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181203210056-e5f3ab76ea4b h1:GEO1etcO5ITWnhgyBGu/gGrt4WupUfbxvBQH3GewL6A=
golang.org/x/tools v0.0.0-20181203210056-e5f3ab76ea4b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181205014116-22934f0fdb62 h1:KRNg63y5nZTd4po+9IAmrqBNARjHSwNMrkRrJj0AViU=
golang.org/x/tools v0.0.0-20181205014116-22934f0fdb62/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.2.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.3.0 h1:FBSsiFRMz3LBeXIomRnVzrQwSDj4ibvcRexLG0LZGQk=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
@@ -0,0 +1,51 @@
package buffalo

import (
"io/ioutil"
"net/http"
"testing"

"github.com/gobuffalo/httptest"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func Test_Request_MultipleReads(t *testing.T) {
r := require.New(t)
var reads []string

h := http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
b, err := ioutil.ReadAll(req.Body)
r.NoError(err)
reads = append(reads, string(b))
})
app := New(Options{
PreHandlers: []http.Handler{h},
})

app.Use(func(next Handler) Handler {
return func(c Context) error {
b, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
return errors.WithStack(err)
}
reads = append(reads, string(b))
return next(c)
}
})
app.POST("/", func(c Context) error {
b, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
return errors.WithStack(err)
}
reads = append(reads, string(b))
return nil
})

w := httptest.New(app)
w.JSON("/").Post(map[string]string{"foo": "foo"})
r.Len(reads, 3)

foo := `{"foo":"foo"}`
r.Equal([]string{foo, foo, foo}, reads)
}
@@ -9,6 +9,7 @@ import (

"github.com/gobuffalo/buffalo/servers"
"github.com/gobuffalo/events"
"github.com/gobuffalo/packd"
"github.com/markbates/refresh/refresh/web"
"github.com/markbates/sigtx"
"github.com/pkg/errors"
@@ -107,6 +108,13 @@ func (a *App) Stop(err error) error {
}

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
// convert the request's body to a packd.File which can be read N times
f, err := packd.NewFile("", r.Body)
if err == nil {
r.Body = f
}
}
ws := &Response{
ResponseWriter: w,
}

0 comments on commit c72131c

Please sign in to comment.
You can’t perform that action at this time.