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 { } 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)
} }

View File

@ -7,6 +7,9 @@ import (
) )
func TestUploadFile(t *testing.T) { func TestUploadFile(t *testing.T) {
for i := 0; i < 1; i++ {
ses := NewSession() ses := NewSession()
wf := ses.Put("http://httpbin.org/put") wf := ses.Put("http://httpbin.org/put")
@ -20,12 +23,17 @@ func TestUploadFile(t *testing.T) {
t.Error("file error", resp.Content()) t.Error("file error", resp.Content())
} }
ses = NewSession()
wf = ses.Patch("http://httpbin.org/patch")
wf.SetBodyAuto("tests/json.file", TypeFormData) wf.SetBodyAuto("tests/json.file", TypeFormData)
resp, _ = wf.Execute() resp, _ = wf.Execute()
if _, ok := gjson.Get(resp.Content(), "files").Map()["file0"]; !ok { if _, ok := gjson.Get(resp.Content(), "files").Map()["file0"]; !ok {
t.Error("file error", resp.Content()) t.Error("file error", resp.Content())
} }
ses = NewSession()
wf = ses.Delete("http://httpbin.org/delete")
ufile = NewUploadFile() ufile = NewUploadFile()
ufile.SetFileName("MyFile") ufile.SetFileName("MyFile")
ufile.SetFieldName("MyField") ufile.SetFieldName("MyField")
@ -36,6 +44,9 @@ func TestUploadFile(t *testing.T) {
t.Error("file error", resp.Content()) t.Error("file error", resp.Content())
} }
// ses = NewSession()
// wf = ses.Put("http://httpbin.org/put")
ufile.SetFileReaderCloserFromFile("tests/json.file") ufile.SetFileReaderCloserFromFile("tests/json.file")
wf.SetBodyAuto(*ufile) wf.SetBodyAuto(*ufile)
resp, _ = wf.Execute() resp, _ = wf.Execute()
@ -43,6 +54,9 @@ func TestUploadFile(t *testing.T) {
t.Error("file error", resp.Content()) t.Error("file error", resp.Content())
} }
// ses = NewSession()
// wf = ses.Put("http://httpbin.org/put")
ufile = NewUploadFile() ufile = NewUploadFile()
ufile.SetFileName("MyFile") ufile.SetFileName("MyFile")
ufile.SetFileReaderCloserFromFile("tests/json.file") ufile.SetFileReaderCloserFromFile("tests/json.file")
@ -55,8 +69,39 @@ func TestUploadFile(t *testing.T) {
ufile.SetFileReaderCloserFromFile("tests/json.file") ufile.SetFileReaderCloserFromFile("tests/json.file")
wf.SetBodyAuto(*ufile) wf.SetBodyAuto(*ufile)
resp, _ = wf.Execute() resp, _ = wf.Execute()
if _, ok := gjson.Get(resp.Content(), "files").Map()["MyField"]; !ok { if _, ok := gjson.Get(resp.Content(), "files").Map()["file0"]; !ok {
t.Error("file error", resp.Content()) 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都必须以/开头 // encodePath path格式每个item都必须以/开头
// func encodePath(path []string) string { func encodePath(path []string) string {
// rawpath := "" rawpath := ""
// for _, p := range path { for _, p := range path {
// if p[0] != '/' { if p[0] != '/' {
// p = "/" + p p = "/" + p
// } }
// rawpath += p rawpath += p
// } }
// 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
// } }
// wf.ParsedURL.Path = encodePath(path) wf.ParsedURL.Path = encodePath(path)
// return wf return wf
// } }
// SetURLRawPath 设置 参数 eg. /get = http:// hostname + /get // SetURLRawPath 设置 参数 eg. /get = http:// hostname + /get
func (wf *Workflow) SetURLRawPath(path string) *Workflow { func (wf *Workflow) SetURLRawPath(path string) *Workflow {
@ -333,5 +333,6 @@ func (wf *Workflow) Execute() (*Response, error) {
return nil, err return nil, err
} }
wf.Body = NewBody()
return FromHTTPResponse(resp, wf.session.Is.isDecompressNoAccept) 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" { if paths[0] != "/anything" || paths[1] != "/user" || paths[2] != "/password" {
t.Error("wf.GetURLPath()", paths) 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) { func TestWorkflow_Query(t *testing.T) {