requests/upload_file_test.go

108 lines
2.8 KiB
Go
Raw Normal View History

package requests
import (
"testing"
"474420502.top/eson/gjson"
)
func TestUploadFile(t *testing.T) {
for i := 0; i < 1; i++ {
ses := NewSession()
wf := ses.Put("http://httpbin.org/put")
2019-09-05 07:00:14 +00:00
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())
}
2019-09-05 07:00:14 +00:00
ses = NewSession()
wf = ses.Patch("http://httpbin.org/patch")
2019-09-05 07:00:14 +00:00
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.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")
}
2019-09-05 07:00:14 +00:00
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())
}
2019-09-05 07:00:14 +00:00
}
}