1
|
package upload
|
1
|
package upload
|
2
|
|
2
|
|
3
|
import (
|
3
|
import (
|
|
|
4
|
+ "bytes"
|
4
|
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
|
5
|
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
|
|
|
6
|
+ "gitlab.fjmaimaimai.com/mmm-go/ability/tests"
|
5
|
"mime/multipart"
|
7
|
"mime/multipart"
|
6
|
"testing"
|
8
|
"testing"
|
7
|
)
|
9
|
)
|
8
|
|
10
|
|
|
|
11
|
+func init(){
|
|
|
12
|
+ tests.Init()
|
|
|
13
|
+}
|
|
|
14
|
+
|
|
|
15
|
+
|
9
|
func Test_Image(t *testing.T){
|
16
|
func Test_Image(t *testing.T){
|
10
|
input := &protocol.ImageRequest{}
|
17
|
input := &protocol.ImageRequest{}
|
11
|
- input.Files = append(input.Files,&multipart.FileHeader{
|
|
|
12
|
- Filename:"abc.gif",
|
|
|
13
|
|
18
|
|
14
|
- })
|
19
|
+ var bufReader bytes.Buffer
|
|
|
20
|
+ mpWriter:=multipart.NewWriter(&bufReader)
|
|
|
21
|
+ fw,err :=mpWriter.CreateFormFile("file_a","a.jpg")//a.txt
|
|
|
22
|
+ if err!=nil{
|
|
|
23
|
+ t.Fatal(err)
|
|
|
24
|
+ return
|
|
|
25
|
+ }
|
|
|
26
|
+ fw.Write([]byte("this is a txt"))
|
|
|
27
|
+ mpWriter.WriteField("name","tiprok")
|
|
|
28
|
+
|
|
|
29
|
+
|
|
|
30
|
+ fwb,err :=mpWriter.CreateFormFile("file_a","b.png")
|
|
|
31
|
+ if err!=nil{
|
|
|
32
|
+ t.Fatal(err)
|
|
|
33
|
+ return
|
|
|
34
|
+ }
|
|
|
35
|
+ fwb.Write([]byte("this is b txt"))
|
|
|
36
|
+ mpWriter.Close()
|
|
|
37
|
+
|
|
|
38
|
+ mpReader:=multipart.NewReader(&bufReader,mpWriter.Boundary())
|
|
|
39
|
+ form,err :=mpReader.ReadForm(100)
|
|
|
40
|
+ if err!=nil{
|
|
|
41
|
+ t.Fatal(err)
|
|
|
42
|
+ }
|
|
|
43
|
+ for _,files :=range form.File{
|
|
|
44
|
+ //for i:=range files{
|
|
|
45
|
+ // file :=files[i]
|
|
|
46
|
+ // if f,err :=file.Open();err!=nil{
|
|
|
47
|
+ // return
|
|
|
48
|
+ // }else{
|
|
|
49
|
+ // bufTmp :=bytes.NewBuffer(nil)
|
|
|
50
|
+ // bufTmp.ReadFrom(f)
|
|
|
51
|
+ // //log.Println(file.Filename," file:",bufTmp.String())
|
|
|
52
|
+ //
|
|
|
53
|
+ // }
|
|
|
54
|
+ //}
|
|
|
55
|
+ input.Files = append(input.Files,files...)
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ rsp,err :=Image(input)
|
|
|
59
|
+ if err!=nil{
|
|
|
60
|
+ t.Fatal(err)
|
|
|
61
|
+ }
|
|
|
62
|
+ t.Log(rsp)
|
15
|
} |
63
|
} |