123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- var should = require('chai').should();
- var imageOptimizer = require('../../lib/tools/redownload/imageOptimizer');
- var fs = require('fs');
- var path = require('path');
- describe('imageOptimizer', function() {
-
- it('should optimize a JPEG image losslessly', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
- var fileSize = fileContent.length;
- imageOptimizer.compressJpegLosslessly(fileContent).then(function(newFile) {
- var newFileSize = newFile.length;
- newFileSize.should.be.below(fileSize);
- done();
- }).fail(function(err) {
- done(err);
- });
- });
- it('should optimize a JPEG image lossly', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
- var fileSize = fileContent.length;
- imageOptimizer.compressJpegLossly(fileContent).then(function(newFile) {
- var newFileSize = newFile.length;
- newFileSize.should.be.below(fileSize);
- done();
- }).fail(function(err) {
- done(err);
- });
- });
- it('should find the best optimization for a jpeg', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/an-image.jpg',
- requestHeaders: {
- 'User-Agent': 'something',
- Referer: 'http://www.google.fr/',
- Accept: '*/*',
- 'Accept-Encoding': 'gzip, deflate'
- },
- status: 200,
- isImage: true,
- type: 'image',
- contentType: 'image/jpeg',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize
- }
- };
- imageOptimizer.optimizeImage(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
- newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
- newEntry.weightCheck.should.have.a.property('lossy').that.is.below(newEntry.weightCheck.lossless);
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should optimize a PNG image losslessly', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
- var fileSize = fileContent.length;
- imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
- var newFileSize = newFile.length;
- newFileSize.should.be.below(fileSize);
- done();
- }).fail(function(err) {
- done(err);
- });
- });
- it('should fail to optimize an already optimized PNG', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/logo-large.png'));
- var fileSize = fileContent.length;
- imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
- var newFileSize = newFile.length;
- newFileSize.should.equal(fileSize);
- done();
- }).fail(function(err) {
- done(err);
- });
- });
- it('should fail to optimize a non-PNG', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
- var fileSize = fileContent.length;
- imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
- var newFileSize = newFile.length;
- newFileSize.should.equal(fileSize);
- done();
- }).fail(function(err) {
- done(err);
- });
- });
- it('should optimize a png', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/an-image.png',
- requestHeaders: {
- 'User-Agent': 'something',
- Referer: 'http://www.google.fr/',
- Accept: '*/*',
- 'Accept-Encoding': 'gzip, deflate'
- },
- status: 200,
- isImage: true,
- type: 'image',
- contentType: 'image/png',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize
- }
- };
- imageOptimizer.optimizeImage(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
- newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should optimize an SVG image losslessly', function(done) {
- this.timeout(5000);
-
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
- var fileSize = fileContent.length;
- imageOptimizer.compressSvgLosslessly(fileContent).then(function(newFile) {
- var newFileSize = newFile.length;
- newFileSize.should.be.below(fileSize);
- done();
- }).fail(function(err) {
- done(err);
- });
- });
- it('should optimize an SVG', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/an-image.svg',
- requestHeaders: {
- 'User-Agent': 'something',
- Referer: 'http://www.google.fr/',
- Accept: '*/*',
- 'Accept-Encoding': 'gzip, deflate'
- },
- status: 200,
- isImage: true,
- isSVG: true,
- type: 'image',
- contentType: 'image/svg+xml',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize
- }
- };
- imageOptimizer.optimizeImage(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
- newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
- newEntry.weightCheck.should.have.a.property('optimized').that.equals(newEntry.weightCheck.lossless);
- newEntry.weightCheck.should.have.a.property('bodyAfterOptimization').that.is.a('string');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('shouldn\'t fail optimizing a corrupted jpeg', function(done) {
- // In this test, we try to optimize a PNG but with a falsy "image/jpeg" content type
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/an-image.png',
- requestHeaders: {
- 'User-Agent': 'something',
- Referer: 'http://www.google.fr/',
- Accept: '*/*',
- 'Accept-Encoding': 'gzip, deflate'
- },
- status: 200,
- isImage: true,
- type: 'image',
- contentType: 'image/jpeg',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize
- }
- };
- imageOptimizer.optimizeImage(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.not.have.a.property('isOptimized');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('shouldn\'t fail optimizing a corrupted png', function(done) {
- // In this test, we try to optimize a JPEG but with a falsy "image/png" content type
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/an-image.jpg',
- requestHeaders: {
- 'User-Agent': 'something',
- Referer: 'http://www.google.fr/',
- Accept: '*/*',
- 'Accept-Encoding': 'gzip, deflate'
- },
- status: 200,
- isImage: true,
- type: 'image',
- contentType: 'image/png',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize
- }
- };
- imageOptimizer.optimizeImage(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.not.have.a.property('isOptimized');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should determine if gain is enough', function() {
- imageOptimizer.gainIsEnough(20000, 10000).should.equal(true);
- imageOptimizer.gainIsEnough(2000, 1000).should.equal(true);
- imageOptimizer.gainIsEnough(20000, 21000).should.equal(false);
- imageOptimizer.gainIsEnough(20000, 40000).should.equal(false);
- imageOptimizer.gainIsEnough(20000, 19500).should.equal(false);
- imageOptimizer.gainIsEnough(250, 120).should.equal(true);
- imageOptimizer.gainIsEnough(200, 120).should.equal(false);
- imageOptimizer.gainIsEnough(2000, 1900).should.equal(false);
- imageOptimizer.gainIsEnough(200000, 197000).should.equal(true);
- });
- });
|