diff --git a/multipart.go b/multipart.go index 6361de6..5da1a64 100644 --- a/multipart.go +++ b/multipart.go @@ -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) } diff --git a/upload_file_test.go b/upload_file_test.go index 027a138..ad1beae 100644 --- a/upload_file_test.go +++ b/upload_file_test.go @@ -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()) + } } } diff --git a/workflow.go b/workflow.go index 989515e..0fc0afc 100644 --- a/workflow.go +++ b/workflow.go @@ -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) } diff --git a/workflow_test.go b/workflow_test.go index 02ebf2f..cf136e3 100644 --- a/workflow_test.go +++ b/workflow_test.go @@ -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) {