From b0db5b3a69c031b591a9bd0f146dfaba4ba93d0d Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 5 Sep 2019 18:35:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9gjson=E5=88=B0tidwall?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E7=89=88=E6=9C=AC1.3.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 8 +++----- go.sum | 24 ++++++++++++++++++++---- response_test.go | 2 +- session_test.go | 2 +- upload_file_test.go | 2 +- workflow_test.go | 2 +- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index f123b43..28aa5dc 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,9 @@ module requests go 1.12 require ( - github.com/474420502/gjson v1.1.3 github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 // indirect - github.com/tidwall/match v1.0.1 // indirect - golang.org/x/net v0.0.0-00010101000000-000000000000 + github.com/stretchr/testify v1.4.0 // indirect + github.com/tidwall/gjson v1.3.2 + golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 ) - -replace golang.org/x/net => github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087 diff --git a/go.sum b/go.sum index 28490c7..8d1a025 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,27 @@ -github.com/474420502/gjson v1.1.3 h1:rQxKNSFS8bM5iVVKKb9EHY1SS2k+EhzVNUXe2xSGn8o= -github.com/474420502/gjson v1.1.3/go.mod h1:mdAOevjPYIFWOE8CpejPHwoJCz96oNnuwhjhrAVeKaY= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 h1:aZtFdDNWY/yH86JPR2WX/PN63635VsE/f/nXNPAbYxY= github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM= github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8= -github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087 h1:haK1T12C0CO79KUdu+ZzLL9+l9BwM9PRkd2/mQqdg8E= -github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087/go.mod h1:98y8FxUyMjTdJ5eOj/8vzuiVO14/dkJ98NYhEPG8QGY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/tidwall/gjson v1.3.2 h1:+7p3qQFaH3fOMXAJSrdZwGKcOO/lYdGS0HqGhPqDdTI= +github.com/tidwall/gjson v1.3.2/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc= github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= +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/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +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= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/response_test.go b/response_test.go index b2524d4..180dfdb 100644 --- a/response_test.go +++ b/response_test.go @@ -4,7 +4,7 @@ import ( "net/http" "testing" - "github.com/474420502/gjson" + "github.com/tidwall/gjson" ) func TestFromHTTPResponse(t *testing.T) { diff --git a/session_test.go b/session_test.go index 440c91a..c932b97 100644 --- a/session_test.go +++ b/session_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/474420502/gjson" + "github.com/tidwall/gjson" ) func TestNewSession(t *testing.T) { diff --git a/upload_file_test.go b/upload_file_test.go index 860e9b0..2d56521 100644 --- a/upload_file_test.go +++ b/upload_file_test.go @@ -3,7 +3,7 @@ package requests import ( "testing" - "github.com/474420502/gjson" + "github.com/tidwall/gjson" ) func TestUploadFile(t *testing.T) { diff --git a/workflow_test.go b/workflow_test.go index d42698a..ef73d4f 100644 --- a/workflow_test.go +++ b/workflow_test.go @@ -7,7 +7,7 @@ import ( "sort" "testing" - "github.com/474420502/gjson" + "github.com/tidwall/gjson" ) func TestWorkflow(t *testing.T) { From e3d696aa430694c08721899b878c31506dfdc59c Mon Sep 17 00:00:00 2001 From: huangsimin Date: Fri, 3 Apr 2020 15:32:33 +0800 Subject: [PATCH 2/2] add response ContentBytes() --- go.mod | 4 ++-- go.sum | 5 +++-- response.go | 26 +++++++++++++++----------- session_test.go | 22 +++++++++++----------- workflow_test.go | 22 +++++++++++----------- 5 files changed, 42 insertions(+), 37 deletions(-) diff --git a/go.mod b/go.mod index 28aa5dc..b427dba 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 8d1a025..dc09fe9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/response.go b/response.go index fcc9363..3692465 100644 --- a/response.go +++ b/response.go @@ -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 diff --git a/session_test.go b/session_test.go index c932b97..421ff4a 100644 --- a/session_test.go +++ b/session_test.go @@ -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())) } }) } diff --git a/workflow_test.go b/workflow_test.go index ef73d4f..e486086 100644 --- a/workflow_test.go +++ b/workflow_test.go @@ -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()) } })