From 85c332b076aeaac56e7e9f4c4f4833068e89d568 Mon Sep 17 00:00:00 2001 From: "zhengzuoyu.zzy" Date: Thu, 18 Jan 2024 16:43:45 +0800 Subject: [PATCH] test: add cases about signUtils --- test/browser/browser.test.js | 31 ++++++++++++++++++++++++++++++- test/node/object.test.js | 19 +++++++++++++++++++ test/node/sts.test.js | 7 ++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index ff01e169..38880df1 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -1235,7 +1235,31 @@ describe('browser', () => { }); }); - it('signatureUrl will should use refreshSTSToken', async () => { + it('should set bucket when use signature V4', async () => { + const tempStore = oss( + Object.assign({}, ossConfig, moreConfigs, { + bucket: undefined + }) + ); + + try { + await tempStore.signatureUrlV4('GET', 60, undefined, 'test.txt'); + assert.fail('Expected getCanonicalRequest to throw an error'); + } catch (err) { + assert.strictEqual(err.message, 'Please ensure that bucketName is passed into getCanonicalRequest.'); + } + }); + + it('should additional headers are included in the request headers when use signature V4', async () => { + try { + await store.signatureUrlV4('GET', 60, undefined, 'test.txt', ['cache-control']); + assert.fail('Expected getCanonicalRequest to throw an error'); + } catch (err) { + assert.strictEqual(err.message, "Can't find additional header cache-control in request headers."); + } + }); + + it('asyncSignatureUrl and signatureUrlV4 should use refreshSTSToken', async () => { let flag = false; store = oss({ @@ -1259,6 +1283,11 @@ describe('browser', () => { await sleep(2000); await store.asyncSignatureUrl('test.txt'); assert(flag); + + flag = false; + await sleep(2000); + await store.signatureUrlV4('GET', 60, undefined, 'test.txt'); + assert(flag); }); }); diff --git a/test/node/object.test.js b/test/node/object.test.js index 2789dac7..26336683 100644 --- a/test/node/object.test.js +++ b/test/node/object.test.js @@ -1342,6 +1342,25 @@ describe('test/object.test.js', () => { }); assert.strictEqual(200, result.status); }); + + it('should set bucket when use signature V4', async () => { + const conf = { ...config, ...moreConfigs, bucket: undefined }; + const tempStore = oss(conf); + + await assert.rejects(tempStore.signatureUrlV4('GET', 60, undefined, 'test.txt'), err => { + assert.strictEqual(err.message, 'Please ensure that bucketName is passed into getCanonicalRequest.'); + + return true; + }); + }); + + it('should additional headers are included in the request headers when use signature V4', async () => { + await assert.rejects(store.signatureUrlV4('GET', 60, undefined, 'test.txt', ['cache-control']), err => { + assert.strictEqual(err.message, "Can't find additional header cache-control in request headers."); + + return true; + }); + }); }); describe('getStream()', () => { diff --git a/test/node/sts.test.js b/test/node/sts.test.js index b155674a..2977911c 100644 --- a/test/node/sts.test.js +++ b/test/node/sts.test.js @@ -213,7 +213,7 @@ describe('test/sts.test.js', () => { } }); - it('asyncSignatureUrl will should use refreshSTSToken', async () => { + it('asyncSignatureUrl and signatureUrlV4 should use refreshSTSToken', async () => { const { credentials } = await stsClient.assumeRole(stsConfig.roleArn); let flag = false; @@ -238,6 +238,11 @@ describe('test/sts.test.js', () => { await store.asyncSignatureUrl('test.txt'); assert(flag); + + flag = false; + await utils.sleep(2000); + await store.signatureUrlV4('GET', 60, undefined, 'test.txt'); + assert(flag); }); }); });