From 635fc33823d3858dafef074026a1d4a0faf705e7 Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Tue, 16 Aug 2016 16:00:05 -0400 Subject: [PATCH 1/2] Add test for beforeRemote hook on download. --- test/upload-download.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/upload-download.test.js b/test/upload-download.test.js index 029378f..4c70074 100644 --- a/test/upload-download.test.js +++ b/test/upload-download.test.js @@ -253,6 +253,25 @@ describe('storage service', function () { }); }); + it('should run a function before a download is started by a client', function(done) { + var hookCalled = false; + + var Container = app.models.Container; + + Container.beforeRemote('download', function(ctx, unused, cb) { + hookCalled = true; + cb(); + }); + + request('http://localhost:' + app.get('port')) + .get('/containers/album1/download/test.jpg') + .expect('Content-Type', 'image/jpeg') + .expect(200, function(err, res) { + assert(hookCalled, 'beforeRemote hook was not called'); + done(); + }); + }); + it('should delete a file', function (done) { request('http://localhost:' + app.get('port')) From 20d9aaadf22555a90029211cf4809c6b77c42440 Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Tue, 16 Aug 2016 16:07:48 -0400 Subject: [PATCH 2/2] Continue middleware chain after download. --- lib/storage-handler.js | 6 ++++++ test/upload-download.test.js | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/storage-handler.js b/lib/storage-handler.js index 52d1d25..19275a6 100644 --- a/lib/storage-handler.js +++ b/lib/storage-handler.js @@ -245,6 +245,9 @@ exports.download = function(provider, req, res, container, file, cb) { reader.on('error', function(err) { handleError(res, err); }); + reader.on('end', function() { + cb(); + }); } }); @@ -258,6 +261,9 @@ exports.download = function(provider, req, res, container, file, cb) { reader.on('error', function(err) { handleError(res, err); }); + reader.on('end', function() { + cb(); + }); } } }; diff --git a/test/upload-download.test.js b/test/upload-download.test.js index 4c70074..8950512 100644 --- a/test/upload-download.test.js +++ b/test/upload-download.test.js @@ -249,6 +249,7 @@ describe('storage service', function () { .get('/containers/album1/download/test.jpg') .expect('Content-Type', 'image/jpeg') .expect(200, function (err, res) { + if (err) done(err); done(); }); }); @@ -267,11 +268,32 @@ describe('storage service', function () { .get('/containers/album1/download/test.jpg') .expect('Content-Type', 'image/jpeg') .expect(200, function(err, res) { + if (err) done(err); assert(hookCalled, 'beforeRemote hook was not called'); done(); }); }); + it('should run a function after a download is started by a client', function(done) { + var hookCalled = false; + + var Container = app.models.Container; + + Container.afterRemote('download', function(ctx, unused, cb) { + hookCalled = true; + cb(); + }); + + request('http://localhost:' + app.get('port')) + .get('/containers/album1/download/test.jpg') + .expect('Content-Type', 'image/jpeg') + .expect(200, function(err, res) { + if (err) done(err); + assert(hookCalled, 'afterRemote hook was not called'); + done(); + }); + }); + it('should delete a file', function (done) { request('http://localhost:' + app.get('port'))