Skip to content

Commit

Permalink
Adding tests for trying to read a missing file.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonlehman committed Apr 10, 2015
1 parent 2b5607b commit 4cea8d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Licensed under the Apache License, Version 2.0. See the top-level file LICENSE.t
[npm-url]: https://npmjs.org/package/4front-s3-deployments
[travis-image]: https://img.shields.io/travis/4front/s3-deployments.svg?style=flat
[travis-url]: https://travis-ci.org/4front/s3-deployments
[coveralls-image]: https://img.shields.io/coveralls/4front-s3-deployments.svg?style=flat
[coveralls-image]: https://img.shields.io/coveralls/4front/s3-deployments.svg?style=flat
[coveralls-url]: https://coveralls.io/r/4front/s3-deployments?branch=master
[downloads-image]: https://img.shields.io/npm/dm/4front-s3-deployments.svg?style=flat
[downloads-url]: https://npmjs.org/package/4front-s3-deployments
16 changes: 15 additions & 1 deletion lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var mime = require('mime');
var debug = require('debug')('4front-s3-deployments');
var AWS = require('aws-sdk');

require('simple-errors');

module.exports = S3Deployments = function(options) {
this.options = _.defaults(options || {}, {
maxAge: 30 * 60 * 30
Expand Down Expand Up @@ -41,7 +43,19 @@ S3Deployments.prototype.deployFile = function(appId, versionId, fileInfo, callba

S3Deployments.prototype.readFileStream = function(appId, versionId, filePath) {
var storageKey = appId + '/' + versionId + '/' + filePath;
return this._s3.getObject({Bucket: this.options.bucket, Key: storageKey }).createReadStream();
var s3Object = this._s3.getObject({Bucket: this.options.bucket, Key: storageKey });

return s3Object.createReadStream()
.on('readable', function() {
// For some reason need to listen for "readable" event in order for the
// "error" event to be emitted.
})
.on('error', function(err) {
if (err.code === 'NoSuchKey')
this.emit('missing', Error.create("Object with key " + storageKey + " not found.", {code: "fileNotFound"}));
else
this.emit('end');
});
};

// Delete a deployed version of an app
Expand Down
11 changes: 11 additions & 0 deletions test/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ describe('S3Deployments', function() {
});
});

it('read missing file', function(done) {
this.s3.readFileStream(this.appId, this.versionId, 'missingfile.txt')
.on('missing', function(err) {
assert.equal(err.code, 'fileNotFound');
done();
})
.on('end', function() {
assert.fail("error event should have fired");
});
});

it('delete version', function(done) {
var files = ['js/main.js', 'css/styles.css'];

Expand Down

0 comments on commit 4cea8d7

Please sign in to comment.