From 45bf499e8d271dac5574a6857c9785532725b20c Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 5 Sep 2019 16:06:12 +0800 Subject: [PATCH] =?UTF-8?q?ok=20requests=093.697s=09coverage:=2081.8%=20of?= =?UTF-8?q?=20statements=20Success:=20Tests=20passed.=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?uploadfile=20body=20=E6=B2=A1=E6=9C=89=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=90=8E=E6=B8=85=E9=99=A4=E7=9A=84bug.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- multipart.go | 2 +- upload_file_test.go | 133 +++++++++++++++++++++++++++++--------------- workflow.go | 37 ++++++------ workflow_test.go | 6 ++ 4 files changed, 115 insertions(+), 63 deletions(-) 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) {