github.com/bytedance/go-tagexpr@v2.7.5-0.20210114074101-de5b8743ad85+incompatible/binding/body_test.go (about)

     1  package binding
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestBody(t *testing.T) {
    13  	var buf bytes.Buffer
    14  	buf.WriteString("abc")
    15  	req := &http.Request{
    16  		Body: ioutil.NopCloser(&buf),
    17  	}
    18  	body, err := GetBody(req)
    19  	assert.NoError(t, err)
    20  	b, err := ioutil.ReadAll(body)
    21  	assert.NoError(t, err)
    22  	assert.Equal(t, []byte("abc"), b)
    23  	body.Reset()
    24  	assert.Equal(t, []byte("abc"), body.bodyBytes)
    25  	b, err = ioutil.ReadAll(body)
    26  	assert.NoError(t, err)
    27  	assert.Equal(t, []byte("abc"), b)
    28  }