forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
50 lines (43 loc) · 1006 Bytes
/
request_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package buffalo
import (
"io/ioutil"
"net/http"
"testing"
"github.com/gobuffalo/httptest"
"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 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 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)
}