Skip to content

Commit

Permalink
增加测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
xfali committed Nov 10, 2020
1 parent 517f462 commit f5378b5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
6 changes: 3 additions & 3 deletions buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type ContentLength interface {

type NopReadCloser struct {
r io.Reader
len int
len int64
}

func (rc *NopReadCloser) Read(p []byte) (n int, err error) {
Expand All @@ -89,7 +89,7 @@ func (rc *NopReadCloser) Close() error {
}

func (rc *NopReadCloser) ContentLength() int64 {
return int64(rc.len)
return rc.len
}

func NewReadCloser(d []byte) *NopReadCloser {
Expand All @@ -98,7 +98,7 @@ func NewReadCloser(d []byte) *NopReadCloser {
}
return &NopReadCloser{
r: bytes.NewReader(d),
len: len(d),
len: int64(len(d)),
}
}

Expand Down
48 changes: 48 additions & 0 deletions rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"github.com/xfali/restclient/restutil"
"github.com/xfali/xlog"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand All @@ -33,6 +34,10 @@ func startHttpServer(shutdown time.Duration) {
http.HandleFunc("/test", func(writer http.ResponseWriter, request *http.Request) {
v := request.Header.Get(restutil.HeaderAuthorization)
fmt.Println(v)
if request.Method == http.MethodPost {
d, _ := ioutil.ReadAll(request.Body)
fmt.Println(string(d))
}
writer.Header().Set(restutil.HeaderContentType, "application/json")
_, err := writer.Write([]byte(`{ "result":["hello", "world"]}`))
if err != nil {
Expand Down Expand Up @@ -295,6 +300,49 @@ func TestBuilder(t *testing.T) {
})
}

func TestContentLengthFilter(t *testing.T) {
t.Run("none", func(t *testing.T) {
c := New(SetTimeout(time.Second), AddFilter(
NewLog(xlog.GetLogger().WithDepth(5), "test").Filter,
ContentLengthFilter),
)
str := ""
_, err := c.Get(&str, "http://localhost:8080/test", nil)
if err != nil {
t.Fatal(err)
}
t.Log(str)
})

t.Run("content-length", func(t *testing.T) {
c := New(SetTimeout(time.Second), AddFilter(
NewLog(xlog.GetLogger().WithDepth(5), "test").Filter,
ContentLengthFilter),
)
str := ""
_, err := c.Post(&str, "http://localhost:8080/test", map[string]interface{}{
"Content-Length": "5",
}, "xxxxx")
if err != nil {
t.Fatal(err)
}
t.Log(str)
})

t.Run("auto", func(t *testing.T) {
c := New(SetTimeout(time.Second), AddFilter(
NewLog(xlog.GetLogger().WithDepth(5), "test").Filter,
ContentLengthFilter),
)
str := ""
_, err := c.Post(&str, "http://localhost:8080/test", nil, "xxxxx")
if err != nil {
t.Fatal(err)
}
t.Log(str)
})
}

func TestChunkGet(t *testing.T) {
t.Run("get_string_chunked", func(t *testing.T) {
c := New(SetTimeout(0))
Expand Down
12 changes: 5 additions & 7 deletions wrap_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/xfali/restclient/restutil"
"github.com/xfali/xlog"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -107,7 +106,7 @@ func (dr *DigestReader) Reader(r io.ReadCloser) io.ReadCloser {
if err != nil {
return nil
}
return ioutil.NopCloser(bytes.NewReader(dr.buf.Bytes()))
return buffer.NewReadCloser(dr.buf.Bytes())
}

func (b *DigestAuth) Exchange(ex Exchange) Exchange {
Expand Down Expand Up @@ -254,19 +253,18 @@ func NewLog(log xlog.Logger, tag string) *Log {
}

func (log *Log) Filter(request *http.Request, fc FilterChain) (*http.Response, error) {
buf := log.pool.Get()
defer log.pool.Put(buf)
reqBuf := buffer.NewReadWriteCloser(log.pool)

var reqData []byte
if request.Body != nil {
_, err := io.Copy(buf, request.Body)
_, err := io.Copy(reqBuf, request.Body)
if err != nil {
return nil, err
}
reqData = buf.Bytes()
reqData = reqBuf.Bytes()
// close old request body
request.Body.Close()
request.Body = buffer.NewReadCloser(reqData)
request.Body = reqBuf
}

now := time.Now()
Expand Down

0 comments on commit f5378b5

Please sign in to comment.