forked from tencentyun/cos-go-sdk-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
185 lines (174 loc) · 4.85 KB
/
helper_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cos
import (
"encoding/xml"
"fmt"
"net/http"
"reflect"
"testing"
)
func Test_calSHA1Digest(t *testing.T) {
want := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
got := fmt.Sprintf("%x", calSHA1Digest([]byte("test")))
if got != want {
t.Errorf("calSHA1Digest request sha1: %+v, want %+v", got, want)
}
}
func Test_calMD5Digest(t *testing.T) {
want := "098f6bcd4621d373cade4e832627b4f6"
got := fmt.Sprintf("%x", calMD5Digest([]byte("test")))
if got != want {
t.Errorf("calMD5Digest request md5: %+v, want %+v", got, want)
}
}
func Test_cloneHeader(t *testing.T) {
ori := http.Header{}
opt := &ori
opt.Add("TestHeader1", "h1")
opt.Add("TestHeader1", "h2")
res := cloneHeader(opt)
if !reflect.DeepEqual(res, opt) {
t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
}
if !reflect.DeepEqual(ori, *opt) {
t.Errorf("cloneHeader, returned:%+v, want:%+v", *opt, ori)
}
res.Add("cloneHeader1", "c1")
res.Add("cloneHeader2", "c2")
if v := opt.Get("cloneHeader1"); v != "" {
t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
}
if v := opt.Get("cloneHeader2"); v != "" {
t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
}
opt = &http.Header{}
res = cloneHeader(opt)
if !reflect.DeepEqual(res, opt) {
t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
}
}
func Test_CloneCompleteMultipartUploadOptions(t *testing.T) {
ori := CompleteMultipartUploadOptions{
XMLName: xml.Name{Local: "CompleteMultipartUploadResult"},
Parts: []Object{
{
Key: "Key1",
ETag: "Etag1",
},
{
Key: "Key2",
ETag: "Etag2",
},
},
XOptionHeader: &http.Header{},
}
ori.XOptionHeader.Add("Test", "value")
opt := &ori
res := CloneCompleteMultipartUploadOptions(opt)
if !reflect.DeepEqual(res, opt) {
t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
}
if !reflect.DeepEqual(ori, *opt) {
t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", *opt, ori)
}
res.XOptionHeader.Add("TestClone", "value")
if v := opt.XOptionHeader.Get("TestClone"); v != "" {
t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
}
opt = &CompleteMultipartUploadOptions{}
res = CloneCompleteMultipartUploadOptions(opt)
if !reflect.DeepEqual(res, opt) {
t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
}
res.Parts = append(res.Parts, Object{Key: "K", ETag: "T"})
if len(opt.Parts) > 0 {
t.Errorf("CloneCompleteMultipartUploadOptions Failed")
}
if reflect.DeepEqual(res, opt) {
t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
}
}
func Test_CopyOptionsToMulti(t *testing.T) {
opt := &ObjectCopyOptions{
&ObjectCopyHeaderOptions{
CacheControl: "max-age=1",
ContentEncoding: "gzip",
ContentType: "text/html",
},
nil,
}
mul := CopyOptionsToMulti(opt)
if opt.ContentType != mul.ContentType {
t.Errorf("CopyOptionsToMulti, returned:%+v,want:%+v", mul, opt)
}
if opt.CacheControl != mul.CacheControl {
t.Errorf("CopyOptionsToMulti, returned:%+v,want:%+v", mul, opt)
}
if opt.ContentEncoding != mul.ContentEncoding {
t.Errorf("CopyOptionsToMulti, returned:%+v,want:%+v", mul, opt)
}
}
func Test_CloneInitiateMultipartUploadOptions(t *testing.T) {
opt := &InitiateMultipartUploadOptions{
&ACLHeaderOptions{},
&ObjectPutHeaderOptions{
CacheControl: "max-age=1",
ContentEncoding: "gzip",
ContentType: "text/html",
},
}
res := CloneInitiateMultipartUploadOptions(opt)
if !reflect.DeepEqual(opt, res) {
t.Errorf("CloneInitiateMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
}
}
func Test_CloneObjectGetOptions(t *testing.T) {
opt := ObjectGetOptions{
Range: "bytes=1-100",
}
res := CloneObjectGetOptions(&opt)
if opt.Range != res.Range {
t.Errorf("CloneObjectGetOptions failed")
}
ro, _ := GetRangeOptions(&opt)
if FormatRangeOptions(ro) != "bytes=1-100" {
t.Errorf("FormatRangeOptions failed")
}
ro.HasStart = true
ro.HasEnd = false
if FormatRangeOptions(ro) != "bytes=1-" {
t.Errorf("FormatRangeOptions failed")
}
ro.HasStart = false
ro.HasEnd = true
if FormatRangeOptions(ro) != "bytes=-100" {
t.Errorf("FormatRangeOptions failed")
}
ro.HasStart = false
ro.HasEnd = false
if FormatRangeOptions(ro) != "" {
t.Errorf("FormatRangeOptions failed")
}
}
func Test_progress(t *testing.T) {
listener := &DefaultProgressListener{}
listener.ProgressChangedCallback(&ProgressEvent{
EventType: ProgressStartedEvent,
TotalBytes: 1,
})
listener.ProgressChangedCallback(&ProgressEvent{
EventType: ProgressDataEvent,
TotalBytes: 1,
})
listener.ProgressChangedCallback(&ProgressEvent{
EventType: ProgressCompletedEvent,
TotalBytes: 1,
})
listener.ProgressChangedCallback(&ProgressEvent{
EventType: ProgressFailedEvent,
TotalBytes: 1,
})
listener.ProgressChangedCallback(&ProgressEvent{
EventType: -1,
TotalBytes: 1,
})
}