Skip to content

Commit 6a6acef

Browse files
committed
upd: 补充单测
1 parent 210fcc6 commit 6a6acef

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

test/test.js

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function prepareBucket() {
123123
}
124124

125125
group('init cos', function() {
126-
const putFile = function(cosIns, assert, done) {
126+
const putFile = function(cosIns, done, assert, canSuccess = true) {
127127
var key = '1.txt';
128128
var content = Date.now().toString();
129129
cosIns.putObject({
@@ -132,7 +132,7 @@ group('init cos', function() {
132132
Key: key,
133133
Body: content,
134134
}, function (err, data) {
135-
assert.ok(!err);
135+
assert.ok(canSuccess ? !err : err);
136136
done();
137137
});
138138
}
@@ -147,48 +147,50 @@ group('init cos', function() {
147147
});
148148
test('使用了小写ak sk', function(done, assert) {
149149
var initCos = new COS({
150-
secretId: config.SecretId + ' ',
151-
secretKey: config.SecretKey + '',
150+
secretId: config.SecretId,
151+
secretKey: config.SecretKey,
152152
});
153-
putFile(initCos, done, assert);
153+
putFile(initCos, done, assert, true);
154154
});
155155
test('SecretId格式错误', function(done, assert) {
156156
var initCos = new COS({
157157
SecretId: config.SecretId + ' ',
158158
SecretKey: config.SecretKey,
159159
});
160-
putFile(initCos, done, assert);
160+
var key = '1.txt';
161+
var content = Date.now().toString();
162+
putFile(initCos, done, assert, false);
161163
});
162164
test('SecretKey格式错误', function(done, assert) {
163-
var initCos = new COS({
165+
var initCos = new COS({
164166
SecretId: config.SecretId,
165167
SecretKey: config.SecretKey + ' ',
166168
});
167-
putFile(initCos, done, assert);
169+
putFile(initCos, done, assert, false);
168170
});
169171
test('StrictSsl=false', function(done, assert) {
170172
var initCos = new COS({
171173
SecretId: config.SecretId,
172174
SecretKey: config.SecretKey,
173175
StrictSsl: false,
174176
});
175-
putFile(initCos, done, assert);
177+
putFile(initCos, done, assert, true);
176178
});
177179
test('Tunnel=false', function(done, assert) {
178180
var initCos = new COS({
179181
SecretId: config.SecretId,
180182
SecretKey: config.SecretKey,
181183
Tunnel: false,
182184
});
183-
putFile(initCos, done, assert);
185+
putFile(initCos, done, assert, true);
184186
});
185187
test('Timeout=6000', function(done, assert) {
186188
var initCos = new COS({
187189
SecretId: config.SecretId,
188190
SecretKey: config.SecretKey,
189191
Timeout: 6000,
190192
});
191-
putFile(initCos, done, assert);
193+
putFile(initCos, done, assert, true);
192194
});
193195
test('模拟sms init', function(done, assert) {
194196
var Credentials = {
@@ -200,9 +202,31 @@ group('init cos', function() {
200202
Credentials.secretId = '123456';
201203
Credentials.secretKey = 'abcdefg';
202204
}, 1000);
203-
putFile(initCos, done, assert);
205+
putFile(initCos, done, assert, true);
204206
});
205-
test('getAuthorization', function(done, assert) {
207+
test('getAuthorization error tmpSecretId', function(done, assert) {
208+
var initCos = new COS({
209+
getAuthorization: function (options, callback) {
210+
callback({
211+
tmpSecretId: config.SecretId,
212+
TmpSecretKey: config.SecretKey,
213+
});
214+
}
215+
});
216+
putFile(initCos, done, assert, false);
217+
});
218+
test('getAuthorization error tmpSecretKey', function(done, assert) {
219+
var initCos = new COS({
220+
getAuthorization: function (options, callback) {
221+
callback({
222+
TmpSecretId: config.SecretId,
223+
tmpSecretKey: config.SecretKey,
224+
});
225+
}
226+
});
227+
putFile(initCos, done, assert, false);
228+
});
229+
test('getAuthorization error', function(done, assert) {
206230
var initCos = new COS({
207231
getAuthorization: function (options, callback) {
208232
callback({
@@ -211,7 +235,7 @@ group('init cos', function() {
211235
});
212236
}
213237
});
214-
putFile(initCos, done, assert);
238+
putFile(initCos, done, assert, false);
215239
});
216240
test('getAuthorization', function(done, assert) {
217241
var initCos = new COS({
@@ -380,6 +404,29 @@ group('getAuth();getV4Auth()', function () {
380404
});
381405
});
382406

407+
group('putObject() 兼容老参数AppId', function () {
408+
test('putObject()', function (done, assert) {
409+
const sp = config.Bucket.split('-');
410+
const len = sp.length;
411+
const appId = sp[len - 1];
412+
sp.pop();
413+
const bucketShortName = sp.join('-');
414+
cos.putObject({
415+
Bucket: bucketShortName,
416+
Region: config.Region,
417+
AppId: appId,
418+
Key: '12345.txt',
419+
Body: '12345',
420+
Headers: {
421+
'x-cos-test': 1
422+
},
423+
}, function (err, data) {
424+
assert.ok(!err);
425+
done();
426+
});
427+
});
428+
});
429+
383430
group('getObjectUrl()', function () {
384431
test('getObjectUrl()', function (done, assert) {
385432
var content = Date.now().toString();
@@ -719,6 +766,22 @@ group('sliceUploadFile() 完整上传文件', function () {
719766
done();
720767
});
721768
});
769+
test('sliceUploadFile() 上传过程中删除本地文件', function (done, assert) {
770+
var filename = '30mb.zip';
771+
var filePath = createFileSync(path.resolve(__dirname, filename), 1024 * 1024 * 30);
772+
cos.sliceUploadFile({
773+
Bucket: config.Bucket,
774+
Region: config.Region,
775+
Key: filename,
776+
FilePath: filePath,
777+
}, function (err, data) {
778+
assert(err);
779+
done();
780+
});
781+
setTimeout(() => {
782+
fs.rmSync(filePath);
783+
}, 1000);
784+
});
722785
});
723786

724787
group('abortUploadTask()', function () {

0 commit comments

Comments
 (0)