ok requests 3.697s coverage: 81.8% of statements

Success: Tests passed.
修改uploadfile body 没有执行后清除的bug.
This commit is contained in:
huangsimin 2019-09-05 16:06:12 +08:00
parent a8f7dac34d
commit 45bf499e8d
4 changed files with 115 additions and 63 deletions

View File

@ -75,7 +75,7 @@ func createMultipart(postParams IBody, params []interface{}) {
} else {
for ii, p := range uploadFiles {
if p.FieldName == "" {
p.FieldName = "file" + strconv.Itoa(ii) + "_" + strconv.Itoa(i)
p.FieldName = "file" + strconv.Itoa(i) + "_" + strconv.Itoa(ii)
}
writeFormUploadFile(mwriter, p)
}

View File

@ -7,56 +7,101 @@ import (
)
func TestUploadFile(t *testing.T) {
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())
}
for i := 0; i < 1; i++ {
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.Put("http://httpbin.org/put")
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())
}
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())
}
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.Patch("http://httpbin.org/patch")
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())
}
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())
}
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.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())
}
}
}

View File

@ -172,25 +172,25 @@ func (wf *Workflow) GetURLRawPath() string {
}
// encodePath path格式每个item都必须以/开头
// func encodePath(path []string) string {
// rawpath := ""
// for _, p := range path {
// if p[0] != '/' {
// p = "/" + p
// }
// rawpath += p
// }
// return rawpath
// }
func encodePath(path []string) string {
rawpath := ""
for _, p := range path {
if p[0] != '/' {
p = "/" + p
}
rawpath += p
}
return rawpath
}
// // SetURLPath 设置Path参数
// func (wf *Workflow) SetURLPath(path []string) *Workflow {
// if path == nil {
// return wf
// }
// wf.ParsedURL.Path = encodePath(path)
// return wf
// }
// SetURLPath 设置Path参数 对应 GetURLPath
func (wf *Workflow) SetURLPath(path []string) *Workflow {
if path == nil {
return wf
}
wf.ParsedURL.Path = encodePath(path)
return wf
}
// SetURLRawPath 设置 参数 eg. /get = http:// hostname + /get
func (wf *Workflow) SetURLRawPath(path string) *Workflow {
@ -333,5 +333,6 @@ func (wf *Workflow) Execute() (*Response, error) {
return nil, err
}
wf.Body = NewBody()
return FromHTTPResponse(resp, wf.session.Is.isDecompressNoAccept)
}

View File

@ -212,6 +212,12 @@ func TestWorkflow_URL(t *testing.T) {
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) {