update vendor

This commit is contained in:
deepzz0
2017-07-11 23:50:01 +08:00
parent e1ec5cd08a
commit c18d9c0bef
107 changed files with 8347 additions and 126 deletions

View File

@@ -0,0 +1,63 @@
// This package provide a method to read and replace http.Request's body.
package seekable
import (
"errors"
"io"
"io/ioutil"
"net/http"
"qiniupkg.com/x/bytes.v7"
)
// ---------------------------------------------------
type Seekabler interface {
Bytes() []byte
Read(val []byte) (n int, err error)
SeekToBegin() error
}
type SeekableCloser interface {
Seekabler
io.Closer
}
// ---------------------------------------------------
type readCloser struct {
Seekabler
io.Closer
}
var ErrNoBody = errors.New("no body")
func New(req *http.Request) (r SeekableCloser, err error) {
if req.Body == nil {
return nil, ErrNoBody
}
var ok bool
if r, ok = req.Body.(SeekableCloser); ok {
return
}
b, err2 := ReadAll(req)
if err2 != nil {
return nil, err2
}
r = bytes.NewReader(b)
req.Body = readCloser{r, req.Body}
return
}
func ReadAll(req *http.Request) (b []byte, err error) {
if req.ContentLength > 0 {
b = make([]byte, int(req.ContentLength))
_, err = io.ReadFull(req.Body, b)
return
} else if req.ContentLength == 0 {
return nil, ErrNoBody
}
return ioutil.ReadAll(req.Body)
}
// ---------------------------------------------------

View File

@@ -0,0 +1,43 @@
package seekable
import (
"bytes"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSeekable_EOFIfReqAlreadyParsed(t *testing.T) {
body := "a=1"
req, err := http.NewRequest("POST", "/a", bytes.NewBufferString(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", "3")
req.ParseForm()
_, err = New(req)
assert.Equal(t, err.Error(), "EOF")
}
func TestSeekable_WorkaroundForEOF(t *testing.T) {
body := "a=1"
req, err := http.NewRequest("POST", "/a", bytes.NewBufferString(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", "3")
_, _ = New(req)
req.ParseForm()
assert.Equal(t, req.FormValue("a"), "1")
_, err = New(req)
assert.NoError(t, err)
}
func TestSeekable(t *testing.T) {
body := "a=1"
req, err := http.NewRequest("POST", "/a", bytes.NewBufferString(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", "3")
_, err = New(req)
assert.NoError(t, err)
}