Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49d86f6d31 | ||
|
|
48e235f35f | ||
|
|
4109dd0388 | ||
|
|
45bf499e8d | ||
|
|
a8f7dac34d | ||
|
|
b431436b17 | ||
| ec9b0614e6 | |||
| 3a355fdd3a | |||
| 0fa10daf8d | |||
|
|
8e2bcbaf02 | ||
|
|
5ca3e9fc0c | ||
|
|
9d998a1964 | ||
| 469276cdaa | |||
|
|
cade90cc40 | ||
| ba2630abdd | |||
| 329d02f6b0 | |||
| 54a47b2073 | |||
| 965d1e6d16 | |||
| a0d8830242 | |||
| 823e5e0bcf | |||
| 30876ae2bf | |||
| 75fc71ccb4 | |||
| e165b6a0a0 | |||
| 2307e6eb10 | |||
| 28d4c9a297 | |||
| 18de9c2c1f | |||
| 1f673f991f | |||
| 2e315b1b52 | |||
| 16859fc78e | |||
|
|
72491204d6 | ||
|
|
84ae0a710e | ||
| 12cf8d58ba | |||
|
|
a6217886dc | ||
|
|
0c71408d1f | ||
|
|
9d6dee25c9 | ||
|
|
8a6e29bc8a | ||
|
|
c4a12f9314 |
18
base.go
18
base.go
@@ -12,17 +12,19 @@ func buildBodyRequest(wf *Workflow) *http.Request {
|
|||||||
var err error
|
var err error
|
||||||
contentType := ""
|
contentType := ""
|
||||||
|
|
||||||
if wf.Body.IOBody() == nil {
|
if wf.Body.GetIOBody() == nil {
|
||||||
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), nil)
|
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), nil)
|
||||||
} else {
|
} else {
|
||||||
var bodybuf *bytes.Buffer
|
var bodybuf *bytes.Buffer
|
||||||
switch wf.Body.IOBody().(type) {
|
switch wf.Body.GetIOBody().(type) {
|
||||||
case []byte:
|
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:
|
case *bytes.Buffer:
|
||||||
bodybuf = bytes.NewBuffer(wf.Body.IOBody().(*bytes.Buffer).Bytes())
|
bodybuf = bytes.NewBuffer(wf.Body.GetIOBody().(*bytes.Buffer).Bytes())
|
||||||
default:
|
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)
|
req, err = http.NewRequest(wf.Method, wf.GetRawURL(), bodybuf)
|
||||||
}
|
}
|
||||||
@@ -35,10 +37,8 @@ func buildBodyRequest(wf *Workflow) *http.Request {
|
|||||||
contentType = wf.Body.ContentType()
|
contentType = wf.Body.ContentType()
|
||||||
} else {
|
} else {
|
||||||
contentType = ""
|
contentType = ""
|
||||||
if contentType == "" {
|
if wf.Method == "POST" || wf.Method == "PUT" || wf.Method == "PATCH" {
|
||||||
if wf.Method == "POST" || wf.Method == "PUT" || wf.Method == "PATCH" {
|
contentType = TypeURLENCODED
|
||||||
contentType = TypeURLENCODED
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
go.mod
Normal file
13
go.mod
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
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 @@
|
|||||||
|
github.com/474420502/gjson v1.1.3 h1:rQxKNSFS8bM5iVVKKb9EHY1SS2k+EhzVNUXe2xSGn8o=
|
||||||
|
github.com/474420502/gjson v1.1.3/go.mod h1:mdAOevjPYIFWOE8CpejPHwoJCz96oNnuwhjhrAVeKaY=
|
||||||
|
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/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
|
||||||
|
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
|
||||||
|
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
|
||||||
29
init_test.go
Normal file
29
init_test.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package requests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/elazarl/goproxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ProxyAddress = "localhost:58080"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
||||||
|
go func() {
|
||||||
|
proxy := goproxy.NewProxyHttpServer()
|
||||||
|
proxy.Verbose = true
|
||||||
|
http.ListenAndServe(ProxyAddress, proxy)
|
||||||
|
}()
|
||||||
|
|
||||||
|
cmd := exec.Command("/bin/bash", "-c", "docker ps | grep httpbin")
|
||||||
|
_, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("recommend 1. docker run -p 80:80 kennethreitz/httpbin \n2. echo \"127.0.0.1 httpbin.org\" >> /etc/hosts")
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
}
|
||||||
26
multipart.go
26
multipart.go
@@ -15,6 +15,11 @@ func writeFormUploadFile(mwriter *multipart.Writer, ufile *UploadFile) {
|
|||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
io.Copy(part, ufile.FileReaderCloser)
|
io.Copy(part, ufile.FileReaderCloser)
|
||||||
|
|
||||||
|
err = ufile.FileReaderCloser.Close()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMultipart(postParams IBody, params []interface{}) {
|
func createMultipart(postParams IBody, params []interface{}) {
|
||||||
@@ -23,8 +28,6 @@ func createMultipart(postParams IBody, params []interface{}) {
|
|||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
mwriter := multipart.NewWriter(body)
|
mwriter := multipart.NewWriter(body)
|
||||||
|
|
||||||
defer mwriter.Close()
|
|
||||||
|
|
||||||
for _, iparam := range params[0 : plen-1] {
|
for _, iparam := range params[0 : plen-1] {
|
||||||
switch param := iparam.(type) {
|
switch param := iparam.(type) {
|
||||||
case *UploadFile:
|
case *UploadFile:
|
||||||
@@ -32,6 +35,11 @@ func createMultipart(postParams IBody, params []interface{}) {
|
|||||||
param.FieldName = "file0"
|
param.FieldName = "file0"
|
||||||
}
|
}
|
||||||
writeFormUploadFile(mwriter, param)
|
writeFormUploadFile(mwriter, param)
|
||||||
|
case UploadFile:
|
||||||
|
if param.FieldName == "" {
|
||||||
|
param.FieldName = "file0"
|
||||||
|
}
|
||||||
|
writeFormUploadFile(mwriter, ¶m)
|
||||||
case []*UploadFile:
|
case []*UploadFile:
|
||||||
for i, p := range param {
|
for i, p := range param {
|
||||||
if p.FieldName == "" {
|
if p.FieldName == "" {
|
||||||
@@ -39,6 +47,13 @@ func createMultipart(postParams IBody, params []interface{}) {
|
|||||||
}
|
}
|
||||||
writeFormUploadFile(mwriter, p)
|
writeFormUploadFile(mwriter, p)
|
||||||
}
|
}
|
||||||
|
case []UploadFile:
|
||||||
|
for i, p := range param {
|
||||||
|
if p.FieldName == "" {
|
||||||
|
p.FieldName = "file" + strconv.Itoa(i)
|
||||||
|
}
|
||||||
|
writeFormUploadFile(mwriter, &p)
|
||||||
|
}
|
||||||
case string:
|
case string:
|
||||||
uploadFiles, err := UploadFileFromGlob(param)
|
uploadFiles, err := UploadFileFromGlob(param)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -60,7 +75,7 @@ func createMultipart(postParams IBody, params []interface{}) {
|
|||||||
} else {
|
} else {
|
||||||
for ii, p := range uploadFiles {
|
for ii, p := range uploadFiles {
|
||||||
if p.FieldName == "" {
|
if p.FieldName == "" {
|
||||||
p.FieldName = "file" + strconv.Itoa(ii) + "_" + strconv.Itoa(i)
|
p.FieldName = "file" + strconv.Itoa(i) + "_" + strconv.Itoa(ii)
|
||||||
}
|
}
|
||||||
writeFormUploadFile(mwriter, p)
|
writeFormUploadFile(mwriter, p)
|
||||||
}
|
}
|
||||||
@@ -87,4 +102,9 @@ func createMultipart(postParams IBody, params []interface{}) {
|
|||||||
|
|
||||||
postParams.AddContentType(mwriter.FormDataContentType())
|
postParams.AddContentType(mwriter.FormDataContentType())
|
||||||
postParams.SetIOBody(body)
|
postParams.SetIOBody(body)
|
||||||
|
|
||||||
|
err := mwriter.Close()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
88
response.go
88
response.go
@@ -9,52 +9,78 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Response 响应内容包含http.Response
|
// Response 响应内容包含http.Response 已读
|
||||||
type Response struct {
|
type Response struct {
|
||||||
DContent string
|
readContent string
|
||||||
GResponse *http.Response
|
readResponse *http.Response
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromHTTPResponse 生成Response 从标准http.Response
|
// FromHTTPResponse 生成Response 从标准http.Response
|
||||||
func FromHTTPResponse(resp *http.Response) (*Response, error) {
|
func FromHTTPResponse(resp *http.Response, isDecompressNoAccept bool) (*Response, error) {
|
||||||
|
var err error
|
||||||
// 复制response 返回内容 并且测试是否有解压的需求
|
// 复制response 返回内容 并且测试是否有解压的需求
|
||||||
srcbuf, err := ioutil.ReadAll(resp.Body)
|
srcbuf, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
cbuf := bytes.NewBuffer([]byte{})
|
content := ""
|
||||||
_, err = io.Copy(cbuf, bytes.NewReader(srcbuf))
|
|
||||||
|
|
||||||
if err != nil {
|
if isDecompressNoAccept { // 在某个已经遗忘的网页测试过, 为了兼容 Python requests
|
||||||
panic(err)
|
srcReader := bytes.NewReader(srcbuf)
|
||||||
|
var reader io.ReadCloser
|
||||||
|
if reader, err = gzip.NewReader(srcReader); err == nil {
|
||||||
|
defer reader.Close()
|
||||||
|
buf, 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)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
content = string(buf)
|
||||||
|
} else {
|
||||||
|
content = string(srcbuf)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
content = string(srcbuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.Body = ioutil.NopCloser(cbuf)
|
return &Response{readContent: content, readResponse: resp}, nil
|
||||||
|
|
||||||
content := string(srcbuf)
|
|
||||||
srcReader := bytes.NewReader(srcbuf)
|
|
||||||
|
|
||||||
if r, err := gzip.NewReader(srcReader); err == nil {
|
|
||||||
buf, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
content = string(buf)
|
|
||||||
} else if r, err := zlib.NewReader(srcReader); err == nil {
|
|
||||||
buf, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
content = string(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Response{DContent: content, GResponse: resp}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Content 返回解压后的内容
|
// Content 返回解压后的内容
|
||||||
func (gresp *Response) Content() string {
|
func (gresp *Response) Content() string {
|
||||||
return gresp.DContent
|
return gresp.readContent
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSrcResponse 获取原生golang http.Response
|
||||||
|
func (gresp *Response) GetSrcResponse() *http.Response {
|
||||||
|
return gresp.readResponse
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStatue 获取Statue String
|
||||||
|
func (gresp *Response) GetStatue() string {
|
||||||
|
return gresp.readResponse.Status
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStatueCode 获取Statue int
|
||||||
|
func (gresp *Response) GetStatueCode() int {
|
||||||
|
return gresp.readResponse.StatusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHeader Header map[string][]string
|
||||||
|
func (gresp *Response) GetHeader() http.Header {
|
||||||
|
return gresp.readResponse.Header
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContentLength 获取Content的内容长度, 如果存在 IsDecompressNoAccept 可能是压缩级别的长度, 非GetContent长度
|
||||||
|
func (gresp *Response) GetContentLength() int64 {
|
||||||
|
return gresp.readResponse.ContentLength
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,62 @@
|
|||||||
package requests
|
package requests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/474420502/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTest(t *testing.T) {
|
func TestFromHTTPResponse(t *testing.T) {
|
||||||
|
|
||||||
|
var gresp *http.Response
|
||||||
|
var err error
|
||||||
|
gresp, err = http.DefaultClient.Get("http://httpbin.org/get")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
resp, err := FromHTTPResponse(gresp, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "headers.Host").String() != "httpbin.org" {
|
||||||
|
t.Error("headers.Host != httpbin.org ?")
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.GetSrcResponse().StatusCode != 200 {
|
||||||
|
t.Error("StatusCode != 200")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(resp.GetSrcResponse().Header) == 0 {
|
||||||
|
t.Error("esp.GetSrcResponse().Header == nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.GetStatue() != "200 OK" || resp.GetStatueCode() != 200 {
|
||||||
|
t.Error(" resp.GetStatue() != 200 OK")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(resp.GetHeader()["Content-Length"]) != 1 {
|
||||||
|
t.Error("resp.GetHeader() is error ?")
|
||||||
|
}
|
||||||
|
|
||||||
|
if int64(len(resp.Content())) != resp.GetContentLength() {
|
||||||
|
t.Error("content len is not equal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResponseDeflate(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
if wf := ses.Get("http://httpbin.org/get"); wf != nil {
|
||||||
|
wf.AddHeader("accept-encoding", "deflate")
|
||||||
|
resp, err := wf.Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
if gjson.Get(resp.Content(), "headers.Accept-Encoding").String() != "deflate" {
|
||||||
|
t.Error("Accept-Encoding != deflate ?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
102
session.go
102
session.go
@@ -36,8 +36,8 @@ func (body *Body) SetIOBody(iobody interface{}) {
|
|||||||
body.ioBody = iobody
|
body.ioBody = iobody
|
||||||
}
|
}
|
||||||
|
|
||||||
// IOBody 获取ioBody值
|
// GetIOBody 获取ioBody值
|
||||||
func (body *Body) IOBody() interface{} {
|
func (body *Body) GetIOBody() interface{} {
|
||||||
return body.ioBody
|
return body.ioBody
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,16 +70,15 @@ func (body *Body) AddContentType(ct string) {
|
|||||||
|
|
||||||
// IBody 相关参数结构
|
// IBody 相关参数结构
|
||||||
type IBody interface {
|
type IBody interface {
|
||||||
|
// GetIOBody 获取iobody data
|
||||||
// Query map[string][]string
|
GetIOBody() interface{}
|
||||||
IOBody() interface{}
|
// SetIOBody 设置iobody data
|
||||||
// SetIOBody
|
|
||||||
SetIOBody(iobody interface{})
|
SetIOBody(iobody interface{})
|
||||||
// Files []UploadFile
|
// ContentType 返回包括 Prefix 所有的ContentType
|
||||||
ContentType() string
|
ContentType() string
|
||||||
// AppendContent
|
// AppendContent
|
||||||
AddContentType(ct string)
|
AddContentType(ct string)
|
||||||
// AddPrefix 添加 Prefix
|
// SetPrefix 设置 Prefix; 唯一前缀; 就是ContentType的第一个, ContentType(Prefix);ContentType;ContentType
|
||||||
SetPrefix(ct string)
|
SetPrefix(ct string)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +90,11 @@ type BasicAuth struct {
|
|||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsSetting 是否设置的一些情景
|
||||||
|
type IsSetting struct {
|
||||||
|
isDecompressNoAccept bool
|
||||||
|
}
|
||||||
|
|
||||||
// Session 的基本方法
|
// Session 的基本方法
|
||||||
type Session struct {
|
type Session struct {
|
||||||
auth *BasicAuth
|
auth *BasicAuth
|
||||||
@@ -104,6 +108,8 @@ type Session struct {
|
|||||||
|
|
||||||
Header http.Header
|
Header http.Header
|
||||||
Query url.Values
|
Query url.Values
|
||||||
|
|
||||||
|
Is IsSetting
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -128,7 +134,7 @@ const (
|
|||||||
// TypeStream application/octet-stream 只能提交一个二进制流, 很少用
|
// TypeStream application/octet-stream 只能提交一个二进制流, 很少用
|
||||||
TypeStream = "application/octet-stream"
|
TypeStream = "application/octet-stream"
|
||||||
|
|
||||||
// TypeFormData 类型
|
// TypeFormData 类型 Upload File 支持path(string) 自动转换成UploadFile
|
||||||
TypeFormData = "multipart/form-data"
|
TypeFormData = "multipart/form-data"
|
||||||
|
|
||||||
// TypeMixed Mixed类型
|
// TypeMixed Mixed类型
|
||||||
@@ -155,6 +161,9 @@ const (
|
|||||||
// CDialTimeout 一个Connect过程的Timeout
|
// CDialTimeout 一个Connect过程的Timeout
|
||||||
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
CDialTimeout // 支持time.Duration 和 int(秒为单位)
|
||||||
|
|
||||||
|
// CKeepAlives 默认KeepAlives false, 如果默认为true容易被一直KeepAlives, 没关闭链接
|
||||||
|
CKeepAlives
|
||||||
|
|
||||||
// CProxy 代理链接
|
// CProxy 代理链接
|
||||||
CProxy // http, https, socks5
|
CProxy // http, https, socks5
|
||||||
|
|
||||||
@@ -167,14 +176,20 @@ const (
|
|||||||
// CTLS 帐号认证
|
// CTLS 帐号认证
|
||||||
CTLS // user pwd
|
CTLS // user pwd
|
||||||
|
|
||||||
// CCookiejar 持久化 CookieJar
|
// CIsWithCookiejar 持久化 CookieJar true or false ; default = true
|
||||||
CCookiejar // true or false ; default = true
|
CIsWithCookiejar
|
||||||
|
|
||||||
|
// CIsDecompressNoAccept 解压 当response header 不存在 Accept-Encoding
|
||||||
|
// 很多特殊情景会不返回Accept-Encoding: Gzip. 如 不按照标准的网站
|
||||||
|
CIsDecompressNoAccept
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewSession 创建Session
|
// NewSession 创建Session
|
||||||
func NewSession() *Session {
|
func NewSession() *Session {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
transport := &http.Transport{DisableCompression: true}
|
transport := &http.Transport{DisableCompression: true, DisableKeepAlives: true}
|
||||||
|
|
||||||
|
EnsureTransporterFinalized(transport)
|
||||||
|
|
||||||
client.Transport = transport
|
client.Transport = transport
|
||||||
cjar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
cjar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||||
@@ -183,7 +198,7 @@ func NewSession() *Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.Jar = cjar
|
client.Jar = cjar
|
||||||
return &Session{client: client, body: NewBody(), transport: transport, auth: nil, cookiejar: client.Jar, Header: make(http.Header)}
|
return &Session{client: client, body: NewBody(), transport: transport, auth: nil, cookiejar: client.Jar, Header: make(http.Header), Is: IsSetting{false}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConfig 设置配置
|
// SetConfig 设置配置
|
||||||
@@ -206,8 +221,13 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
|||||||
panic(errors.New("error type " + reflect.TypeOf(v).String()))
|
panic(errors.New("error type " + reflect.TypeOf(v).String()))
|
||||||
}
|
}
|
||||||
case CDialTimeout:
|
case CDialTimeout:
|
||||||
// 没时间实现这些小细节
|
// TODO: CDialTimeout CRequestTimeout 与 细节
|
||||||
case CCookiejar:
|
case CIsDecompressNoAccept:
|
||||||
|
ses.Is.isDecompressNoAccept = values.(bool)
|
||||||
|
case CKeepAlives:
|
||||||
|
// println(ses.transport.DisableKeepAlives)
|
||||||
|
ses.transport.DisableKeepAlives = !values.(bool)
|
||||||
|
case CIsWithCookiejar:
|
||||||
v := values.(bool)
|
v := values.(bool)
|
||||||
if v {
|
if v {
|
||||||
if ses.client.Jar == nil {
|
if ses.client.Jar == nil {
|
||||||
@@ -230,7 +250,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
|||||||
ses.transport.Proxy = nil
|
ses.transport.Proxy = nil
|
||||||
}
|
}
|
||||||
case CInsecure:
|
case CInsecure:
|
||||||
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)}
|
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: values.(bool)}
|
||||||
case CTLS:
|
case CTLS:
|
||||||
ses.transport.TLSClientConfig = values.(*tls.Config)
|
ses.transport.TLSClientConfig = values.(*tls.Config)
|
||||||
case CBasicAuth:
|
case CBasicAuth:
|
||||||
@@ -241,13 +261,13 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
|
|||||||
switch v := values.(type) {
|
switch v := values.(type) {
|
||||||
case *BasicAuth:
|
case *BasicAuth:
|
||||||
ses.auth.User = v.User
|
ses.auth.User = v.User
|
||||||
ses.auth.User = v.Password
|
ses.auth.Password = v.Password
|
||||||
case BasicAuth:
|
case BasicAuth:
|
||||||
ses.auth.User = v.User
|
ses.auth.User = v.User
|
||||||
ses.auth.User = v.Password
|
ses.auth.Password = v.Password
|
||||||
case []string:
|
case []string:
|
||||||
ses.auth.User = v[0]
|
ses.auth.User = v[0]
|
||||||
ses.auth.User = v[1]
|
ses.auth.Password = v[1]
|
||||||
case nil:
|
case nil:
|
||||||
ses.auth = nil
|
ses.auth = nil
|
||||||
}
|
}
|
||||||
@@ -267,7 +287,7 @@ func (ses *Session) GetQuery() url.Values {
|
|||||||
return ses.Query
|
return ses.Query
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHeader 设置set Header的值
|
// SetHeader 设置set Header的值, 必须符合规范 HaHa -> Haha 如果真要HaHa,只能这样 Ha-Ha
|
||||||
func (ses *Session) SetHeader(header http.Header) {
|
func (ses *Session) SetHeader(header http.Header) {
|
||||||
ses.Header = header
|
ses.Header = header
|
||||||
}
|
}
|
||||||
@@ -282,8 +302,8 @@ func (ses *Session) SetCookies(u *url.URL, cookies []*http.Cookie) {
|
|||||||
ses.cookiejar.SetCookies(u, cookies)
|
ses.cookiejar.SetCookies(u, cookies)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cookies 返回 Cookies
|
// GetCookies 返回 Cookies
|
||||||
func (ses *Session) Cookies(u *url.URL) []*http.Cookie {
|
func (ses *Session) GetCookies(u *url.URL) []*http.Cookie {
|
||||||
return ses.cookiejar.Cookies(u)
|
return ses.cookiejar.Cookies(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,6 +313,7 @@ func (ses *Session) DelCookies(u *url.URL, name string) {
|
|||||||
for _, c := range cookies {
|
for _, c := range cookies {
|
||||||
if c.Name == name {
|
if c.Name == name {
|
||||||
c.MaxAge = -1
|
c.MaxAge = -1
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ses.SetCookies(u, cookies)
|
ses.SetCookies(u, cookies)
|
||||||
@@ -308,6 +329,13 @@ func (ses *Session) ClearCookies() {
|
|||||||
ses.client.Jar = ses.cookiejar
|
ses.client.Jar = ses.cookiejar
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Head 请求
|
||||||
|
func (ses *Session) Head(url string) *Workflow {
|
||||||
|
wf := NewWorkflow(ses, url)
|
||||||
|
wf.Method = "HEAD"
|
||||||
|
return wf
|
||||||
|
}
|
||||||
|
|
||||||
// Get 请求
|
// Get 请求
|
||||||
func (ses *Session) Get(url string) *Workflow {
|
func (ses *Session) Get(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses, url)
|
wf := NewWorkflow(ses, url)
|
||||||
@@ -336,20 +364,6 @@ func (ses *Session) Patch(url string) *Workflow {
|
|||||||
return wf
|
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 请求
|
// Options 请求
|
||||||
func (ses *Session) Options(url string) *Workflow {
|
func (ses *Session) Options(url string) *Workflow {
|
||||||
wf := NewWorkflow(ses, url)
|
wf := NewWorkflow(ses, url)
|
||||||
@@ -357,16 +371,22 @@ func (ses *Session) Options(url string) *Workflow {
|
|||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseIdleConnections closes the idle connections that a session client may make use of
|
// Delete 请求
|
||||||
// 从levigross/grequests 借鉴
|
func (ses *Session) Delete(url string) *Workflow {
|
||||||
func (ses *Session) CloseIdleConnections() {
|
wf := NewWorkflow(ses, url)
|
||||||
ses.client.Transport.(*http.Transport).CloseIdleConnections()
|
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
|
// EnsureTransporterFinalized will ensure that when the HTTP client is GCed
|
||||||
// the runtime will close the idle connections (so that they won't leak)
|
// the runtime will close the idle connections (so that they won't leak)
|
||||||
// this function was adopted from Hashicorp's go-cleanhttp package
|
// this function was adopted from Hashicorp's go-cleanhttp package
|
||||||
// 暂时不用, 标记到以后是否起作用
|
|
||||||
func EnsureTransporterFinalized(httpTransport *http.Transport) {
|
func EnsureTransporterFinalized(httpTransport *http.Transport) {
|
||||||
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
|
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
|
||||||
(*transportInt).CloseIdleConnections()
|
(*transportInt).CloseIdleConnections()
|
||||||
|
|||||||
221
session_test.go
221
session_test.go
@@ -1,15 +1,15 @@
|
|||||||
package requests
|
package requests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
"time"
|
||||||
|
|
||||||
func init() {
|
"github.com/474420502/gjson"
|
||||||
log.SetFlags(log.Lshortfile | log.LstdFlags)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewSession(t *testing.T) {
|
func TestNewSession(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
@@ -45,7 +45,7 @@ func TestSession_Get(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if len(resp.Content()) <= 200 {
|
if len(resp.Content()) <= 150 {
|
||||||
t.Error(resp.Content())
|
t.Error(resp.Content())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -76,14 +76,14 @@ func TestSession_Post(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params...).Execute()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Metchod error = %v", err)
|
t.Errorf("Metchod error = %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tt.want.MatchString(got.DContent) == false {
|
if tt.want.MatchString(got.readContent) == false {
|
||||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
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) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
|
|
||||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params...).Execute()
|
got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params...).Execute()
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("Metchod error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tt.want.MatchString(got.DContent) == false {
|
if tt.want.MatchString(got.readContent) == false {
|
||||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -169,14 +169,14 @@ func TestSession_PostUploadFile(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
got, err := ses.Post("http://httpbin.org/post").SetBody(tt.args.params, TypeFormData).Execute()
|
got, err := ses.Post("http://httpbin.org/post").SetBodyAuto(tt.args.params, TypeFormData).Execute()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Metchod error = %v", err)
|
t.Errorf("Metchod error = %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tt.want.MatchString(got.DContent) == false {
|
if tt.want.MatchString(got.readContent) == false {
|
||||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,14 +213,14 @@ func TestSession_Put(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
got, err := ses.Put("http://httpbin.org/put").SetBody(tt.args.params, TypeFormData).Execute()
|
got, err := ses.Put("http://httpbin.org/put").SetBodyAuto(tt.args.params, TypeFormData).Execute()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Metchod error = %v", err)
|
t.Errorf("Metchod error = %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tt.want.MatchString(got.DContent) == false {
|
if tt.want.MatchString(got.readContent) == false {
|
||||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,14 +257,14 @@ func TestSession_Patch(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
got, err := ses.Patch("http://httpbin.org/patch").SetBody(tt.args.params, TypeFormData).Execute()
|
got, err := ses.Patch("http://httpbin.org/patch").SetBodyAuto(tt.args.params, TypeFormData).Execute()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Metchod error = %v", err)
|
t.Errorf("Metchod error = %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if tt.want.MatchString(got.DContent) == false {
|
if tt.want.MatchString(got.readContent) == false {
|
||||||
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
t.Errorf("Metchod = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ func TestSession_SetConfig(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "test timeout",
|
name: "test timeout",
|
||||||
args: args{typeConfig: CRequestTimeout, values: 0.01},
|
args: args{typeConfig: CRequestTimeout, values: 0.0001},
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -297,7 +297,7 @@ func TestSession_SetConfig(t *testing.T) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
name: "test proxy",
|
name: "test proxy",
|
||||||
args: args{typeConfig: CProxy, values: "http://474420502.top:7070"},
|
args: args{typeConfig: CProxy, values: "http://" + ProxyAddress},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -305,7 +305,7 @@ func TestSession_SetConfig(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
ses.SetConfig(tt.args.typeConfig, tt.args.values)
|
ses.SetConfig(tt.args.typeConfig, tt.args.values)
|
||||||
_, err := ses.Get("https://httpbin.org/get").Execute()
|
_, err := ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("Metchod error = %v", err)
|
t.Errorf("Metchod error = %v", err)
|
||||||
@@ -319,7 +319,7 @@ func TestSession_SetConfig(t *testing.T) {
|
|||||||
func TestSession_SetConfigInsecure(t *testing.T) {
|
func TestSession_SetConfigInsecure(t *testing.T) {
|
||||||
|
|
||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
ses.SetConfig(CInsecure, false)
|
ses.SetConfig(CInsecure, true)
|
||||||
|
|
||||||
for _, badSSL := range []string{
|
for _, badSSL := range []string{
|
||||||
"https://self-signed.badssl.com/",
|
"https://self-signed.badssl.com/",
|
||||||
@@ -330,8 +330,8 @@ func TestSession_SetConfigInsecure(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Unable to make request", err)
|
t.Error("Unable to make request", err)
|
||||||
}
|
}
|
||||||
if resp.GResponse.StatusCode != 200 {
|
if resp.readResponse.StatusCode != 200 {
|
||||||
t.Error("Request did not return OK, is ", resp.GResponse.StatusCode)
|
t.Error("Request did not return OK, is ", resp.readResponse.StatusCode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,14 +341,13 @@ func TestSession_Cookies(t *testing.T) {
|
|||||||
ses := NewSession()
|
ses := NewSession()
|
||||||
|
|
||||||
t.Run("set cookie", func(t *testing.T) {
|
t.Run("set cookie", func(t *testing.T) {
|
||||||
|
|
||||||
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
resp, err := ses.Get("http://httpbin.org/cookies/set").AddKVCookie("a", "1").Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("cookies set error", err)
|
t.Error("cookies set error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.DContent) {
|
if !regexp.MustCompile(`"a": "1"`).MatchString(resp.readContent) {
|
||||||
t.Error(resp.DContent)
|
t.Error(resp.readContent)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -365,8 +364,8 @@ func TestSession_Header(t *testing.T) {
|
|||||||
t.Error("cookies set error", err)
|
t.Error("cookies set error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resp.DContent) <= 5000 {
|
if len(resp.readContent) <= 5000 {
|
||||||
t.Error(resp.DContent, len(resp.DContent))
|
t.Error(resp.readContent, len(resp.readContent))
|
||||||
}
|
}
|
||||||
|
|
||||||
ses = NewSession()
|
ses = NewSession()
|
||||||
@@ -375,8 +374,172 @@ func TestSession_Header(t *testing.T) {
|
|||||||
t.Error("cookies set error", err)
|
t.Error("cookies set error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resp.DContent) <= 5000 {
|
if len(resp.readContent) <= 5000 {
|
||||||
t.Error(resp.DContent, len(resp.DContent))
|
t.Error(resp.readContent, len(resp.readContent))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSession_ConfigEx(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
ses.SetConfig(CRequestTimeout, time.Microsecond)
|
||||||
|
resp, err := ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Error(resp)
|
||||||
|
} else {
|
||||||
|
if strings.LastIndex(err.Error(), "Client.Timeout exceeded while awaiting headers") < 0 {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.SetConfig(CRequestTimeout, float32(0.0000001))
|
||||||
|
resp, err = ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Error(resp)
|
||||||
|
} else {
|
||||||
|
if strings.LastIndex(err.Error(), "Client.Timeout exceeded while awaiting headers") < 0 {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.SetConfig(CKeepAlives, true)
|
||||||
|
ses.SetConfig(CRequestTimeout, int64(5))
|
||||||
|
// jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||||
|
u, err := url.Parse("http://httpbin.org")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
// jar.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}})
|
||||||
|
ses.SetConfig(CIsWithCookiejar, false)
|
||||||
|
ses.SetConfig(CIsWithCookiejar, true)
|
||||||
|
ses.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}, &http.Cookie{Name: "eson", Value: "bad"}})
|
||||||
|
resp, err = ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "headers.Cookie").String() != "Request=Cookiejar; eson=bad" {
|
||||||
|
t.Error(resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.GetSrcResponse().Header["Connection"][0] != "keep-alive" {
|
||||||
|
t.Error("CKeepAlive is error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.SetConfig(CProxy, nil)
|
||||||
|
if u, err := url.Parse("http://" + ProxyAddress); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
ses.SetConfig(CProxy, u)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.DelCookies(u, "eson")
|
||||||
|
resp, err = ses.Get("http://httpbin.org/cookies").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
cookies := ses.GetCookies(u)
|
||||||
|
if len(cookies) != 1 && cookies[0].String() != "Request=Cookiejar" {
|
||||||
|
t.Error("cookies del get error please check it")
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.ClearCookies()
|
||||||
|
resp, err = ses.Get("http://httpbin.org/cookies").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if gjson.Get(resp.Content(), "cookies").String() != "{}" {
|
||||||
|
t.Error(resp.Content())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSession_SetQuery(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
ses.SetQuery(url.Values{"query": []string{"a", "b"}})
|
||||||
|
resp, err := ses.Get("http://httpbin.org/get").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
query := gjson.Get(resp.Content(), "args.query").Array()
|
||||||
|
for _, q := range query {
|
||||||
|
if !(q.String() == "a" || q.String() == "b") {
|
||||||
|
t.Error("query error, ", resp.Content())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSession_SetHeader(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
var header http.Header
|
||||||
|
header = make(http.Header)
|
||||||
|
header["xx-xx"] = []string{"Header"}
|
||||||
|
ses.SetHeader(header)
|
||||||
|
|
||||||
|
resp, err := ses.Get("http://httpbin.org/headers").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "headers.Xx-Xx").String() != "Header" {
|
||||||
|
t.Error("Xx-Xx is not exists", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if ses.GetHeader()["xx-xx"][0] != "Header" {
|
||||||
|
t.Error("header error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSession_SetBasicAuth(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
ses.SetConfig(CBasicAuth, &BasicAuth{User: "eson", Password: "123456"})
|
||||||
|
resp, err := ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if resp.GetSrcResponse().StatusCode != 200 {
|
||||||
|
t.Error("code != 200, code = ", resp.GetStatue())
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.SetConfig(CBasicAuth, BasicAuth{User: "eson", Password: "12345"})
|
||||||
|
resp, err = ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.GetSrcResponse().StatusCode != 401 {
|
||||||
|
t.Error("code != 401, code = ", resp.GetStatue())
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.GetSrcResponse().StatusCode != 401 {
|
||||||
|
t.Error("code != 401, code = ", resp.GetStatue())
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.SetConfig(CBasicAuth, []string{"son", "123456"})
|
||||||
|
resp, err = ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if resp.GetSrcResponse().StatusCode != 401 {
|
||||||
|
t.Error("code != 401, code = ", resp.GetStatue())
|
||||||
|
}
|
||||||
|
|
||||||
|
ses.SetConfig(CBasicAuth, nil)
|
||||||
|
resp, err = ses.Get("http://httpbin.org/basic-auth/eson/123456").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if resp.GetSrcResponse().StatusCode != 401 {
|
||||||
|
t.Error("code != 401, code = ", resp.GetStatue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,16 +14,59 @@ type UploadFile struct {
|
|||||||
FileReaderCloser io.ReadCloser
|
FileReaderCloser io.ReadCloser
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetFileName 设置FileName属性
|
||||||
|
func (ufile *UploadFile) SetFileName(filename string) {
|
||||||
|
ufile.FileName = filename
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFileName 设置FileName属性
|
||||||
|
func (ufile *UploadFile) GetFileName() string {
|
||||||
|
return ufile.FileName
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFileReaderCloser 设置FileName属性
|
||||||
|
func (ufile *UploadFile) SetFileReaderCloser(readerCloser io.ReadCloser) {
|
||||||
|
ufile.FileReaderCloser = readerCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFileReaderCloserFromFile 设置FileName属性
|
||||||
|
func (ufile *UploadFile) SetFileReaderCloserFromFile(filename string) error {
|
||||||
|
fd, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ufile.SetFileReaderCloser(fd)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFileReaderCloser 设置FileName属性
|
||||||
|
func (ufile *UploadFile) GetFileReaderCloser() io.ReadCloser {
|
||||||
|
return ufile.FileReaderCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFieldName 设置FileName属性
|
||||||
|
func (ufile *UploadFile) SetFieldName(fieldname string) {
|
||||||
|
ufile.FieldName = fieldname
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFieldName 设置FileName属性
|
||||||
|
func (ufile *UploadFile) GetFieldName() string {
|
||||||
|
return ufile.FieldName
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUploadFile 创建一个空的UploadFile, 必须设置 FileName FieldName FileReaderCloser 三个属性
|
||||||
|
func NewUploadFile() *UploadFile {
|
||||||
|
return &UploadFile{}
|
||||||
|
}
|
||||||
|
|
||||||
// UploadFileFromPath 从本地文件获取上传文件
|
// UploadFileFromPath 从本地文件获取上传文件
|
||||||
func UploadFileFromPath(fileName string) (*UploadFile, error) {
|
func UploadFileFromPath(fileName string) (*UploadFile, error) {
|
||||||
fd, err := os.Open(fileName)
|
fd, err := os.Open(fileName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil
|
return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadFileFromGlob 根据Glob从本地文件获取上传文件
|
// UploadFileFromGlob 根据Glob从本地文件获取上传文件
|
||||||
|
|||||||
107
upload_file_test.go
Normal file
107
upload_file_test.go
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package requests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/474420502/gjson"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUploadFile(t *testing.T) {
|
||||||
|
|
||||||
|
for i := 0; i < 1; i++ {
|
||||||
|
|
||||||
|
ses := NewSession()
|
||||||
|
wf := ses.Put("http://httpbin.org/put")
|
||||||
|
|
||||||
|
ufile, err := UploadFileFromPath("tests/json.file")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
wf.SetBodyAuto(ufile, TypeFormData)
|
||||||
|
resp, _ := wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["file0"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
ses = NewSession()
|
||||||
|
wf = ses.Patch("http://httpbin.org/patch")
|
||||||
|
|
||||||
|
wf.SetBodyAuto("tests/json.file", TypeFormData)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["file0"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
ses = NewSession()
|
||||||
|
wf = ses.Delete("http://httpbin.org/delete")
|
||||||
|
ufile = NewUploadFile()
|
||||||
|
ufile.SetFileName("MyFile")
|
||||||
|
ufile.SetFieldName("MyField")
|
||||||
|
ufile.SetFileReaderCloserFromFile("tests/json.file")
|
||||||
|
wf.SetBodyAuto(ufile)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["MyField"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ses = NewSession()
|
||||||
|
// wf = ses.Put("http://httpbin.org/put")
|
||||||
|
|
||||||
|
ufile.SetFileReaderCloserFromFile("tests/json.file")
|
||||||
|
wf.SetBodyAuto(*ufile)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["MyField"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ses = NewSession()
|
||||||
|
// wf = ses.Put("http://httpbin.org/put")
|
||||||
|
|
||||||
|
ufile = NewUploadFile()
|
||||||
|
ufile.SetFileName("MyFile")
|
||||||
|
ufile.SetFileReaderCloserFromFile("tests/json.file")
|
||||||
|
wf.SetBodyAuto(ufile)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["file0"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
ufile.SetFileReaderCloserFromFile("tests/json.file")
|
||||||
|
wf.SetBodyAuto(*ufile)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["file0"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
var ufileList []*UploadFile
|
||||||
|
ufile, err = UploadFileFromPath("tests/json.file")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
ufileList = append(ufileList, ufile)
|
||||||
|
ufile, err = UploadFileFromPath("tests/learn.js")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
ufileList = append(ufileList, ufile)
|
||||||
|
wf.SetBodyAuto(ufileList)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["file1"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if wf.GetBody().ContentType() != "" {
|
||||||
|
t.Error("Body is not Clear")
|
||||||
|
}
|
||||||
|
|
||||||
|
wf.SetBodyAuto([]string{"tests/learn.js", "tests/json.file"}, TypeFormData)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["file1_0"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
if _, ok := gjson.Get(resp.Content(), "files").Map()["file0_0"]; !ok {
|
||||||
|
t.Error("file error", resp.Content())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
92
workflow.go
92
workflow.go
@@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Workflow 工作流
|
// Workflow 工作流 设计点: 这个并不影响Session的属性变化 如 NewWorkflow(ses, url).AddHeader() 对ses没影响
|
||||||
type Workflow struct {
|
type Workflow struct {
|
||||||
session *Session
|
session *Session
|
||||||
ParsedURL *url.URL
|
ParsedURL *url.URL
|
||||||
@@ -18,11 +18,10 @@ type Workflow struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewWorkflow new and init workflow
|
// NewWorkflow new and init workflow
|
||||||
func NewWorkflow(ses *Session, u string) *Workflow {
|
func NewWorkflow(ses *Session, urlstr string) *Workflow {
|
||||||
wf := &Workflow{}
|
wf := &Workflow{}
|
||||||
wf.SwitchSession(ses)
|
wf.SwitchSession(ses)
|
||||||
|
wf.SetRawURL(urlstr)
|
||||||
wf.SetRawURL(u)
|
|
||||||
|
|
||||||
wf.Body = NewBody()
|
wf.Body = NewBody()
|
||||||
wf.Header = make(http.Header)
|
wf.Header = make(http.Header)
|
||||||
@@ -35,15 +34,22 @@ func (wf *Workflow) SwitchSession(ses *Session) {
|
|||||||
wf.session = ses
|
wf.session = ses
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddHeader 添加头信息 Get方法从Header参数上获取
|
// AddHeader 添加头信息 Get方法从Header参数上获取 必须符合规范 HaHa -> Haha 如果真要HaHa,只能这样 Ha-Ha
|
||||||
func (wf *Workflow) AddHeader(key, value string) *Workflow {
|
func (wf *Workflow) AddHeader(key, value string) *Workflow {
|
||||||
wf.Header.Add(key, value)
|
wf.Header[key] = append(wf.Header[key], value)
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHeader 设置头信息 这样构造 []string{value} Get方法从Header参数上获取
|
// SetHeader 设置完全替换原有Header 必须符合规范 HaHa -> Haha 如果真要HaHa,只能这样 Ha-Ha
|
||||||
func (wf *Workflow) SetHeader(key, value string) *Workflow {
|
func (wf *Workflow) SetHeader(header http.Header) *Workflow {
|
||||||
wf.Header.Set(key, value)
|
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
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +75,14 @@ func (wf *Workflow) AddCookie(c *http.Cookie) *Workflow {
|
|||||||
return wf
|
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
|
// AddKVCookie 添加 以 key value 的 Cookie
|
||||||
func (wf *Workflow) AddKVCookie(name, value string) *Workflow {
|
func (wf *Workflow) AddKVCookie(name, value string) *Workflow {
|
||||||
wf.Cookies[name] = &http.Cookie{Name: name, Value: value}
|
wf.Cookies[name] = &http.Cookie{Name: name, Value: value}
|
||||||
@@ -147,7 +161,7 @@ func (wf *Workflow) SetQuery(query url.Values) *Workflow {
|
|||||||
|
|
||||||
var regexGetPath = regexp.MustCompile("/[^/]*")
|
var regexGetPath = regexp.MustCompile("/[^/]*")
|
||||||
|
|
||||||
// GetURLPath 获取Path参数
|
// GetURLPath 获取Path参数 http://localhost/anything/user/pwd return [/anything /user /pwd]
|
||||||
func (wf *Workflow) GetURLPath() []string {
|
func (wf *Workflow) GetURLPath() []string {
|
||||||
return regexGetPath.FindAllString(wf.ParsedURL.Path, -1)
|
return regexGetPath.FindAllString(wf.ParsedURL.Path, -1)
|
||||||
}
|
}
|
||||||
@@ -169,7 +183,7 @@ func encodePath(path []string) string {
|
|||||||
return rawpath
|
return rawpath
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetURLPath 设置Path参数
|
// SetURLPath 设置Path参数 对应 GetURLPath
|
||||||
func (wf *Workflow) SetURLPath(path []string) *Workflow {
|
func (wf *Workflow) SetURLPath(path []string) *Workflow {
|
||||||
if path == nil {
|
if path == nil {
|
||||||
return wf
|
return wf
|
||||||
@@ -178,14 +192,29 @@ func (wf *Workflow) SetURLPath(path []string) *Workflow {
|
|||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetURLRawPath 设置Pa晚上参数
|
// SetURLRawPath 设置 参数 eg. /get = http:// hostname + /get
|
||||||
func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
func (wf *Workflow) SetURLRawPath(path string) *Workflow {
|
||||||
wf.ParsedURL.Path = path
|
if path[0] != '/' {
|
||||||
|
wf.ParsedURL.Path = "/" + path
|
||||||
|
} else {
|
||||||
|
wf.ParsedURL.Path = path
|
||||||
|
}
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBody 参数设置
|
// 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() IBody {
|
||||||
|
return wf.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBodyAuto 参数设置
|
||||||
|
func (wf *Workflow) SetBodyAuto(params ...interface{}) *Workflow {
|
||||||
|
|
||||||
if params != nil {
|
if params != nil {
|
||||||
plen := len(params)
|
plen := len(params)
|
||||||
@@ -194,14 +223,14 @@ func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
|
|||||||
if plen >= 2 {
|
if plen >= 2 {
|
||||||
t := params[plen-1]
|
t := params[plen-1]
|
||||||
defaultContentType = t.(string)
|
defaultContentType = t.(string)
|
||||||
wf.Body.SetPrefix(defaultContentType)
|
|
||||||
} else {
|
|
||||||
wf.Body.SetPrefix(defaultContentType)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaultContentType == TypeFormData {
|
wf.Body.SetPrefix(defaultContentType)
|
||||||
createMultipart(wf.Body, params)
|
|
||||||
} else {
|
switch defaultContentType {
|
||||||
|
case TypeFormData:
|
||||||
|
createMultipart(wf.Body, params) // 还存在 Mixed的可能
|
||||||
|
default:
|
||||||
var values url.Values
|
var values url.Values
|
||||||
switch param := params[0].(type) {
|
switch param := params[0].(type) {
|
||||||
case map[string]string:
|
case map[string]string:
|
||||||
@@ -217,9 +246,26 @@ func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
|
|||||||
wf.Body.SetIOBody([]byte(param))
|
wf.Body.SetIOBody([]byte(param))
|
||||||
case []byte:
|
case []byte:
|
||||||
wf.Body.SetIOBody(param)
|
wf.Body.SetIOBody(param)
|
||||||
}
|
|
||||||
|
|
||||||
|
case *UploadFile:
|
||||||
|
params = append(params, TypeFormData)
|
||||||
|
wf.Body.SetPrefix(TypeFormData)
|
||||||
|
createMultipart(wf.Body, params)
|
||||||
|
case UploadFile:
|
||||||
|
params = append(params, TypeFormData)
|
||||||
|
wf.Body.SetPrefix(TypeFormData)
|
||||||
|
createMultipart(wf.Body, params)
|
||||||
|
case []*UploadFile:
|
||||||
|
params = append(params, TypeFormData)
|
||||||
|
wf.Body.SetPrefix(TypeFormData)
|
||||||
|
createMultipart(wf.Body, params)
|
||||||
|
case []UploadFile:
|
||||||
|
params = append(params, TypeFormData)
|
||||||
|
wf.Body.SetPrefix(TypeFormData)
|
||||||
|
createMultipart(wf.Body, params)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return wf
|
return wf
|
||||||
}
|
}
|
||||||
@@ -286,5 +332,7 @@ func (wf *Workflow) Execute() (*Response, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return FromHTTPResponse(resp)
|
|
||||||
|
wf.Body = NewBody()
|
||||||
|
return FromHTTPResponse(resp, wf.session.Is.isDecompressNoAccept)
|
||||||
}
|
}
|
||||||
|
|||||||
306
workflow_test.go
Normal file
306
workflow_test.go
Normal file
@@ -0,0 +1,306 @@
|
|||||||
|
package requests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/474420502/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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflow_SetHeader(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
wf := ses.Get("http://httpbin.org/headers")
|
||||||
|
var header http.Header
|
||||||
|
header = make(http.Header)
|
||||||
|
header["Eson"] = []string{"Bad"}
|
||||||
|
header["HaHa"] = []string{"xixi"}
|
||||||
|
wf.SetHeader(header)
|
||||||
|
|
||||||
|
resp, err := wf.Execute()
|
||||||
|
if err == nil && gjson.Get(resp.Content(), "headers.Eson").String() != "Bad" {
|
||||||
|
t.Error("wf header error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil && gjson.Get(resp.Content(), "headers.Haha").String() != "xixi" {
|
||||||
|
t.Error("wf header error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输入不符合规范不 会自动转换
|
||||||
|
if wf.GetHeader()["HaHa"][0] != "xixi" {
|
||||||
|
t.Error("Header 错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ses.GetHeader()) != 0 {
|
||||||
|
t.Error("session header should be zero")
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(header, "HaHa")
|
||||||
|
ses.SetHeader(header)
|
||||||
|
wf = ses.Get("http://httpbin.org/headers")
|
||||||
|
wf.AddHeader("Hello", "Hehe")
|
||||||
|
|
||||||
|
resp, err = wf.Execute()
|
||||||
|
if err != nil || gjson.Get(resp.Content(), "headers.Eson").String() != "Bad" {
|
||||||
|
t.Error("wf header error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil || gjson.Get(resp.Content(), "headers.Hello").String() != "Hehe" {
|
||||||
|
t.Error("wf header error", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(wf.GetHeader()) != 1 || wf.GetHeader()["Hello"][0] != "Hehe" {
|
||||||
|
t.Error("session header should be 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
cheader := wf.GetCombineHeader()
|
||||||
|
if len(cheader) != 2 || cheader["Eson"][0] != "Bad" {
|
||||||
|
t.Error("GetCombineHeader error")
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = wf.DelHeader("Hello").Execute()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err, resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "headers.Hello").Exists() {
|
||||||
|
t.Error(" wf.DelHeader error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflow_Cookies(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
u, err := url.Parse("http://httpbin.org")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
ses.SetCookies(u, []*http.Cookie{&http.Cookie{Name: "Request", Value: "Cookiejar"}})
|
||||||
|
wf := ses.Get("http://httpbin.org/cookies")
|
||||||
|
wf.AddCookie(&http.Cookie{Name: "eson", Value: "Bad"})
|
||||||
|
|
||||||
|
resp, _ := wf.Execute()
|
||||||
|
if gjson.Get(resp.Content(), "cookies.Request").String() != "Cookiejar" {
|
||||||
|
t.Error(" wf.AddCookie error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "cookies.eson").String() != "Bad" {
|
||||||
|
t.Error(" wf.AddCookie error")
|
||||||
|
}
|
||||||
|
|
||||||
|
wf.DelCookie("eson")
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if gjson.Get(resp.Content(), "cookies.Request").String() != "Cookiejar" {
|
||||||
|
t.Error(" wf.AddCookie error")
|
||||||
|
}
|
||||||
|
if gjson.Get(resp.Content(), "cookies.eson").Exists() {
|
||||||
|
t.Error(" wf.DelCookie error")
|
||||||
|
}
|
||||||
|
|
||||||
|
wf.AddCookies([]*http.Cookie{&http.Cookie{Name: "A", Value: "AA"}, &http.Cookie{Name: "B", Value: "BB"}})
|
||||||
|
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if gjson.Get(resp.Content(), "cookies.Request").String() != "Cookiejar" {
|
||||||
|
t.Error(" wf.AddCookie error")
|
||||||
|
}
|
||||||
|
if gjson.Get(resp.Content(), "cookies.A").String() != "AA" {
|
||||||
|
t.Error(" wf.AddCookies error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "cookies.B").String() != "BB" {
|
||||||
|
t.Error(" wf.AddCookies error")
|
||||||
|
}
|
||||||
|
|
||||||
|
wf.DelCookie(&http.Cookie{Name: "A", Value: "AA"})
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
if gjson.Get(resp.Content(), "cookies.A").Exists() {
|
||||||
|
t.Error(" wf.AddCookies error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "cookies.B").String() != "BB" {
|
||||||
|
t.Error(" wf.AddCookies error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflow_URL(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
wf := ses.Get("http://httpbin.org/")
|
||||||
|
u, err := url.Parse("http://httpbin.org/get")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
wf.SetParsedURL(u)
|
||||||
|
resp, _ := wf.Execute()
|
||||||
|
if gjson.Get(resp.Content(), "url").String() != "http://httpbin.org/get" {
|
||||||
|
t.Error("SetParsedURL ", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if wf.GetParsedURL().String() != "http://httpbin.org/get" {
|
||||||
|
t.Error("SetParsedURL ", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
wf = ses.Get("http://httpbin.org/")
|
||||||
|
|
||||||
|
resp, _ = wf.SetURLRawPath("/get").Execute()
|
||||||
|
if gjson.Get(resp.Content(), "url").String() != "http://httpbin.org/get" {
|
||||||
|
t.Error("SetParsedURL ", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
if wf.GetURLRawPath() != "/get" {
|
||||||
|
t.Error("SetParsedURL ", resp.Content())
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ = wf.SetURLRawPath("anything/user/password").Execute()
|
||||||
|
if gjson.Get(resp.Content(), "url").String() != "http://httpbin.org/anything/user/password" {
|
||||||
|
t.Error("SetParsedURL ", resp.Content())
|
||||||
|
}
|
||||||
|
paths := wf.GetURLPath()
|
||||||
|
if paths[0] != "/anything" || paths[1] != "/user" || paths[2] != "/password" {
|
||||||
|
t.Error("wf.GetURLPath()", paths)
|
||||||
|
}
|
||||||
|
|
||||||
|
wf = ses.Get("http://httpbin.org/")
|
||||||
|
wf.SetURLPath(paths)
|
||||||
|
if gjson.Get(resp.Content(), "url").String() != "http://httpbin.org/anything/user/password" {
|
||||||
|
t.Error("SetParsedURL ", resp.Content())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflow_Query(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
query := make(url.Values)
|
||||||
|
query["session"] = []string{"true"}
|
||||||
|
ses.SetQuery(query)
|
||||||
|
wf := ses.Get("http://httpbin.org/get")
|
||||||
|
wfquery := make(url.Values)
|
||||||
|
wfquery["workflow"] = []string{"do", "to"}
|
||||||
|
wf.SetQuery(wfquery)
|
||||||
|
|
||||||
|
resp, _ := wf.Execute()
|
||||||
|
result := gjson.Get(resp.Content(), "args.workflow")
|
||||||
|
|
||||||
|
for _, r := range result.Array() {
|
||||||
|
if !(r.String() == "to" || r.String() == "do") {
|
||||||
|
t.Error("workflow SetQuery error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if gjson.Get(resp.Content(), "args.session").String() != "true" {
|
||||||
|
t.Error("session SetQuery error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := wf.GetQuery()["workflow"]; ok {
|
||||||
|
sort.Slice(v, func(i, j int) bool {
|
||||||
|
if v[i] > v[j] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
if !(v[0] == "to" && v[1] == "do") && len(v) != 2 {
|
||||||
|
t.Error("workflow GetQuery", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := wf.GetQuery()["session"]; ok {
|
||||||
|
if v[0] != "true" && len(v) != 1 {
|
||||||
|
t.Error("workflow error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorkflow_Body(t *testing.T) {
|
||||||
|
ses := NewSession()
|
||||||
|
wf := ses.Post("http://httpbin.org/post")
|
||||||
|
body := NewBody()
|
||||||
|
body.SetIOBody("a=1&b=2")
|
||||||
|
wf.SetBody(body)
|
||||||
|
resp, _ := wf.Execute()
|
||||||
|
form := gjson.Get(resp.Content(), "form").Map()
|
||||||
|
if v, ok := form["a"]; ok {
|
||||||
|
if v.String() != "1" {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := form["b"]; ok {
|
||||||
|
if v.String() != "2" {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body.SetPrefix(TypeJSON)
|
||||||
|
body.SetIOBody(`{"a": "1", "b": "2"}`)
|
||||||
|
wf.SetBody(body)
|
||||||
|
resp, _ = wf.Execute()
|
||||||
|
json := gjson.Get(resp.Content(), "json").Map()
|
||||||
|
if v, ok := json["a"]; ok {
|
||||||
|
if v.String() != "1" {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := json["b"]; ok {
|
||||||
|
if v.String() != "2" {
|
||||||
|
t.Error(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// body.SetPrefix(TypeXML)
|
||||||
|
// body.SetIOBody(`<root><a>1</a><b>2</b></root>`)
|
||||||
|
// wf.SetBody(body)
|
||||||
|
// resp, _ = wf.Execute()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user