Skip to content

Commit 3243561

Browse files
committed
upd: 新增单测
1 parent b851586 commit 3243561

File tree

1 file changed

+67
-15
lines changed

1 file changed

+67
-15
lines changed

test/test.js

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var cos = new COS({
5252
SecretKey: config.SecretKey,
5353
Proxy: proxy,
5454
// 可选参数
55-
FileParallelLimit: 3, // 控制文件上传并发数
55+
FileParallelLimit: 6, // 控制文件上传并发数
5656
ChunkParallelLimit: 3, // 控制单个文件下分片上传并发数
5757
ChunkSize: 1024 * 1024, // 控制分片大小,单位 B
5858
ProgressInterval: 1, // 控制 onProgress 回调的间隔
@@ -166,15 +166,18 @@ group('init cos', function() {
166166
});
167167
putFile(initCos, done, assert);
168168
});
169-
test('模拟web环境', function(done, assert) {
170-
var window = {};
171-
var initCos = new COS({
172-
SecretId: config.SecretId,
173-
SecretKey: config.SecretKey + ' ',
174-
});
169+
test('模拟sms init', function(done, assert) {
170+
var Credentials = {
171+
secretId: config.SecretId,
172+
secretKey: config.SecretKey,
173+
};
174+
var initCos = new COS(Credentials);
175+
setTimeout(() => {
176+
Credentials.secretId = '123456';
177+
Credentials.secretKey = 'abcdefg';
178+
}, 1000);
175179
putFile(initCos, done, assert);
176-
window = null;
177-
});
180+
});
178181
});
179182

180183
group('getService()', function () {
@@ -459,8 +462,8 @@ group('putObject(),cancelTask()', function () {
459462

460463
group('task 队列', function () {
461464
test('putObject() 批量上传', function (done, assert) {
462-
var upload = function(i) {
463-
var filename = `10m(${i}).zip`;
465+
var upload = function() {
466+
var filename = '10m.zip';
464467
var taskId;
465468
cos.putObject({
466469
Bucket: config.Bucket,
@@ -474,7 +477,7 @@ group('task 队列', function () {
474477
});
475478
}
476479
for(var i = 0; i < 1200; i ++) {
477-
upload(i);
480+
upload();
478481
}
479482
var taskList = cos.getTaskList();
480483
const isUploading = cos.isUploadRunning();
@@ -643,6 +646,7 @@ group('sliceUploadFile() 完整上传文件', function () {
643646
FilePath: filePath,
644647
Headers: {
645648
'x-cos-test': 'test',
649+
'x-cos-meta-test': 'meta',
646650
'x-cos-traffic-limit': 819200
647651
},
648652
}, function (err, data) {
@@ -766,7 +770,7 @@ group('headBucket()', function () {
766770
Bucket: config.Bucket + Date.now().toString(36),
767771
Region: config.Region
768772
}, function (err, data) {
769-
assert.ok(err, '正常获取 head bucket');
773+
assert.ok(err, 'deleteBucket 不存在');
770774
done();
771775
});
772776
});
@@ -818,6 +822,10 @@ group('putObject()', function () {
818822
Key: filename,
819823
Body: fs.createReadStream(filePath),
820824
ContentLength: fs.statSync(filePath).size,
825+
onTaskReady(id) {
826+
// 暂停任务
827+
cos.pauseTask(id);
828+
},
821829
onProgress: function (info) {
822830
lastPercent = info.percent;
823831
},
@@ -1230,6 +1238,9 @@ group('sliceCopyFile()', function () {
12301238
Key: Key,
12311239
CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/'+ filename,
12321240
CopySliceSize: 5 * 1024 * 1024,
1241+
Headers: {
1242+
'x-cos-metadata-directive': 'Replaced',
1243+
},
12331244
},function (err, data) {
12341245
if (err) throw err;
12351246
assert.ok(data.ETag.length > 0);
@@ -1330,7 +1341,18 @@ group('sliceCopyFile()', function () {
13301341
assert.ok(err);
13311342
done();
13321343
});
1333-
});
1344+
});
1345+
test('CopySource() fileSize=0', function (done, assert) {
1346+
var Key = '0b.zip';
1347+
cos.sliceCopyFile({
1348+
Bucket: config.Bucket, // Bucket 格式:test-1250000000
1349+
Region: config.Region,
1350+
CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + Key,
1351+
}, function (err, data) {
1352+
assert.ok(err);
1353+
done();
1354+
});
1355+
});
13341356
test('CopySource not found', function (done, assert) {
13351357
cos.sliceCopyFile({
13361358
Bucket: config.Bucket,
@@ -3443,6 +3465,7 @@ group('Promise', function () {
34433465
Bucket: config.Bucket,
34443466
Region: config.Region,
34453467
Key: '123.txt',
3468+
Expires: 900,
34463469
});
34473470
assert.ok(!res.then);
34483471
done();
@@ -4157,7 +4180,36 @@ group('downloadFile', function () {
41574180
});
41584181
}
41594182
});
4160-
4183+
});
4184+
test('downloadFile() 下载归档文件', function (done, assert) {
4185+
var Key = '10mb.zip';
4186+
var fileSize = 1024 * 1024 * 10;
4187+
var filePath = createFileSync(path.resolve(__dirname, Key), fileSize);
4188+
cos.sliceUploadFile({
4189+
Bucket: config.Bucket,
4190+
Region: config.Region,
4191+
Key: Key,
4192+
FilePath: filePath,
4193+
StorageClass: 'ARCHIVE',
4194+
}, function (err, data) {
4195+
if (err) {
4196+
done();
4197+
} else {
4198+
cos.downloadFile({
4199+
Bucket: config.Bucket, // Bucket 格式:test-1250000000
4200+
Region: config.Region,
4201+
Key: Key,
4202+
FilePath: './' + Key, // 本地保存路径
4203+
ChunkSize: 1024 * 1024 * 8, // 分块大小
4204+
ParallelLimit: 5, // 分块并发数
4205+
RetryTimes: 3, // 分块失败重试次数
4206+
TaskId: '123', // 可以自己生成TaskId,用于取消下载
4207+
}, function (err, data) {
4208+
assert.ok(!err);
4209+
done();
4210+
});
4211+
}
4212+
});
41614213
});
41624214
});
41634215

0 commit comments

Comments
 (0)