From 335c0ac842ed83e1cb5f6d040801610d383b2f39 Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Wed, 27 Jan 2016 15:42:37 +0100 Subject: [PATCH 1/2] Handle image loading failure (empty body or statusCode other than 200) --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d946171..ec5bd9f 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,9 @@ var base64encoder = function (url, options, callback) { if (body && res.statusCode === 200) { return encoder(body, options); + } else { + if (!body) { return callback('Image loading error - empty body'); } + else { return callback('Image loading error - ' + res.statusCode); } } }); } @@ -58,4 +61,4 @@ var base64decoder = function (imageBuffer, options, callback) { module.exports = { base64encoder: base64encoder, base64decoder: base64decoder -}; \ No newline at end of file +}; From 27d1ff82e38e854fb698e775067c6358ae9b46db Mon Sep 17 00:00:00 2001 From: Paula Stachova Date: Tue, 2 Feb 2016 13:35:59 +0100 Subject: [PATCH 2/2] Test added for 404 case --- test/tests.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/tests.js b/test/tests.js index fcef8ae..d341890 100644 --- a/test/tests.js +++ b/test/tests.js @@ -34,6 +34,18 @@ describe('Image download and encode/decode to Base64', function () { done(); }); + it('should report if image could not be loaded', function (done) { + var url = 'http://www.pctools.com/security-news/wp-content/uploads/2011/09/nonExisting404.jpg', options; + + base64Image.base64encoder(url, options, function (err, image) { + err.should.exist; + should.not.exist(image); + + done(); + }); + + }); + it('should download an image and return base64 encoded Buffer', function (done) { this.timeout(15000); @@ -49,6 +61,7 @@ describe('Image download and encode/decode to Base64', function () { }); }); + it('should download an image and return base64 encoded string', function (done) { this.timeout(15000);