-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
upload_test.go
105 lines (93 loc) · 2.09 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
package b2_test
import (
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"os"
"testing"
)
func TestUploadError(t *testing.T) {
c := getClient(t)
b := getBucket(t, c)
defer deleteBucket(t, b)
file := make([]byte, 123456)
rand.Read(file)
_, err := b.Upload(bytes.NewReader(file), "illegal//filename", "")
if err == nil {
t.Fatal("Expected an error")
}
t.Log(err)
}
func TestUploadFile(t *testing.T) {
c := getClient(t)
b := getBucket(t, c)
defer deleteBucket(t, b)
tmpfile, err := ioutil.TempFile("", "b2")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
content := make([]byte, 123456)
rand.Read(content)
if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
f, err := os.Open(tmpfile.Name())
if err != nil {
t.Fatal(err)
}
defer f.Close()
fi, err := b.Upload(f, "foo-file", "")
if err != nil {
t.Fatal(err)
}
defer c.DeleteFile(fi.ID, fi.Name)
if fi.ContentLength != 123456 {
t.Error("mismatched fi.ContentLength", fi.ContentLength)
}
if n, err := io.Copy(ioutil.Discard, f); err != nil || n != 0 {
t.Error("should have read 0 bytes:", n, err)
}
}
func TestUploadBuffer(t *testing.T) {
c := getClient(t)
b := getBucket(t, c)
defer deleteBucket(t, b)
content := make([]byte, 123456)
rand.Read(content)
buf := bytes.NewBuffer(content)
fi, err := b.Upload(buf, "foo-file", "")
if err != nil {
t.Fatal(err)
}
defer c.DeleteFile(fi.ID, fi.Name)
if fi.ContentLength != 123456 {
t.Error("mismatched fi.ContentLength", fi.ContentLength)
}
if buf.Len() != 0 {
t.Error("Buffer is not empty")
}
}
func TestUploadReader(t *testing.T) {
c := getClient(t)
b := getBucket(t, c)
defer deleteBucket(t, b)
content := make([]byte, 123456)
rand.Read(content)
r := bytes.NewReader(content)
fi, err := b.Upload(ioutil.NopCloser(r), "foo-file", "") // shadow Seek method
if err != nil {
t.Fatal(err)
}
defer c.DeleteFile(fi.ID, fi.Name)
if fi.ContentLength != 123456 {
t.Error("mismatched fi.ContentLength", fi.ContentLength)
}
if r.Len() != 0 {
t.Error("Reader is not empty")
}
}