11const assert = require ( 'assert' ) ;
22const async = require ( 'async' ) ;
3+ const {
4+ ListObjectsCommand,
5+ ListObjectVersionsCommand,
6+ DeleteObjectsCommand,
7+ } = require ( '@aws-sdk/client-s3' ) ;
38
49function _deleteVersionList ( s3Client , versionList , bucket , callback ) {
510 if ( versionList === undefined || versionList . length === 0 ) {
611 return callback ( ) ;
712 }
8- const params = { Bucket : bucket , Delete : { Objects : [ ] } } ;
9- versionList . forEach ( version => {
10- params . Delete . Objects . push ( {
11- Key : version . Key , VersionId : version . VersionId } ) ;
12- } ) ;
13-
14- return s3Client . deleteObjects ( params , callback ) ;
13+ const params = {
14+ Bucket : bucket ,
15+ Delete : {
16+ Objects : versionList . map ( version => ( {
17+ Key : version . Key ,
18+ VersionId : version . VersionId ,
19+ } ) ) ,
20+ } ,
21+ } ;
22+ return s3Client . send ( new DeleteObjectsCommand ( params ) )
23+ . then ( ( ) => callback ( ) )
24+ . catch ( callback ) ;
1525}
1626
1727const testUtils = { } ;
@@ -21,62 +31,92 @@ testUtils.runIfMongo = process.env.S3METADATA === 'mongodb' ?
2131
2232testUtils . runAndCheckSearch = ( s3Client , bucketName , encodedSearch , listVersions ,
2333 testResult , done ) => {
24- let searchRequest ;
25- if ( listVersions ) {
26- searchRequest = s3Client . listObjectVersions ( { Bucket : bucketName } ) ;
27- searchRequest . on ( 'build' , ( ) => {
28- searchRequest . httpRequest . path =
29- `/${ bucketName } ?search=${ encodedSearch } &&versions` ;
30- } ) ;
31- searchRequest . on ( 'success' , res => {
32- if ( testResult ) {
33- assert . notStrictEqual ( res . data . Versions [ 0 ] . VersionId , undefined ) ;
34- if ( Array . isArray ( testResult ) ) {
35- assert . strictEqual ( res . data . Versions . length , testResult . length ) ;
36- async . forEachOf ( testResult , ( expected , i , next ) => {
37- assert . strictEqual ( res . data . Versions [ i ] . Key , expected ) ;
38- next ( ) ;
39- } ) ;
34+ const makeRequest = async ( ) => {
35+ try {
36+ const input = {
37+ Bucket : bucketName ,
38+ } ;
39+
40+ let command ;
41+ if ( listVersions ) {
42+ command = new ListObjectVersionsCommand ( input ) ;
43+ } else {
44+ command = new ListObjectsCommand ( input ) ;
45+ }
46+
47+ // Add middleware to inject the search query parameter
48+ // SDK v3 automatically encodes query parameters, so we decode first to avoid double-encoding
49+ command . middlewareStack . add (
50+ ( next ) => async ( args ) => {
51+ if ( ! args . request . query ) {
52+ args . request . query = { } ;
53+ }
54+ // Decode the already-encoded search string since SDK v3 will encode it again
55+ args . request . query . search = decodeURIComponent ( encodedSearch ) ;
56+ if ( listVersions ) {
57+ args . request . query . versions = '' ;
58+ }
59+
60+ return next ( args ) ;
61+ } ,
62+ {
63+ step : 'build' ,
64+ name : 'addSearchQuery' ,
65+ }
66+ ) ;
67+
68+ const res = await s3Client . send ( command ) ;
69+
70+ if ( listVersions ) {
71+ if ( testResult ) {
72+ assert . notStrictEqual ( res . Versions [ 0 ] . VersionId , undefined ) ;
73+ if ( Array . isArray ( testResult ) ) {
74+ assert . strictEqual ( res . Versions . length , testResult . length ) ;
75+ async . forEachOf ( testResult , ( expected , i , next ) => {
76+ assert . strictEqual ( res . Versions [ i ] . Key , expected ) ;
77+ next ( ) ;
78+ } , done ) ;
79+ return ;
80+ } else {
81+ assert ( res . Versions [ 0 ] , 'should be Contents listed' ) ;
82+ assert . strictEqual ( res . Versions [ 0 ] . Key , testResult ) ;
83+ assert . strictEqual ( res . Versions . length , 1 ) ;
84+ }
4085 } else {
41- assert ( res . data . Versions [ 0 ] , 'should be Contents listed' ) ;
42- assert . strictEqual ( res . data . Versions [ 0 ] . Key , testResult ) ;
43- assert . strictEqual ( res . data . Versions . length , 1 ) ;
86+ assert . strictEqual ( res . Versions . length , 0 ) ;
4487 }
4588 } else {
46- assert . strictEqual ( res . data . Versions . length , 0 ) ;
89+ if ( testResult && typeof testResult === 'object' && testResult . code ) {
90+ // This was expected to be an error, but we got success
91+ return done ( new Error ( 'Expected error but got success' ) ) ;
92+ }
93+ if ( testResult ) {
94+ assert ( res . Contents [ 0 ] , 'should be Contents listed' ) ;
95+ assert . strictEqual ( res . Contents [ 0 ] . Key , testResult ) ;
96+ assert . strictEqual ( res . Contents . length , 1 ) ;
97+ } else {
98+ assert . strictEqual ( res . Contents ?. length , undefined ) ;
99+ }
47100 }
48101 return done ( ) ;
49- } ) ;
50- } else {
51- searchRequest = s3Client . listObjects ( { Bucket : bucketName } ) ;
52- searchRequest . on ( 'build' , ( ) => {
53- searchRequest . httpRequest . path =
54- `/${ bucketName } ?search=${ encodedSearch } ` ;
55- } ) ;
56- searchRequest . on ( 'success' , res => {
57- if ( testResult ) {
58- assert ( res . data . Contents [ 0 ] , 'should be Contents listed' ) ;
59- assert . strictEqual ( res . data . Contents [ 0 ] . Key , testResult ) ;
60- assert . strictEqual ( res . data . Contents . length , 1 ) ;
61- } else {
62- assert . strictEqual ( res . data . Contents . length , 0 ) ;
102+ } catch ( err ) {
103+ if ( testResult && typeof testResult === 'object' && testResult . code ) {
104+ assert . strictEqual ( err . name , testResult . code ) ;
105+ assert . strictEqual ( err . message , testResult . message ) ;
106+ return done ( ) ;
63107 }
64- return done ( ) ;
65- } ) ;
66- }
67- searchRequest . on ( 'error' , err => {
68- if ( testResult ) {
69- assert . strictEqual ( err . code , testResult . code ) ;
70- assert . strictEqual ( err . message , testResult . message ) ;
108+ return done ( err ) ;
71109 }
72- return done ( ) ;
73- } ) ;
74- searchRequest . send ( ) ;
110+ } ;
111+
112+ makeRequest ( ) ;
75113} ;
76114
77115testUtils . removeAllVersions = ( s3Client , bucket , callback ) => {
78116 async . waterfall ( [
79- cb => s3Client . listObjectVersions ( { Bucket : bucket } , cb ) ,
117+ cb => s3Client . send ( new ListObjectVersionsCommand ( { Bucket : bucket } ) )
118+ . then ( data => cb ( null , data ) )
119+ . catch ( cb ) ,
80120 ( data , cb ) => _deleteVersionList ( s3Client , data . DeleteMarkers , bucket ,
81121 err => cb ( err , data ) ) ,
82122 ( data , cb ) => _deleteVersionList ( s3Client , data . Versions , bucket ,
@@ -88,7 +128,7 @@ testUtils.removeAllVersions = (s3Client, bucket, callback) => {
88128 KeyMarker : data . NextKeyMarker ,
89129 VersionIdMarker : data . NextVersionIdMarker ,
90130 } ;
91- return this . removeAllVersions ( params , cb ) ;
131+ return testUtils . removeAllVersions ( s3Client , params , cb ) ;
92132 }
93133 return cb ( ) ;
94134 } ,
0 commit comments