From dc08729155f62b5f585599afa5e598ffa1364f31 Mon Sep 17 00:00:00 2001 From: Jamie Hall Date: Wed, 28 Jan 2015 12:55:51 +0100 Subject: [PATCH] Implemented custom metadata tests --- .jshintrc | 2 +- lib/controllers.js | 9 +++++++-- lib/file-store.js | 7 +++++-- lib/models/s3-object.js | 2 +- test/test.js | 25 +++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/.jshintrc b/.jshintrc index 7eb8ccf9..79cf5517 100644 --- a/.jshintrc +++ b/.jshintrc @@ -23,7 +23,7 @@ "maxparams" : false, // {int} Max number of formal params allowed per function "maxdepth" : false, // {int} Max depth of nested blocks (within functions) "maxstatements" : false, // {int} Max number statements per function - "maxcomplexity" : 7, // {int} Max cyclomatic complexity per function + "maxcomplexity" : 8, // {int} Max cyclomatic complexity per function "maxlen" : false, // {int} Max number of characters per line // Relaxing diff --git a/lib/controllers.js b/lib/controllers.js index 1c7cea98..fa1c0a01 100644 --- a/lib/controllers.js +++ b/lib/controllers.js @@ -58,8 +58,8 @@ module.exports = function (rootDirectory, logger) { */ if ((/^[a-z0-9]+(-[a-z0-9]+)*$/.test(bucketName) === false)) { template = templateBuilder.buildError('InvalidBucketName', - 'Bucket names can contain lowercase letters, numbers, and hyphens. ' + - 'Each label must start and end with a lowercase letter or a number.'); + 'Bucket names can contain lowercase letters, numbers, and hyphens. ' + + 'Each label must start and end with a lowercase letter or a number.'); return buildXmlResponse(res, 400, template); } if (bucketName.length < 3 || bucketName.length > 63) { @@ -125,6 +125,11 @@ module.exports = function (rootDirectory, logger) { res.header('Last-Modified', new Date(object.modifiedDate).toUTCString()); res.header('Content-Type', object.contentType); res.header('Content-Length', object.size); + if (object.customMetaData.length > 0) { + object.customMetaData.forEach(function (metaData) { + res.header(metaData.key, metaData.value); + }); + } res.status(200); if (req.method === 'HEAD') { return res.end(); diff --git a/lib/file-store.js b/lib/file-store.js index 79947b2f..47a80adb 100644 --- a/lib/file-store.js +++ b/lib/file-store.js @@ -150,7 +150,10 @@ var FileStore = function (rootDirectory) { var customMetaData = []; for (var header in headers) { if (/^x-amz-meta-(.*)$/.test(header)) { - customMetaData.push(header); + customMetaData.push({ + key: header, + value: headers[header] + }); } } return customMetaData; @@ -188,7 +191,7 @@ var FileStore = function (rootDirectory) { size: results[1].size, modifiedDate: results[0].mtime, creationDate: results[0].ctime, - customMetadata: getCustomMetaData(headers) + customMetaData: getCustomMetaData(headers) }; fs.writeFile(metaFile, JSON.stringify(metaData), function (err) { if(err) { diff --git a/lib/models/s3-object.js b/lib/models/s3-object.js index 33e5af57..952da3ef 100644 --- a/lib/models/s3-object.js +++ b/lib/models/s3-object.js @@ -7,7 +7,7 @@ var S3Object = function (s3Item) { size: s3Item.size, modifiedDate: s3Item.modifiedDate, creationDate: s3Item.creationDate, - customMetaData: s3Item.customMetadata + customMetaData: s3Item.customMetaData }; return item; }; diff --git a/test/test.js b/test/test.js index ccae9024..eaf75fc0 100644 --- a/test/test.js +++ b/test/test.js @@ -150,6 +150,31 @@ describe('S3rver Tests', function () { }); }); + it('should store a text object with some custom metadata', function (done) { + var params = { + Bucket: buckets[0], Key: 'textmetadata', Body: 'Hello!', Metadata: { + someKey: 'value' + } + }; + s3Client.putObject(params, function (err, data) { + /[a-fA-F0-9]{32}/.test(data.ETag).should.equal(true); + if (err) { + return done(err); + } + done(); + }); + }); + + it('should return a text object with some custom metadata', function (done) { + s3Client.getObject({Bucket: buckets[0], Key: 'textmetadata'}, function (err, object) { + if (err) { + return done(err); + } + object.Metadata.somekey.should.equal('value'); + done(); + }); + }); + it('should store an image in a bucket', function (done) { var file = path.join(__dirname, 'resources/image.jpg'); fs.readFile(file, function (err, data) {