Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cade90cc40 | ||
| ba2630abdd | |||
| 329d02f6b0 | |||
| 54a47b2073 | |||
| 965d1e6d16 | |||
| a0d8830242 | |||
| 823e5e0bcf | |||
| 30876ae2bf | |||
| 75fc71ccb4 | |||
| e165b6a0a0 | |||
| 2307e6eb10 | |||
| 28d4c9a297 | |||
| 18de9c2c1f | |||
| 1f673f991f | |||
| 2e315b1b52 | |||
| 16859fc78e |
11
go.mod
Normal file
11
go.mod
Normal file
@@ -0,0 +1,11 @@
|
||||
module requests
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
474420502.top/eson/gjson v1.1.3
|
||||
github.com/tidwall/match v1.0.1 // indirect
|
||||
golang.org/x/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087
|
||||
)
|
||||
|
||||
replace golang.org/x/net => github.com/golang/net v0.0.0-20190902185345-cdfb69ac37fc6fa907650654115ebebb3aae2087
|
||||
11
go.sum
Normal file
11
go.sum
Normal file
@@ -0,0 +1,11 @@
|
||||
474420502.top/eson/gjson v1.1.3 h1:SDeD1/SWm1YknuokcPww8ZmsOOguQqFAYLWnQTMMX98=
|
||||
474420502.top/eson/gjson v1.1.3/go.mod h1:95mdr7XPHsGvsGZj/FeQ+iT7mggI6LKc3JT/ZnveebI=
|
||||
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/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
|
||||
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU=
|
||||
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/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=
|
||||
42
response.go
42
response.go
@@ -9,52 +9,48 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Response 响应内容包含http.Response
|
||||
// Response 响应内容包含http.Response 已读
|
||||
type Response struct {
|
||||
DContent string
|
||||
GResponse *http.Response
|
||||
readContent string
|
||||
readResponse *http.Response
|
||||
}
|
||||
|
||||
// FromHTTPResponse 生成Response 从标准http.Response
|
||||
func FromHTTPResponse(resp *http.Response) (*Response, error) {
|
||||
|
||||
var err error
|
||||
// 复制response 返回内容 并且测试是否有解压的需求
|
||||
srcbuf, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
cbuf := bytes.NewBuffer([]byte{})
|
||||
_, err = io.Copy(cbuf, bytes.NewReader(srcbuf))
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
resp.Body = ioutil.NopCloser(cbuf)
|
||||
|
||||
content := string(srcbuf)
|
||||
content := ""
|
||||
srcReader := bytes.NewReader(srcbuf)
|
||||
|
||||
if r, err := gzip.NewReader(srcReader); err == nil {
|
||||
buf, err := ioutil.ReadAll(r)
|
||||
var reader io.ReadCloser
|
||||
if reader, err = gzip.NewReader(srcReader); err == nil {
|
||||
defer reader.Close()
|
||||
buf, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
content = string(buf)
|
||||
} else if r, err := zlib.NewReader(srcReader); err == nil {
|
||||
buf, err := ioutil.ReadAll(r)
|
||||
} else if reader, err = zlib.NewReader(srcReader); err == nil {
|
||||
defer reader.Close()
|
||||
buf, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
content = string(buf)
|
||||
} else {
|
||||
content = string(srcbuf)
|
||||
}
|
||||
|
||||
return &Response{DContent: content, GResponse: resp}, nil
|
||||
return &Response{readContent: content, readResponse: resp}, nil
|
||||
}
|
||||
|
||||
// Content 返回解压后的内容
|
||||
func (gresp *Response) Content() string {
|
||||
return gresp.DContent
|
||||
return gresp.readContent
|
||||
}
|
||||
|
||||
24
session.go
24
session.go
@@ -154,6 +154,9 @@ const (
|
||||
// CDialTimeout 一个Connect过程的Timeout
|
||||
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
||||
|
||||
// CKeepAlives 默认不KeepAlives, 容易被一直KeepAlives 没关闭链接
|
||||
CKeepAlives
|
||||
|
||||
// CProxy 代理链接
|
||||
CProxy // http, https, socks5
|
||||
|
||||
@@ -173,7 +176,9 @@ const (
|
||||
// NewSession 创建Session
|
||||
func NewSession() *Session {
|
||||
client := &http.Client{}
|
||||
transport := &http.Transport{DisableCompression: true}
|
||||
transport := &http.Transport{DisableCompression: true, DisableKeepAlives: true}
|
||||
|
||||
EnsureTransporterFinalized(transport)
|
||||
|
||||
client.Transport = transport
|
||||
cjar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||
@@ -206,6 +211,8 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||
}
|
||||
case CDialTimeout:
|
||||
// 没时间实现这些小细节
|
||||
case CKeepAlives:
|
||||
ses.transport.DisableKeepAlives = !values.(bool)
|
||||
case CCookiejar:
|
||||
v := values.(bool)
|
||||
if v {
|
||||
@@ -281,8 +288,8 @@ func (ses *Session) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
||||
ses.cookiejar.SetCookies(u, cookies)
|
||||
}
|
||||
|
||||
// Cookies 返回 Cookies
|
||||
func (ses *Session) Cookies(u *url.URL) []*http.Cookie {
|
||||
// GetCookies 返回 Cookies
|
||||
func (ses *Session) GetCookies(u *url.URL) []*http.Cookie {
|
||||
return ses.cookiejar.Cookies(u)
|
||||
}
|
||||
|
||||
@@ -356,16 +363,15 @@ func (ses *Session) Delete(url string) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// CloseIdleConnections closes the idle connections that a session client may make use of
|
||||
// 从levigross/grequests 借鉴
|
||||
func (ses *Session) CloseIdleConnections() {
|
||||
ses.client.Transport.(*http.Transport).CloseIdleConnections()
|
||||
}
|
||||
// // CloseIdleConnections closes the idle connections that a session client may make use of
|
||||
// // 从levigross/grequests 借鉴
|
||||
// func (ses *Session) CloseIdleConnections() {
|
||||
// ses.client.Transport.(*http.Transport).CloseIdleConnections()
|
||||
// }
|
||||
|
||||
// EnsureTransporterFinalized will ensure that when the HTTP client is GCed
|
||||
// the runtime will close the idle connections (so that they won't leak)
|
||||
// this function was adopted from Hashicorp's go-cleanhttp package
|
||||
// 暂时不用, 标记到以后是否起作用
|
||||
func EnsureTransporterFinalized(httpTransport *http.Transport) {
|
||||
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
|
||||
(*transportInt).CloseIdleConnections()
|
||||
|
||||
@@ -83,7 +83,7 @@ func TestSession_Post(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == 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.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == 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.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == 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.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == 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.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ func TestSession_SetConfig(t *testing.T) {
|
||||
func TestSession_SetConfigInsecure(t *testing.T) {
|
||||
|
||||
ses := NewSession()
|
||||
ses.SetConfig(CInsecure, false)
|
||||
ses.SetConfig(CInsecure, true)
|
||||
|
||||
for _, badSSL := range []string{
|
||||
"https://self-signed.badssl.com/",
|
||||
@@ -330,8 +330,8 @@ func TestSession_SetConfigInsecure(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Unable to make request", err)
|
||||
}
|
||||
if resp.GResponse.StatusCode != 200 {
|
||||
t.Error("Request did not return OK, is ", resp.GResponse.StatusCode)
|
||||
if resp.readResponse.StatusCode != 200 {
|
||||
t.Error("Request did not return OK, is ", resp.readResponse.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,14 +341,13 @@ func TestSession_Cookies(t *testing.T) {
|
||||
ses := NewSession()
|
||||
|
||||
t.Run("set cookie", func(t *testing.T) {
|
||||
|
||||
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.DContent) {
|
||||
t.Error(resp.DContent)
|
||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -365,8 +364,8 @@ func TestSession_Header(t *testing.T) {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if len(resp.DContent) <= 5000 {
|
||||
t.Error(resp.DContent, len(resp.DContent))
|
||||
if len(resp.readContent) <= 5000 {
|
||||
t.Error(resp.readContent, len(resp.readContent))
|
||||
}
|
||||
|
||||
ses = NewSession()
|
||||
@@ -375,8 +374,8 @@ func TestSession_Header(t *testing.T) {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if len(resp.DContent) <= 5000 {
|
||||
t.Error(resp.DContent, len(resp.DContent))
|
||||
if len(resp.readContent) <= 5000 {
|
||||
t.Error(resp.readContent, len(resp.readContent))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Workflow 工作流
|
||||
// Workflow 工作流 设计点: 这个并不影响Session的属性变化 如 NewWorkflow(ses, url).AddHeader() 对ses没影响
|
||||
type Workflow struct {
|
||||
session *Session
|
||||
ParsedURL *url.URL
|
||||
@@ -18,11 +18,11 @@ type Workflow struct {
|
||||
}
|
||||
|
||||
// NewWorkflow new and init workflow
|
||||
func NewWorkflow(ses *Session, u string) *Workflow {
|
||||
func NewWorkflow(ses *Session, urlstr string) *Workflow {
|
||||
wf := &Workflow{}
|
||||
wf.SwitchSession(ses)
|
||||
|
||||
wf.SetRawURL(u)
|
||||
wf.SetRawURL(urlstr)
|
||||
|
||||
wf.Body = NewBody()
|
||||
wf.Header = make(http.Header)
|
||||
@@ -37,7 +37,7 @@ func (wf *Workflow) SwitchSession(ses *Session) {
|
||||
|
||||
// AddHeader 添加头信息 Get方法从Header参数上获取
|
||||
func (wf *Workflow) AddHeader(key, value string) *Workflow {
|
||||
wf.Header.Add(key, value)
|
||||
wf.Header[key] = append(wf.Header[key], value)
|
||||
return wf
|
||||
}
|
||||
|
||||
@@ -311,5 +311,6 @@ func (wf *Workflow) Execute() (*Response, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return FromHTTPResponse(resp)
|
||||
}
|
||||
|
||||
60
workflow_test.go
Normal file
60
workflow_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"474420502.top/eson/gjson"
|
||||
)
|
||||
|
||||
func TestWorkflow(t *testing.T) {
|
||||
ses := NewSession()
|
||||
|
||||
t.Run("set cookie", func(t *testing.T) {
|
||||
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
wf := ses.Get("http://httpbin.org/cookies/set")
|
||||
resp, err = wf.AddKVCookie("b", "2").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
result := gjson.Get(resp.readContent, "cookies.a")
|
||||
if result.Exists() {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
result = gjson.Get(resp.readContent, "cookies.b")
|
||||
if result.Int() != 2 {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
resp, err = wf.AddKVCookie("a", "3").Execute()
|
||||
results := gjson.GetMany(resp.readContent, "cookies.a", "cookies.b")
|
||||
if results[0].Int() != 3 {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
if results[1].Int() != 2 {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
|
||||
resp, err = wf.AddHeader("XX", "123").SetRawURL("http://httpbin.org/headers").Execute()
|
||||
if err != nil {
|
||||
t.Error("cookies set error", err)
|
||||
}
|
||||
|
||||
// headers 只能是String 表示
|
||||
result = gjson.Get(resp.readContent, "headers.Xx")
|
||||
if result.String() != "123" {
|
||||
t.Error(resp.readContent)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user