-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathhelper_test.go
342 lines (312 loc) · 9.18 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package cos
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"testing"
)
// 非IsLenReader
type tmpOtherReader struct {
Reader io.Reader
}
func (this *tmpOtherReader) Read(p []byte) (int, error) {
return this.Reader.Read(p)
}
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,
})
}
func Test_UnmarshalInitMultiUploadResult_Error(t *testing.T) {
data := []byte(`
<InitiateMultipartUploadResult>
<Key>test</Key>
<UploadId>149795166761115ef06e259b2fceb8ff34bf7dd840883d26a0f90243562dd398efa41718db</UploadId>
</InitiateMultipartUploadResult>`)
var res InitiateMultipartUploadResult
err := UnmarshalInitMultiUploadResult(data, &res)
if err == nil {
t.Errorf("UnmarshalInitMultiUploadResult except return error")
}
data = []byte(`
<InitiateMultipartUploadResult>
<Bucket>test-1253846586</Bucket>
<UploadId>149795166761115ef06e259b2fceb8ff34bf7dd840883d26a0f90243562dd398efa41718db</UploadId>
</InitiateMultipartUploadResult>`)
err = UnmarshalInitMultiUploadResult(data, &res)
if err == nil {
t.Errorf("UnmarshalInitMultiUploadResult except return error")
}
data = []byte(`
<InitiateMultipartUploadResult>
<Bucket>test-1253846586</Bucket>
<Key>test</Key>
</InitiateMultipartUploadResult>`)
err = UnmarshalInitMultiUploadResult(data, &res)
if err == nil {
t.Errorf("UnmarshalInitMultiUploadResult except return error")
}
}
func Test_UnmarshalCompleteMultiUploadResult_Error(t *testing.T) {
data := []byte(`
<CompleteMultipartUploadResult>
<Bucket>test</Bucket>
<Key>test/hello.txt</Key>
<ETag>"594f98b11c6901c0f0683de1085a6d0e-4"</ETag>
</CompleteMultipartUploadResult>`)
var res CompleteMultipartUploadResult
err := UnmarshalCompleteMultiUploadResult(data, &res)
if err == nil {
t.Errorf("UnmarshalInitMultiUploadResult except return error")
}
data = []byte(`
<CompleteMultipartUploadResult>
<Location>test-1253846586.cos.ap-guangzhou.myqcloud.com/test/hello.txt</Location>
<Key>test/hello.txt</Key>
<ETag>"594f98b11c6901c0f0683de1085a6d0e-4"</ETag>
</CompleteMultipartUploadResult>`)
err = UnmarshalCompleteMultiUploadResult(data, &res)
if err == nil {
t.Errorf("UnmarshalInitMultiUploadResult except return error")
}
data = []byte(`
<CompleteMultipartUploadResult>
<Location>test-1253846586.cos.ap-guangzhou.myqcloud.com/test/hello.txt</Location>
<Bucket>test</Bucket>
<ETag>"594f98b11c6901c0f0683de1085a6d0e-4"</ETag>
</CompleteMultipartUploadResult>`)
err = UnmarshalCompleteMultiUploadResult(data, &res)
if err == nil {
t.Errorf("UnmarshalInitMultiUploadResult except return error")
}
data = []byte(`
<CompleteMultipartUploadResult>
<Location>test-1253846586.cos.ap-guangzhou.myqcloud.com/test/hello.txt</Location>
<Bucket>test</Bucket>
<Key>test/hello.txt</Key>
</CompleteMultipartUploadResult>`)
err = UnmarshalCompleteMultiUploadResult(data, &res)
if err == nil {
t.Errorf("UnmarshalInitMultiUploadResult except return error")
}
}
func Test_EncodeURIComponent(t *testing.T) {
res := EncodeURIComponent("/")
if res != "%2F" {
t.Errorf("EncodeURIComponent failed, res: %v", res)
}
}
func Test_GetRange(t *testing.T) {
_, err := GetRange("byte=0-1")
if err == nil {
t.Errorf("GetRange expect failed")
}
_, err = GetRange("bytes=10")
if err == nil {
t.Errorf("GetRange expect failed")
}
_, err = GetRange("bytes=a-10")
if err == nil {
t.Errorf("GetRange expect failed")
}
_, err = GetRange("bytes=0-b")
if err == nil {
t.Errorf("GetRange expect failed")
}
_, err = GetRange("bytes=0-10")
if err != nil {
t.Errorf("GetRange failed: %v", err)
}
_, err = GetRange("bytes=0-")
if err != nil {
t.Errorf("GetRange failed: %v", err)
}
_, err = GetRange("bytes=-10")
if err != nil {
t.Errorf("GetRange failed: %v", err)
}
res, err := GetRangeOptions(&ObjectGetOptions{})
if err != nil || res != nil {
t.Errorf("GetRangeOptions failed: %v, %v", res, err)
}
}
func Test_GetBucketRegionFromUrl(t *testing.T) {
bucket, region := GetBucketRegionFromUrl(nil)
if bucket != "" || region != "" {
t.Errorf("GetBucketRegionFromUrl failed")
}
u, _ := url.Parse("http://test-125000000.cos.ap-guangzhou.myqcloud.com")
bucket, region = GetBucketRegionFromUrl(u)
if bucket != "test-125000000" && region != "ap-guangzhou" {
t.Errorf("GetBucketRegionFromUrl failed")
}
u, _ = url.Parse("http://test-125000000.com")
bucket, region = GetBucketRegionFromUrl(u)
if bucket != "" || region != "" {
t.Errorf("GetBucketRegionFromUrl failed")
}
}