作者 yangfu

image upload test

... ... @@ -40,6 +40,7 @@ func Image(request *protocol.ImageRequest)(rsp *protocol.ImageResponse,err error
subfix:=path.Ext(f.Filename)
//文件格式不符合
if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){
err = common.NewErrorWithMsg(2,"file format error")
return
}
filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix)
... ...
package upload
import (
"bytes"
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/ability/tests"
"mime/multipart"
"testing"
)
func init(){
tests.Init()
}
func Test_Image(t *testing.T){
input := &protocol.ImageRequest{}
input.Files = append(input.Files,&multipart.FileHeader{
Filename:"abc.gif",
})
var bufReader bytes.Buffer
mpWriter:=multipart.NewWriter(&bufReader)
fw,err :=mpWriter.CreateFormFile("file_a","a.jpg")//a.txt
if err!=nil{
t.Fatal(err)
return
}
fw.Write([]byte("this is a txt"))
mpWriter.WriteField("name","tiprok")
fwb,err :=mpWriter.CreateFormFile("file_a","b.png")
if err!=nil{
t.Fatal(err)
return
}
fwb.Write([]byte("this is b txt"))
mpWriter.Close()
mpReader:=multipart.NewReader(&bufReader,mpWriter.Boundary())
form,err :=mpReader.ReadForm(100)
if err!=nil{
t.Fatal(err)
}
for _,files :=range form.File{
//for i:=range files{
// file :=files[i]
// if f,err :=file.Open();err!=nil{
// return
// }else{
// bufTmp :=bytes.NewBuffer(nil)
// bufTmp.ReadFrom(f)
// //log.Println(file.Filename," file:",bufTmp.String())
//
// }
//}
input.Files = append(input.Files,files...)
}
rsp,err :=Image(input)
if err!=nil{
t.Fatal(err)
}
t.Log(rsp)
}
... ...