From 1189b1e95b0973e76787b84b411275632353ab04 Mon Sep 17 00:00:00 2001 From: Aileen Nowak Date: Thu, 4 Jan 2018 17:46:26 +0700 Subject: [PATCH] Added test for images w/o extensions (#109) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #97 Images without extensions don't need to be manipulated, as we're now reading the bytes and pass those to the `image-size` lib. This PR adds another `user-agent` to emulate multiple browser requests, as I stumbled over an example where the image without extension is protected otherwise. Added a test, that works with above mentioned image, but is currently mocked. Nevertheless, the image worked as a PoC, that we're able to read the bytes of an image without its extension and still return a valid `amp-img` tag. Updated the README, as there were some code typos in there 🙈. --- README.md | 7 +++---- lib/amperize.js | 2 +- test/amperize.test.js | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 52bc3cc..deb012e 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,12 @@ var Amperize = require('amperize'); var html = ''; -amperize = new Amperize(); +var amperize = new Amperize(); amperize.parse(html, function (error, result) { if (error) { - // do something with error - return new Error(err); - } + // do something with error + return new Error(err); } // do something with result return result; diff --git a/lib/amperize.js b/lib/amperize.js index 889a2b3..2e5a89a 100644 --- a/lib/amperize.js +++ b/lib/amperize.js @@ -181,7 +181,7 @@ Amperize.prototype.traverse = function traverse(data, html, done) { // We need the user-agent, otherwise some https request may fail (e. g. cloudfare) requestOptions = { headers: { - 'User-Agent': 'Mozilla/5.0' + 'User-Agent': 'Mozilla/5.0 Safari/537.36' }, timeout: timeout, encoding: null diff --git a/test/amperize.test.js b/test/amperize.test.js index 32c861f..b10a6fe 100644 --- a/test/amperize.test.js +++ b/test/amperize.test.js @@ -177,6 +177,31 @@ describe('Amperize', function () { }); }); + it('transforms into when no file extension is given', function (done) { + // This test is mocked, but works with this specific example. + // You can comment out the mocks and the test should still pass. + sizeOfMock = nock('https://www.zomato.com') + .matchHeader('User-Agent', /Mozilla\/.*Safari\/.*/) + .get('/logo/18163505/minilogo') + .reply(200, { + body: '' + }); + + sizeOfStub.returns({width: 104, height: 15, type: 'png'}); + Amperize.__set__('sizeOf', sizeOfStub); + + amperize.parse('', function (error, result) { + expect(result).to.exist; + expect(result).to.contain(''); + done(); + }); + }); + it('returns largest image value for .ico files', function (done) { sizeOfMock = nock('https://somewebsite.com') .get('/favicon.ico')