-
Notifications
You must be signed in to change notification settings - Fork 6
/
upload_test.go
130 lines (102 loc) · 3.15 KB
/
upload_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package coquelicot
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"testing"
//"code.google.com/p/go-uuid/uuid"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
)
const dummy = "bin/coquelicot/dummy"
func TestUploadMultipart(t *testing.T) {
assert := assert.New(t)
var body bytes.Buffer
mw := multipart.NewWriter(&body)
if err := writeMPbody(dummy+"/32509211_news_bigpic.jpg", mw); err != nil {
assert.Error(err)
}
if err := writeMPbody(dummy+"/kino.jpg", mw); err != nil {
assert.Error(err)
}
mw.Close()
req, _ := http.NewRequest("POST", "/files", &body)
req.Header.Set("Content-Type", mw.FormDataContentType())
req.AddCookie(&http.Cookie{Name: "coquelicot", Value: "abcdef"})
files, err := process(req, dummy+"/root_storage")
assert.Nil(err)
assert.Equal("kino.jpg", files[1].Filename)
assert.Equal("image", files[1].BaseMime)
}
func TestUploadBinary(t *testing.T) {
assert := assert.New(t)
req, _ := http.NewRequest("POST", "/files", nil)
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("X-File", dummy+"/bin-data")
req.Header.Set("Content-Disposition", `attachment; filename="basta.png"`)
req.AddCookie(&http.Cookie{Name: "coquelicot", Value: "abcdef"})
files, err := process(req, dummy+"/root_storage")
assert.Nil(err)
assert.Equal("basta.png", files[0].Filename)
assert.Equal("image", files[0].BaseMime)
}
func TestUploadChunked(t *testing.T) {
assert := assert.New(t)
storage := dummy + "/root_storage"
fname := dummy + "/kino.jpg"
f, _ := os.Open(fname)
defer f.Close()
cookie := &http.Cookie{Name: "coquelicot", Value: uuid.New()}
req := createChunkRequest(f, 0, 24999)
req.AddCookie(cookie)
files, err := process(req, storage)
assert.Equal(incomplete, err)
assert.Equal(25000, int(files[0].Size))
req = createChunkRequest(f, 25000, 49999)
req.AddCookie(cookie)
files, err = process(req, storage)
assert.Equal(incomplete, err)
assert.Equal(50000, int(files[0].Size))
req = createChunkRequest(f, 50000, 52096)
req.AddCookie(cookie)
files, err = process(req, storage)
assert.Nil(err)
assert.Equal(52097, int(files[0].Size))
assert.Equal("kino.jpg", files[0].Filename)
}
func createChunkRequest(f *os.File, start int64, end int64) *http.Request {
var body bytes.Buffer
mw := multipart.NewWriter(&body)
fi, _ := f.Stat()
fw, _ := mw.CreateFormFile("files[]", fi.Name())
io.CopyN(fw, f, end-start+1)
mw.Close()
req, _ := http.NewRequest("POST", "/files", &body)
req.Header.Set("Content-Type", mw.FormDataContentType())
req.Header.Set("Content-Disposition", `attachment; filename="`+fi.Name()+`"`)
req.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, fi.Size()))
return req
}
func TestTempFileChunks(t *testing.T) {
assert := assert.New(t)
file, err := tempFileChunks(0, dummy+"/root_storage", "abcdef", "kino.jpg")
assert.Nil(err)
assert.NotNil(file)
}
func writeMPbody(fname string, mw *multipart.Writer) error {
fw, _ := mw.CreateFormFile("files[]", filepath.Base(fname))
f, err := os.Open(fname)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(fw, f)
if err != nil {
return err
}
return nil
}