Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cade90cc40 | ||
| ba2630abdd | |||
| 329d02f6b0 | |||
| 54a47b2073 | |||
| 965d1e6d16 | |||
| a0d8830242 | |||
| 823e5e0bcf | |||
| 30876ae2bf | |||
| 75fc71ccb4 | |||
| e165b6a0a0 | |||
| 2307e6eb10 | |||
| 28d4c9a297 | |||
| 18de9c2c1f | |||
| 1f673f991f | |||
| 2e315b1b52 | |||
| 16859fc78e | |||
|
|
72491204d6 | ||
|
|
84ae0a710e | ||
| 12cf8d58ba | |||
|
|
a6217886dc | ||
|
|
0c71408d1f | ||
|
|
9d6dee25c9 | ||
|
|
8a6e29bc8a | ||
|
|
c4a12f9314 |
12
base.go
12
base.go
@@ -12,17 +12,19 @@ func buildBodyRequest(wf *Workflow) *http.Request {
|
||||
var err error
|
||||
contentType := ""
|
||||
|
||||
if wf.Body.IOBody() == nil {
|
||||
if wf.Body.GetIOBody() == nil {
|
||||
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), nil)
|
||||
} else {
|
||||
var bodybuf *bytes.Buffer
|
||||
switch wf.Body.IOBody().(type) {
|
||||
switch wf.Body.GetIOBody().(type) {
|
||||
case []byte:
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody().([]byte))
|
||||
bodybuf = bytes.NewBuffer(wf.Body.GetIOBody().([]byte))
|
||||
case string:
|
||||
bodybuf = bytes.NewBuffer([]byte(wf.Body.GetIOBody().(string)))
|
||||
case *bytes.Buffer:
|
||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody().(*bytes.Buffer).Bytes())
|
||||
bodybuf = bytes.NewBuffer(wf.Body.GetIOBody().(*bytes.Buffer).Bytes())
|
||||
default:
|
||||
panic(errors.New("the type is not exist, type is" + reflect.TypeOf(wf.Body.IOBody).String()))
|
||||
panic(errors.New("the type is not exist, type is " + reflect.TypeOf(wf.Body.GetIOBody()).String()))
|
||||
}
|
||||
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), bodybuf)
|
||||
}
|
||||
|
||||
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=
|
||||
12
multipart.go
12
multipart.go
@@ -15,6 +15,11 @@ func writeFormUploadFile(mwriter *multipart.Writer, ufile *UploadFile) {
|
||||
log.Panic(err)
|
||||
}
|
||||
io.Copy(part, ufile.FileReaderCloser)
|
||||
|
||||
err = ufile.FileReaderCloser.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func createMultipart(postParams IBody, params []interface{}) {
|
||||
@@ -23,8 +28,6 @@ func createMultipart(postParams IBody, params []interface{}) {
|
||||
body := &bytes.Buffer{}
|
||||
mwriter := multipart.NewWriter(body)
|
||||
|
||||
defer mwriter.Close()
|
||||
|
||||
for _, iparam := range params[0 : plen-1] {
|
||||
switch param := iparam.(type) {
|
||||
case *UploadFile:
|
||||
@@ -87,4 +90,9 @@ func createMultipart(postParams IBody, params []interface{}) {
|
||||
|
||||
postParams.AddContentType(mwriter.FormDataContentType())
|
||||
postParams.SetIOBody(body)
|
||||
|
||||
err := mwriter.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
67
session.go
67
session.go
@@ -36,8 +36,8 @@ func (body *Body) SetIOBody(iobody interface{}) {
|
||||
body.ioBody = iobody
|
||||
}
|
||||
|
||||
// IOBody 获取ioBody值
|
||||
func (body *Body) IOBody() interface{} {
|
||||
// GetIOBody 获取ioBody值
|
||||
func (body *Body) GetIOBody() interface{} {
|
||||
return body.ioBody
|
||||
}
|
||||
|
||||
@@ -70,16 +70,15 @@ func (body *Body) AddContentType(ct string) {
|
||||
|
||||
// IBody 相关参数结构
|
||||
type IBody interface {
|
||||
|
||||
// Query map[string][]string
|
||||
IOBody() interface{}
|
||||
// SetIOBody
|
||||
// GetIOBody 获取iobody data
|
||||
GetIOBody() interface{}
|
||||
// SetIOBody 设置iobody data
|
||||
SetIOBody(iobody interface{})
|
||||
// Files []UploadFile
|
||||
// ContentType 返回包括 Prefix 所有的ContentType
|
||||
ContentType() string
|
||||
// AppendContent
|
||||
AddContentType(ct string)
|
||||
// AddPrefix 添加 Prefix
|
||||
// SetPrefix 设置 Prefix; 唯一前缀
|
||||
SetPrefix(ct string)
|
||||
}
|
||||
|
||||
@@ -155,6 +154,9 @@ const (
|
||||
// CDialTimeout 一个Connect过程的Timeout
|
||||
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
||||
|
||||
// CKeepAlives 默认不KeepAlives, 容易被一直KeepAlives 没关闭链接
|
||||
CKeepAlives
|
||||
|
||||
// CProxy 代理链接
|
||||
CProxy // http, https, socks5
|
||||
|
||||
@@ -174,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})
|
||||
@@ -207,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 {
|
||||
@@ -230,7 +236,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
||||
ses.transport.Proxy = nil
|
||||
}
|
||||
case CInsecure:
|
||||
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)}
|
||||
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: values.(bool)}
|
||||
case CTLS:
|
||||
ses.transport.TLSClientConfig = values.(*tls.Config)
|
||||
case CBasicAuth:
|
||||
@@ -282,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)
|
||||
}
|
||||
|
||||
@@ -308,6 +314,13 @@ func (ses *Session) ClearCookies() {
|
||||
ses.client.Jar = ses.cookiejar
|
||||
}
|
||||
|
||||
// Head 请求
|
||||
func (ses *Session) Head(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "HEAD"
|
||||
return wf
|
||||
}
|
||||
|
||||
// Get 请求
|
||||
func (ses *Session) Get(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
@@ -336,20 +349,6 @@ func (ses *Session) Patch(url string) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// Delete 请求
|
||||
func (ses *Session) Delete(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "DELETE"
|
||||
return wf
|
||||
}
|
||||
|
||||
// Head 请求
|
||||
func (ses *Session) Head(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "HEAD"
|
||||
return wf
|
||||
}
|
||||
|
||||
// Options 请求
|
||||
func (ses *Session) Options(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
@@ -357,16 +356,22 @@ func (ses *Session) Options(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()
|
||||
// Delete 请求
|
||||
func (ses *Session) Delete(url string) *Workflow {
|
||||
wf := NewWorkflow(ses, url)
|
||||
wf.Method = "DELETE"
|
||||
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()
|
||||
// }
|
||||
|
||||
// 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()
|
||||
|
||||
@@ -76,14 +76,14 @@ func TestSession_Post(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params...).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
@@ -127,13 +127,13 @@ func TestSession_Setparams(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params...).Execute()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
@@ -169,14 +169,14 @@ func TestSession_PostUploadFile(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Post("http://httpbin.org/post").AutoSetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
@@ -213,14 +213,14 @@ func TestSession_Put(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Put("http://httpbin.org/put").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Put("http://httpbin.org/put").AutoSetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if tt.want.MatchString(got.DContent) == false {
|
||||
if tt.want.MatchString(got.readContent) == false {
|
||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||
}
|
||||
|
||||
@@ -257,14 +257,14 @@ func TestSession_Patch(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ses := NewSession()
|
||||
got, err := ses.Patch("http://httpbin.org/patch").SetBody(tt.args.params, TypeFormData).Execute()
|
||||
got, err := ses.Patch("http://httpbin.org/patch").AutoSetBody(tt.args.params, TypeFormData).Execute()
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Metchod error = %v", err)
|
||||
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))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -17,13 +17,11 @@ type UploadFile struct {
|
||||
// UploadFileFromPath 从本地文件获取上传文件
|
||||
func UploadFileFromPath(fileName string) (*UploadFile, error) {
|
||||
fd, err := os.Open(fileName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil
|
||||
|
||||
}
|
||||
|
||||
// UploadFileFromGlob 根据Glob从本地文件获取上传文件
|
||||
|
||||
48
workflow.go
48
workflow.go
@@ -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,13 +37,20 @@ 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
|
||||
}
|
||||
|
||||
// SetHeader 设置头信息 这样构造 []string{value} Get方法从Header参数上获取
|
||||
func (wf *Workflow) SetHeader(key, value string) *Workflow {
|
||||
wf.Header.Set(key, value)
|
||||
// SetHeader 设置完全替换原有Header
|
||||
func (wf *Workflow) SetHeader(header http.Header) *Workflow {
|
||||
wf.Header = make(http.Header)
|
||||
for k, HValues := range header {
|
||||
var newHValues []string
|
||||
for _, value := range HValues {
|
||||
newHValues = append(newHValues, value)
|
||||
}
|
||||
wf.Header[k] = newHValues
|
||||
}
|
||||
return wf
|
||||
}
|
||||
|
||||
@@ -69,6 +76,14 @@ func (wf *Workflow) AddCookie(c *http.Cookie) *Workflow {
|
||||
return wf
|
||||
}
|
||||
|
||||
// AddCookies 添加[]*http.Cookie
|
||||
func (wf *Workflow) AddCookies(cookies []*http.Cookie) *Workflow {
|
||||
for _, c := range cookies {
|
||||
wf.AddCookie(c)
|
||||
}
|
||||
return wf
|
||||
}
|
||||
|
||||
// AddKVCookie 添加 以 key value 的 Cookie
|
||||
func (wf *Workflow) AddKVCookie(name, value string) *Workflow {
|
||||
wf.Cookies[name] = &http.Cookie{Name: name, Value: value}
|
||||
@@ -185,7 +200,18 @@ func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
||||
}
|
||||
|
||||
// SetBody 参数设置
|
||||
func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
|
||||
func (wf *Workflow) SetBody(body IBody) *Workflow {
|
||||
wf.Body = body
|
||||
return wf
|
||||
}
|
||||
|
||||
// GetBody 参数设置
|
||||
func (wf *Workflow) GetBody(body IBody) IBody {
|
||||
return wf.Body
|
||||
}
|
||||
|
||||
// AutoSetBody 参数设置
|
||||
func (wf *Workflow) AutoSetBody(params ...interface{}) *Workflow {
|
||||
|
||||
if params != nil {
|
||||
plen := len(params)
|
||||
@@ -194,11 +220,10 @@ func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
|
||||
if plen >= 2 {
|
||||
t := params[plen-1]
|
||||
defaultContentType = t.(string)
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
} else {
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
}
|
||||
|
||||
wf.Body.SetPrefix(defaultContentType)
|
||||
|
||||
if defaultContentType == TypeFormData {
|
||||
createMultipart(wf.Body, params)
|
||||
} else {
|
||||
@@ -286,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