Merge branch 'release/v2.0.1'

This commit is contained in:
huangsimin 2020-04-03 15:36:58 +08:00
commit 12283fed12
7 changed files with 66 additions and 47 deletions

10
go.mod
View File

@ -1,13 +1,11 @@
module requests module requests
go 1.12 go 1.14
require ( require (
github.com/474420502/gjson v1.1.3
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2 github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 // indirect github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 // indirect
github.com/tidwall/match v1.0.1 // indirect github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/net v0.0.0-00010101000000-000000000000 github.com/tidwall/gjson v1.3.2
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
) )
replace golang.org/x/net => github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087

25
go.sum
View File

@ -1,11 +1,28 @@
github.com/474420502/gjson v1.1.3 h1:rQxKNSFS8bM5iVVKKb9EHY1SS2k+EhzVNUXe2xSGn8o= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/474420502/gjson v1.1.3/go.mod h1:mdAOevjPYIFWOE8CpejPHwoJCz96oNnuwhjhrAVeKaY= 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 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 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 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8= 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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087/go.mod h1:98y8FxUyMjTdJ5eOj/8vzuiVO14/dkJ98NYhEPG8QGY= 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/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 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= 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-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=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

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

View File

@ -4,7 +4,7 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/474420502/gjson" "github.com/tidwall/gjson"
) )
func TestFromHTTPResponse(t *testing.T) { func TestFromHTTPResponse(t *testing.T) {

View File

@ -8,7 +8,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/474420502/gjson" "github.com/tidwall/gjson"
) )
func TestNewSession(t *testing.T) { func TestNewSession(t *testing.T) {
@ -83,7 +83,7 @@ func TestSession_Post(t *testing.T) {
return return
} }
if tt.want.MatchString(got.readContent) == false { if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want) t.Errorf("Metchod = %v, want %v", got, tt.want)
} }
@ -133,7 +133,7 @@ func TestSession_Setparams(t *testing.T) {
return return
} }
if tt.want.MatchString(got.readContent) == false { if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want) t.Errorf("Metchod = %v, want %v", got, tt.want)
} }
}) })
@ -176,7 +176,7 @@ func TestSession_PostUploadFile(t *testing.T) {
return return
} }
if tt.want.MatchString(got.readContent) == false { if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want) t.Errorf("Metchod = %v, want %v", got, tt.want)
} }
@ -220,7 +220,7 @@ func TestSession_Put(t *testing.T) {
return return
} }
if tt.want.MatchString(got.readContent) == false { if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want) t.Errorf("Metchod = %v, want %v", got, tt.want)
} }
@ -264,7 +264,7 @@ func TestSession_Patch(t *testing.T) {
return return
} }
if tt.want.MatchString(got.readContent) == false { if tt.want.MatchString(got.Content()) == false {
t.Errorf("Metchod = %v, want %v", got, tt.want) 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) t.Error("cookies set error", err)
} }
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) { if !regexp.MustCompile(`"a": "1"`).MatchString(resp.Content()) {
t.Error(resp.readContent) t.Error(resp.Content())
} }
}) })
} }
@ -364,8 +364,8 @@ func TestSession_Header(t *testing.T) {
t.Error("cookies set error", err) t.Error("cookies set error", err)
} }
if len(resp.readContent) <= 5000 { if len(resp.Content()) <= 5000 {
t.Error(resp.readContent, len(resp.readContent)) t.Error(resp.Content(), len(resp.Content()))
} }
ses = NewSession() ses = NewSession()
@ -374,8 +374,8 @@ func TestSession_Header(t *testing.T) {
t.Error("cookies set error", err) t.Error("cookies set error", err)
} }
if len(resp.readContent) <= 5000 { if len(resp.Content()) <= 5000 {
t.Error(resp.readContent, len(resp.readContent)) t.Error(resp.Content(), len(resp.Content()))
} }
}) })
} }

View File

@ -3,7 +3,7 @@ package requests
import ( import (
"testing" "testing"
"github.com/474420502/gjson" "github.com/tidwall/gjson"
) )
func TestUploadFile(t *testing.T) { func TestUploadFile(t *testing.T) {

View File

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