Skip to content

Commit b1fa7bb

Browse files
committed
Refactor GetObjectAttributes functional tests to use extended command
Issue: CLDSRV-868
1 parent aae4dd5 commit b1fa7bb

File tree

1 file changed

+28
-87
lines changed

1 file changed

+28
-87
lines changed

tests/functional/aws-node-sdk/test/object/objectGetAttributes.js

Lines changed: 28 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ const {
99
UploadPartCommand,
1010
CompleteMultipartUploadCommand,
1111
} = require('@aws-sdk/client-s3');
12-
const { streamCollector } = require('@smithy/node-http-handler');
13-
const { parseStringPromise } = require('xml2js');
14-
12+
const { GetObjectAttributesExtendedCommand } = require('@scality/cloudserverclient');
1513
const withV4 = require('../support/withV4');
1614
const BucketUtility = require('../../lib/utility/bucket-util');
1715

@@ -260,72 +258,6 @@ describe('objectGetAttributes with user metadata', () => {
260258
let bucketUtil;
261259
let s3;
262260

263-
const getObjectAttributesWithUserMetadata = async (client, params, attributes) => {
264-
let rawXml = '';
265-
266-
const addHeaderMiddleware = next => async args => {
267-
// eslint-disable-next-line no-param-reassign
268-
args.request.headers['x-amz-object-attributes'] = attributes;
269-
return next(args);
270-
};
271-
272-
const originalHandler = client.config.requestHandler;
273-
const wrappedHandler = {
274-
async handle(request, options) {
275-
const { response } = await originalHandler.handle(request, options);
276-
277-
if (response && response.body) {
278-
const collected = await streamCollector(response.body);
279-
const buffer = Buffer.from(collected);
280-
rawXml = buffer.toString('utf-8');
281-
282-
const { Readable } = require('stream');
283-
response.body = Readable.from([buffer]);
284-
}
285-
286-
return { response };
287-
},
288-
};
289-
290-
// eslint-disable-next-line no-param-reassign
291-
client.config.requestHandler = wrappedHandler;
292-
client.middlewareStack.add(addHeaderMiddleware, {
293-
step: 'build',
294-
name: 'addObjectAttributesHeader',
295-
});
296-
297-
try {
298-
const result = await client.send(new GetObjectAttributesCommand({
299-
Bucket: params.Bucket,
300-
Key: params.Key,
301-
ObjectAttributes: ['ETag'],
302-
}));
303-
304-
if (!rawXml) {
305-
return result;
306-
}
307-
308-
const parsedXml = await parseStringPromise(rawXml);
309-
const parsedData = parsedXml?.GetObjectAttributesResponse;
310-
311-
if (!parsedData) {
312-
return result;
313-
}
314-
315-
Object.keys(parsedData).forEach(k => {
316-
if (k.startsWith('x-amz-meta-')) {
317-
result[k] = parsedData[k][0];
318-
}
319-
});
320-
321-
return result;
322-
} finally {
323-
// eslint-disable-next-line no-param-reassign
324-
client.config.requestHandler = originalHandler;
325-
client.middlewareStack.remove('addObjectAttributesHeader');
326-
}
327-
};
328-
329261
before(() => {
330262
bucketUtil = new BucketUtility('default', sigCfg);
331263
s3 = bucketUtil.s3;
@@ -351,10 +283,11 @@ describe('objectGetAttributes with user metadata', () => {
351283
},
352284
}));
353285

354-
const response = await getObjectAttributesWithUserMetadata(s3, {
286+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
355287
Bucket: bucket,
356288
Key: key,
357-
}, 'x-amz-meta-custom-key');
289+
ObjectAttributes: ['x-amz-meta-custom-key'],
290+
}));
358291

359292
assert.strictEqual(response['x-amz-meta-custom-key'], 'custom-value');
360293
});
@@ -371,10 +304,11 @@ describe('objectGetAttributes with user metadata', () => {
371304
},
372305
}));
373306

374-
const response = await getObjectAttributesWithUserMetadata(s3, {
307+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
375308
Bucket: bucket,
376309
Key: key,
377-
}, 'x-amz-meta-foo,x-amz-meta-bar');
310+
ObjectAttributes: ['x-amz-meta-foo', 'x-amz-meta-bar'],
311+
}));
378312

379313
assert.strictEqual(response['x-amz-meta-foo'], 'foo-value');
380314
assert.strictEqual(response['x-amz-meta-bar'], 'bar-value');
@@ -392,10 +326,11 @@ describe('objectGetAttributes with user metadata', () => {
392326
},
393327
}));
394328

395-
const response = await getObjectAttributesWithUserMetadata(s3, {
329+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
396330
Bucket: bucket,
397331
Key: key,
398-
}, 'x-amz-meta-*');
332+
ObjectAttributes: ['x-amz-meta-*'],
333+
}));
399334

400335
assert.strictEqual(response['x-amz-meta-key1'], 'value1');
401336
assert.strictEqual(response['x-amz-meta-key2'], 'value2');
@@ -410,10 +345,11 @@ describe('objectGetAttributes with user metadata', () => {
410345
Body: body,
411346
}));
412347

413-
const response = await getObjectAttributesWithUserMetadata(s3, {
348+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
414349
Bucket: bucket,
415350
Key: key,
416-
}, 'ETag,x-amz-meta-*');
351+
ObjectAttributes: ['ETag', 'x-amz-meta-*'],
352+
}));
417353

418354
const metadataKeys = Object.keys(response).filter(k => k.startsWith('x-amz-meta-'));
419355
assert.strictEqual(metadataKeys.length, 0);
@@ -429,10 +365,11 @@ describe('objectGetAttributes with user metadata', () => {
429365
},
430366
}));
431367

432-
const response = await getObjectAttributesWithUserMetadata(s3, {
368+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
433369
Bucket: bucket,
434370
Key: key,
435-
}, 'ETag,x-amz-meta-nonexistent');
371+
ObjectAttributes: ['ETag', 'x-amz-meta-nonexistent'],
372+
}));
436373

437374
assert.strictEqual(response['x-amz-meta-nonexistent'], undefined);
438375
});
@@ -447,10 +384,11 @@ describe('objectGetAttributes with user metadata', () => {
447384
},
448385
}));
449386

450-
const response = await getObjectAttributesWithUserMetadata(s3, {
387+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
451388
Bucket: bucket,
452389
Key: key,
453-
}, 'ETag,x-amz-meta-custom,ObjectSize');
390+
ObjectAttributes: ['ETag', 'x-amz-meta-custom', 'ObjectSize'],
391+
}));
454392

455393
assert.strictEqual(response.ETag, expectedMD5);
456394
assert.strictEqual(response.ObjectSize, body.length);
@@ -469,10 +407,11 @@ describe('objectGetAttributes with user metadata', () => {
469407
},
470408
}));
471409

472-
const response = await getObjectAttributesWithUserMetadata(s3, {
410+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
473411
Bucket: bucket,
474412
Key: key,
475-
}, 'x-amz-meta-*,x-amz-meta-key1');
413+
ObjectAttributes: ['x-amz-meta-*', 'x-amz-meta-key1'],
414+
}));
476415

477416
assert.strictEqual(response['x-amz-meta-key1'], 'value1');
478417
assert.strictEqual(response['x-amz-meta-key2'], 'value2');
@@ -490,10 +429,11 @@ describe('objectGetAttributes with user metadata', () => {
490429
},
491430
}));
492431

493-
const response = await getObjectAttributesWithUserMetadata(s3, {
432+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
494433
Bucket: bucket,
495434
Key: key,
496-
}, 'x-amz-meta-*,x-amz-meta-*');
435+
ObjectAttributes: ['x-amz-meta-*', 'x-amz-meta-*'],
436+
}));
497437

498438
assert.strictEqual(response['x-amz-meta-key1'], 'value1');
499439
assert.strictEqual(response['x-amz-meta-key2'], 'value2');
@@ -510,10 +450,11 @@ describe('objectGetAttributes with user metadata', () => {
510450
},
511451
}));
512452

513-
const response = await getObjectAttributesWithUserMetadata(s3, {
453+
const response = await s3.send(new GetObjectAttributesExtendedCommand({
514454
Bucket: bucket,
515455
Key: key,
516-
}, 'x-amz-meta-key1,x-amz-meta-key1');
456+
ObjectAttributes: ['x-amz-meta-key1', 'x-amz-meta-key1'],
457+
}));
517458

518459
assert.strictEqual(response['x-amz-meta-key1'], 'value1');
519460
assert.strictEqual(response['x-amz-meta-key2'], undefined);

0 commit comments

Comments
 (0)