Skip to content

Commit

Permalink
upd: 补充单测
Browse files Browse the repository at this point in the history
  • Loading branch information
livehigh committed Apr 19, 2023
1 parent 210fcc6 commit 6a6acef
Showing 1 changed file with 77 additions and 14 deletions.
91 changes: 77 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function prepareBucket() {
}

group('init cos', function() {
const putFile = function(cosIns, assert, done) {
const putFile = function(cosIns, done, assert, canSuccess = true) {
var key = '1.txt';
var content = Date.now().toString();
cosIns.putObject({
Expand All @@ -132,7 +132,7 @@ group('init cos', function() {
Key: key,
Body: content,
}, function (err, data) {
assert.ok(!err);
assert.ok(canSuccess ? !err : err);
done();
});
}
Expand All @@ -147,48 +147,50 @@ group('init cos', function() {
});
test('使用了小写ak sk', function(done, assert) {
var initCos = new COS({
secretId: config.SecretId + ' ',
secretKey: config.SecretKey + '',
secretId: config.SecretId,
secretKey: config.SecretKey,
});
putFile(initCos, done, assert);
putFile(initCos, done, assert, true);
});
test('SecretId格式错误', function(done, assert) {
var initCos = new COS({
SecretId: config.SecretId + ' ',
SecretKey: config.SecretKey,
});
putFile(initCos, done, assert);
var key = '1.txt';
var content = Date.now().toString();
putFile(initCos, done, assert, false);
});
test('SecretKey格式错误', function(done, assert) {
var initCos = new COS({
var initCos = new COS({
SecretId: config.SecretId,
SecretKey: config.SecretKey + ' ',
});
putFile(initCos, done, assert);
putFile(initCos, done, assert, false);
});
test('StrictSsl=false', function(done, assert) {
var initCos = new COS({
SecretId: config.SecretId,
SecretKey: config.SecretKey,
StrictSsl: false,
});
putFile(initCos, done, assert);
putFile(initCos, done, assert, true);
});
test('Tunnel=false', function(done, assert) {
var initCos = new COS({
SecretId: config.SecretId,
SecretKey: config.SecretKey,
Tunnel: false,
});
putFile(initCos, done, assert);
putFile(initCos, done, assert, true);
});
test('Timeout=6000', function(done, assert) {
var initCos = new COS({
SecretId: config.SecretId,
SecretKey: config.SecretKey,
Timeout: 6000,
});
putFile(initCos, done, assert);
putFile(initCos, done, assert, true);
});
test('模拟sms init', function(done, assert) {
var Credentials = {
Expand All @@ -200,9 +202,31 @@ group('init cos', function() {
Credentials.secretId = '123456';
Credentials.secretKey = 'abcdefg';
}, 1000);
putFile(initCos, done, assert);
putFile(initCos, done, assert, true);
});
test('getAuthorization', function(done, assert) {
test('getAuthorization error tmpSecretId', function(done, assert) {
var initCos = new COS({
getAuthorization: function (options, callback) {
callback({
tmpSecretId: config.SecretId,
TmpSecretKey: config.SecretKey,
});
}
});
putFile(initCos, done, assert, false);
});
test('getAuthorization error tmpSecretKey', function(done, assert) {
var initCos = new COS({
getAuthorization: function (options, callback) {
callback({
TmpSecretId: config.SecretId,
tmpSecretKey: config.SecretKey,
});
}
});
putFile(initCos, done, assert, false);
});
test('getAuthorization error', function(done, assert) {
var initCos = new COS({
getAuthorization: function (options, callback) {
callback({
Expand All @@ -211,7 +235,7 @@ group('init cos', function() {
});
}
});
putFile(initCos, done, assert);
putFile(initCos, done, assert, false);
});
test('getAuthorization', function(done, assert) {
var initCos = new COS({
Expand Down Expand Up @@ -380,6 +404,29 @@ group('getAuth();getV4Auth()', function () {
});
});

group('putObject() 兼容老参数AppId', function () {
test('putObject()', function (done, assert) {
const sp = config.Bucket.split('-');
const len = sp.length;
const appId = sp[len - 1];
sp.pop();
const bucketShortName = sp.join('-');
cos.putObject({
Bucket: bucketShortName,
Region: config.Region,
AppId: appId,
Key: '12345.txt',
Body: '12345',
Headers: {
'x-cos-test': 1
},
}, function (err, data) {
assert.ok(!err);
done();
});
});
});

group('getObjectUrl()', function () {
test('getObjectUrl()', function (done, assert) {
var content = Date.now().toString();
Expand Down Expand Up @@ -719,6 +766,22 @@ group('sliceUploadFile() 完整上传文件', function () {
done();
});
});
test('sliceUploadFile() 上传过程中删除本地文件', function (done, assert) {
var filename = '30mb.zip';
var filePath = createFileSync(path.resolve(__dirname, filename), 1024 * 1024 * 30);
cos.sliceUploadFile({
Bucket: config.Bucket,
Region: config.Region,
Key: filename,
FilePath: filePath,
}, function (err, data) {
assert(err);
done();
});
setTimeout(() => {
fs.rmSync(filePath);
}, 1000);
});
});

group('abortUploadTask()', function () {
Expand Down

0 comments on commit 6a6acef

Please sign in to comment.