test all success!

This commit is contained in:
huangsimin 2018-11-19 18:42:31 +08:00
parent 02bc589ba3
commit d382505453
3 changed files with 31 additions and 9 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.pyc *.pyc
*.vscode

View File

@ -18,6 +18,8 @@ import (
type Body struct { type Body struct {
// Query map[string][]string // Query map[string][]string
ioBody interface{} ioBody interface{}
// prefix ContentType 前缀
prefix string
// Files []UploadFile // Files []UploadFile
contentTypes map[string]int contentTypes map[string]int
} }
@ -41,19 +43,36 @@ func (body *Body) IOBody() interface{} {
// ContentType 获取ContentType // ContentType 获取ContentType
func (body *Body) ContentType() string { func (body *Body) ContentType() string {
content := "" content := body.prefix + ";"
for kvalue := range body.contentTypes { for kvalue := range body.contentTypes {
content += kvalue + ";" content += kvalue + ";"
} }
return strings.TrimRight(content, ";") return strings.TrimRight(content, ";")
} }
// AddPrefix AddPrefix 和 AddContentType的顺序会影响到ContentType()的返回结果
func (body *Body) AddPrefix(ct string) {
content := body.prefix
for _, v := range strings.Split(ct, ";") {
v = strings.Trim(v, " ")
if v != "" {
if body.prefix != v {
content += v + ";"
}
}
}
content = strings.TrimRight(content, ";")
body.prefix = content
}
// AddContentType 添加 Add Type类型 // AddContentType 添加 Add Type类型
func (body *Body) AddContentType(ct string) { func (body *Body) AddContentType(ct string) {
for _, v := range strings.Split(ct, ";") { for _, v := range strings.Split(ct, ";") {
v = strings.Trim(v, " ") v = strings.Trim(v, " ")
if v != "" { if v != "" {
body.contentTypes[v] = 1 if body.prefix != v {
body.contentTypes[v] = 1
}
} }
} }
@ -70,6 +89,8 @@ type IBody interface {
ContentType() string ContentType() string
// AppendContent // AppendContent
AddContentType(ct string) AddContentType(ct string)
// AddPrefix 添加 Prefix
AddPrefix(ct string)
} }
// BasicAuth 帐号认真结构 // BasicAuth 帐号认真结构
@ -105,10 +126,10 @@ const (
TypeURLENCODED = "application/x-www-form-urlencoded" TypeURLENCODED = "application/x-www-form-urlencoded"
// TypeForm PostForm类型 // TypeForm PostForm类型
TypeForm = TypeURLENCODED TypeForm = TypeURLENCODED
// TypeContentEmpty 没有Form的类型 Content
TypeContentEmpty = "ContentEmpty"
// TypeFormData 类型 // TypeFormData 类型
TypeFormData = "multipart/form-data" TypeFormData = "multipart/form-data"
// TypeMixed Mixed类型
TypeMixed = "multipart/mixed"
// HeaderKeyHost Host // HeaderKeyHost Host
HeaderKeyHost = "Host" HeaderKeyHost = "Host"

View File

@ -1,6 +1,7 @@
package requests package requests
import ( import (
"log"
"net/http" "net/http"
"net/url" "net/url"
"regexp" "regexp"
@ -175,23 +176,22 @@ func (wf *Workflow) SetURLRawPath(path string) *Workflow {
// SetBody 参数设置 // SetBody 参数设置
func (wf *Workflow) SetBody(params ...interface{}) *Workflow { func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
if params != nil {
if params != nil {
plen := len(params) plen := len(params)
defaultContentType := TypeURLENCODED defaultContentType := TypeURLENCODED
if plen >= 2 { if plen >= 2 {
t := params[plen-1] t := params[plen-1]
defaultContentType = t.(string) defaultContentType = t.(string)
wf.Body.AddContentType(defaultContentType) wf.Body.AddPrefix(defaultContentType)
} else { } else {
wf.Body.AddContentType(defaultContentType) wf.Body.AddPrefix(defaultContentType)
} }
if defaultContentType == TypeFormData { if defaultContentType == TypeFormData {
createMultipart(wf.Body, params) createMultipart(wf.Body, params)
} else { } else {
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:
@ -211,7 +211,7 @@ func (wf *Workflow) SetBody(params ...interface{}) *Workflow {
} }
} }
log.Println(wf.Body.ContentType())
return wf return wf
} }