requests/upload_file_test.go
2019-09-05 15:00:14 +08:00

63 lines
1.6 KiB
Go

package requests
import (
"testing"
"474420502.top/eson/gjson"
)
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())
}
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 = 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.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 = 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()["MyField"]; !ok {
t.Error("file error", resp.Content())
}
}