add response ContentBytes()

This commit is contained in:
huangsimin 2020-04-03 15:32:33 +08:00
parent b0db5b3a69
commit e3d696aa43
5 changed files with 42 additions and 37 deletions

4
go.mod
View File

@ -1,11 +1,11 @@
module requests
go 1.12
go 1.14
require (
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 // indirect
github.com/stretchr/testify v1.4.0 // indirect
github.com/tidwall/gjson v1.3.2
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
)

5
go.sum
View File

@ -17,9 +17,10 @@ github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -11,13 +11,15 @@ import (
// Response 响应内容包含http.Response 已读
type Response struct {
readContent string
readBytes []byte
readResponse *http.Response
}
// FromHTTPResponse 生成Response 从标准http.Response
func FromHTTPResponse(resp *http.Response, isDecompressNoAccept bool) (*Response, error) {
var err error
var rbuf []byte
// 复制response 返回内容 并且测试是否有解压的需求
srcbuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
@ -25,39 +27,41 @@ func FromHTTPResponse(resp *http.Response, isDecompressNoAccept bool) (*Response
}
resp.Body.Close()
content := ""
if isDecompressNoAccept { // 在某个已经遗忘的网页测试过, 为了兼容 Python requests
srcReader := bytes.NewReader(srcbuf)
var reader io.ReadCloser
if reader, err = gzip.NewReader(srcReader); err == nil {
defer reader.Close()
buf, err := ioutil.ReadAll(reader)
rbuf, err = ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
content = string(buf)
} else if reader, err = zlib.NewReader(srcReader); err == nil {
defer reader.Close()
buf, err := ioutil.ReadAll(reader)
rbuf, err = ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
content = string(buf)
} else {
content = string(srcbuf)
rbuf = srcbuf
}
} else {
content = string(srcbuf)
rbuf = srcbuf
}
return &Response{readContent: content, readResponse: resp}, nil
return &Response{readBytes: rbuf, readResponse: resp}, nil
}
// Content 返回解压后的内容
func (gresp *Response) Content() string {
return gresp.readContent
return string(gresp.readBytes)
}
// ContentBytes 返回解压后的内容Bytes
func (gresp *Response) ContentBytes() []byte {
return gresp.readBytes
}
// GetSrcResponse 获取原生golang http.Response

View File

@ -83,7 +83,7 @@ func TestSession_Post(t *testing.T) {
return
}
if tt.want.MatchString(got.readContent) == false {
if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want)
}
@ -133,7 +133,7 @@ func TestSession_Setparams(t *testing.T) {
return
}
if tt.want.MatchString(got.readContent) == false {
if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want)
}
})
@ -176,7 +176,7 @@ func TestSession_PostUploadFile(t *testing.T) {
return
}
if tt.want.MatchString(got.readContent) == false {
if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want)
}
@ -220,7 +220,7 @@ func TestSession_Put(t *testing.T) {
return
}
if tt.want.MatchString(got.readContent) == false {
if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want)
}
@ -264,7 +264,7 @@ func TestSession_Patch(t *testing.T) {
return
}
if tt.want.MatchString(got.readContent) == false {
if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want)
}
@ -346,8 +346,8 @@ func TestSession_Cookies(t *testing.T) {
t.Error("cookies set error", err)
}
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
t.Error(resp.readContent)
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.Content()) {
t.Error(resp.Content())
}
})
}
@ -364,8 +364,8 @@ func TestSession_Header(t *testing.T) {
t.Error("cookies set error", err)
}
if len(resp.readContent) <= 5000 {
t.Error(resp.readContent, len(resp.readContent))
if len(resp.Content()) <= 5000 {
t.Error(resp.Content(), len(resp.Content()))
}
ses = NewSession()
@ -374,8 +374,8 @@ func TestSession_Header(t *testing.T) {
t.Error("cookies set error", err)
}
if len(resp.readContent) <= 5000 {
t.Error(resp.readContent, len(resp.readContent))
if len(resp.Content()) <= 5000 {
t.Error(resp.Content(), len(resp.Content()))
}
})
}

View File

@ -19,8 +19,8 @@ func TestWorkflow(t *testing.T) {
t.Error("cookies set error", err)
}
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
t.Error(resp.readContent)
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.Content()) {
t.Error(resp.Content())
}
wf := ses.Get("http://httpbin.org/cookies/set")
@ -29,24 +29,24 @@ func TestWorkflow(t *testing.T) {
t.Error("cookies set error", err)
}
result := gjson.Get(resp.readContent, "cookies.a")
result := gjson.Get(resp.Content(), "cookies.a")
if result.Exists() {
t.Error(resp.readContent)
t.Error(resp.Content())
}
result = gjson.Get(resp.readContent, "cookies.b")
result = gjson.Get(resp.Content(), "cookies.b")
if result.Int() != 2 {
t.Error(resp.readContent)
t.Error(resp.Content())
}
resp, err = wf.AddKVCookie("a", "3").Execute()
results := gjson.GetMany(resp.readContent, "cookies.a", "cookies.b")
results := gjson.GetMany(resp.Content(), "cookies.a", "cookies.b")
if results[0].Int() != 3 {
t.Error(resp.readContent)
t.Error(resp.Content())
}
if results[1].Int() != 2 {
t.Error(resp.readContent)
t.Error(resp.Content())
}
resp, err = wf.AddHeader("XX", "123").SetRawURL("http://httpbin.org/headers").Execute()
@ -55,9 +55,9 @@ func TestWorkflow(t *testing.T) {
}
// headers 只能是String 表示
result = gjson.Get(resp.readContent, "headers.Xx")
result = gjson.Get(resp.Content(), "headers.Xx")
if result.String() != "123" {
t.Error(resp.readContent)
t.Error(resp.Content())
}
})