forked from liaowang11/aliyun-go
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapi_test.go
119 lines (104 loc) · 2.81 KB
/
api_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
package oss
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)
var (
accessID = "dcocschE0RsKNJ7G"
accessKey = "Ib86whviPt9tLussFO05CQ7n5fohlG"
testBucket = "testcasepinidea015"
testRegion = HangZhou
testData = []byte("helloworld")
)
func TestNew(t *testing.T) {
o := New(testRegion, accessID, accessKey)
if o == nil {
t.Error("Unable new oss")
}
}
func TestPutBucket(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket(testBucket)
err := bucket.PutBucket(PublicRead)
if err != nil {
t.Error("Unable put bucket:", err)
}
}
func TestPut(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket(testBucket)
data := testData
err := bucket.Put("readme", data, "text/plain", Private)
if err != nil {
t.Error("Unable put object:", err)
}
}
func TestGet(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket(testBucket)
data, err := bucket.Get("readme")
if err != nil {
t.Error("Unable get object:", err)
return
}
if bytes.Compare(data, testData) != 0 {
t.Error("Got wrong object:", err, string(data))
}
}
func TestURL(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket(testBucket)
url := bucket.URL("readme")
if url != "http://" + testBucket + ".oss-cn-hangzhou.aliyuncs.com/readme" {
t.Error("Unable get correct url:", url)
}
resp, err := http.Get(url)
defer resp.Body.Close()
if err != nil {
t.Error("Unable get object:", err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil || bytes.Compare(data, testData) != 0 {
t.Error("Got wrong object:", err, string(data))
}
}
func TestDel(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket(testBucket)
err := bucket.Del("readme")
if err != nil {
t.Error("Unable del object:", err)
}
}
func TestDelBucket(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket(testBucket)
err := bucket.DelBucket()
if err != nil {
t.Error("Unable del bucket:", err)
}
}
func TestPutBuceketWithRegion(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket("pinidea-test111")
err := bucket.PutBucket(PublicRead)
if err != nil {
t.Error("Unable put bucket:", err)
}
}
func TestPutWithRegion(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket("pinidea-test111")
data := []byte("helloworld")
err := bucket.Put("readme", data, "text/plain", Private)
if err != nil {
t.Error("Unable put object:", err)
}
}
func TestDelWithRegion(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket("pinidea-test111")
if err := bucket.Del("readme"); err != nil {
t.Error("Unable del with region", err)
}
}
func TestDelBucketWithRegion(t *testing.T) {
bucket := New(testRegion, accessID, accessKey).Bucket("pinidea-test111")
err := bucket.DelBucket()
if err != nil {
t.Error("Unable del bucket with region", err)
}
}