Skip to content

Commit 5eeb4ea

Browse files
committed
Refactor ListObjectsV2 functional tests to use extended command
Issue: CLDSRV-868
1 parent b1fa7bb commit 5eeb4ea

File tree

1 file changed

+10
-84
lines changed
  • tests/functional/aws-node-sdk/test/bucket

1 file changed

+10
-84
lines changed

tests/functional/aws-node-sdk/test/bucket/get.js

Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
ListObjectsV2Command,
77
S3Client,
88
} = require('@aws-sdk/client-s3');
9-
const { streamCollector } = require('@smithy/node-http-handler');
9+
const { ListObjectsV2ExtendedCommand } = require('@scality/cloudserverclient');
1010
const {
1111
IAMClient,
1212
CreatePolicyCommand,
@@ -17,8 +17,6 @@ const {
1717
DeletePolicyCommand,
1818
DeleteUserCommand,
1919
} = require('@aws-sdk/client-iam');
20-
const { parseStringPromise } = require('xml2js');
21-
2220
const withV4 = require('../support/withV4');
2321
const BucketUtility = require('../../lib/utility/bucket-util');
2422
const bucketSchema = require('../../schema/bucket');
@@ -609,75 +607,6 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
609607
await iamClient.send(new DeleteUserCommand({ UserName: userWithoutPermission.UserName }));
610608
});
611609

612-
const listObjectsV2WithOptionalAttributes = async (s3, bucket, headerValue) => {
613-
const localS3 = s3;
614-
let rawXml = '';
615-
616-
const addHeaderMiddleware = next => async args => {
617-
const localArgs = args;
618-
localArgs.request.headers['x-amz-optional-object-attributes'] = headerValue;
619-
return next(localArgs);
620-
};
621-
622-
const originalHandler = s3.config.requestHandler;
623-
const wrappedHandler = {
624-
async handle(request, options) {
625-
const { response } = await originalHandler.handle(request, options);
626-
627-
if (response && response.body) {
628-
const collected = await streamCollector(response.body);
629-
const buffer = Buffer.from(collected);
630-
rawXml = buffer.toString('utf-8');
631-
632-
const { Readable } = require('stream');
633-
response.body = Readable.from([buffer]);
634-
}
635-
636-
return { response };
637-
},
638-
destroy() {
639-
if (originalHandler.destroy) {
640-
originalHandler.destroy();
641-
}
642-
}
643-
};
644-
645-
localS3.config.requestHandler = wrappedHandler;
646-
localS3.middlewareStack.add(addHeaderMiddleware, {
647-
step: 'build',
648-
name: 'addOptionalAttributesHeader',
649-
});
650-
651-
try {
652-
const result = await s3.send(new ListObjectsV2Command({ Bucket: bucket }));
653-
654-
if (!rawXml) {
655-
return result;
656-
}
657-
658-
const parsedXml = await parseStringPromise(rawXml);
659-
const contents = result.Contents;
660-
const parsedContents = parsedXml?.ListBucketResult?.Contents;
661-
662-
if (!contents || !parsedContents) {
663-
return result;
664-
}
665-
666-
if (parsedContents[0]?.['x-amz-meta-department']) {
667-
contents[0]['x-amz-meta-department'] = parsedContents[0]['x-amz-meta-department'][0];
668-
}
669-
670-
if (parsedContents[0]?.['x-amz-meta-hr']) {
671-
contents[0]['x-amz-meta-hr'] = parsedContents[0]['x-amz-meta-hr'][0];
672-
}
673-
674-
return result;
675-
} finally {
676-
localS3.config.requestHandler = originalHandler;
677-
localS3.middlewareStack.remove('addOptionalAttributesHeader');
678-
}
679-
};
680-
681610
it('should return an XML if the header is set', async () => {
682611
const s3 = bucketUtil.s3;
683612
const Bucket = bucketName;
@@ -690,11 +619,10 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
690619
hr: 'true',
691620
},
692621
}));
693-
const result = await listObjectsV2WithOptionalAttributes(
694-
s3,
622+
const result = await s3.send(new ListObjectsV2ExtendedCommand({
695623
Bucket,
696-
'x-amz-meta-*,RestoreStatus,x-amz-meta-department',
697-
);
624+
ObjectAttributes: ['x-amz-meta-*', 'RestoreStatus', 'x-amz-meta-department'],
625+
}));
698626

699627
assert.strictEqual(result.Contents.length, 1);
700628
assert.strictEqual(result.Contents[0].Key, 'super-power-object');
@@ -716,11 +644,10 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
716644
}));
717645

718646
try {
719-
await listObjectsV2WithOptionalAttributes(
720-
s3ClientWithoutPermission,
647+
await s3ClientWithoutPermission.send(new ListObjectsV2ExtendedCommand({
721648
Bucket,
722-
'x-amz-meta-*,RestoreStatus,x-amz-meta-department',
723-
);
649+
ObjectAttributes: ['x-amz-meta-*', 'RestoreStatus', 'x-amz-meta-department'],
650+
}));
724651
throw new Error('Request should have been rejected');
725652
} catch (err) {
726653
if (err.message === 'Request should have been rejected') {
@@ -743,11 +670,10 @@ describe('GET Bucket - AWS.S3.listObjects', () => {
743670
hr: 'true',
744671
},
745672
}));
746-
const result = await listObjectsV2WithOptionalAttributes(
747-
s3ClientWithoutPermission,
673+
const result = await s3ClientWithoutPermission.send(new ListObjectsV2ExtendedCommand({
748674
Bucket,
749-
'RestoreStatus',
750-
);
675+
ObjectAttributes: ['RestoreStatus'],
676+
}));
751677

752678
assert.strictEqual(result.Contents.length, 1);
753679
assert.strictEqual(result.Contents[0].Key, 'super-power-object');

0 commit comments

Comments
 (0)