Add Image Optimization rule to the dashbard

This commit is contained in:
Gaël Métais
2015-06-01 14:57:05 +02:00
parent c428c8ae2d
commit 1606cbab18
10 changed files with 351 additions and 133 deletions
+85 -4
View File
@@ -1,5 +1,7 @@
var should = require('chai').should();
var weightChecker = require('../../lib/tools/weightChecker/weightChecker');
var fs = require('fs');
var path = require('path');
describe('weightChecker', function() {
@@ -14,7 +16,8 @@ describe('weightChecker', function() {
Accept: '*/*'
},
status: 200,
isHTML: true
isHTML: true,
type: 'html'
},
{
method: 'GET',
@@ -25,7 +28,34 @@ describe('weightChecker', function() {
Accept: '*/*'
},
status: 200,
isJS: true
isJS: true,
type: 'js'
},
{
method: 'GET',
url: 'http://localhost:8388/jpeg-image.jpg',
requestHeaders: {
'User-Agent': 'something',
Referer: 'http://www.google.fr/',
Accept: '*/*'
},
status: 200,
isImage: true,
type: 'image',
contentType: 'image/jpeg'
},
{
method: 'GET',
url: 'http://localhost:8388/svg-image.svg',
requestHeaders: {
'User-Agent': 'something',
Referer: 'http://www.google.fr/',
Accept: '*/*'
},
status: 200,
isImage: true,
type: 'image',
contentType: 'image/svg+xml'
}
];
@@ -48,6 +78,18 @@ describe('weightChecker', function() {
data.toolsResults.should.have.a.property('weightChecker');
data.toolsResults.weightChecker.should.have.a.property('metrics');
data.toolsResults.weightChecker.should.have.a.property('offenders');
data.toolsResults.weightChecker.offenders.should.have.a.property('totalWeight');
data.toolsResults.weightChecker.offenders.totalWeight.totalWeight.should.be.above(0);
data.toolsResults.weightChecker.offenders.totalWeight.byType.html.requests.length.should.equal(1);
data.toolsResults.weightChecker.offenders.totalWeight.byType.js.requests.length.should.equal(1);
data.toolsResults.weightChecker.offenders.totalWeight.byType.image.requests.length.should.equal(2);
data.toolsResults.weightChecker.offenders.should.have.a.property('imageOptimization');
data.toolsResults.weightChecker.offenders.imageOptimization.totalGain.should.be.above(0);
data.toolsResults.weightChecker.offenders.imageOptimization.byType.jpeg.requests.length.should.equal(1);
data.toolsResults.weightChecker.offenders.imageOptimization.byType.svg.requests.length.should.equal(1);
done();
})
@@ -77,7 +119,7 @@ describe('weightChecker', function() {
newEntry.weightCheck.uncompressedSize.should.equal(newEntry.weightCheck.bodySize);
newEntry.weightCheck.isCompressed.should.equal(false);
newEntry.weightCheck.headersSize.should.be.above(200).and.below(400);
newEntry.weightCheck.body.should.have.string('1.8.3');
newEntry.weightCheck.body.toString().should.have.string('1.8.3');
done();
})
@@ -87,6 +129,45 @@ describe('weightChecker', function() {
});
});
it('should download a PNG image and find the same body as fs.readFile', function(done) {
var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/logo-large.png'));
var entry = {
method: 'GET',
url: 'http://localhost:8388/logo-large.png',
requestHeaders: {
'User-Agent': 'something',
Referer: 'http://www.google.fr/',
Accept: '*/*'
},
status: 200,
isImage: true,
contentType: 'image/png'
};
weightChecker.redownloadEntry(entry)
.then(function(newEntry) {
newEntry.weightCheck.bodySize.should.equal(4193);
newEntry.weightCheck.body.should.equal(fileContent.toString('binary'));
// Opening the image in lwip to check if the format is good
var lwip = require('lwip');
var buffer = new Buffer(newEntry.weightCheck.body, 'binary');
lwip.open(buffer, 'png', function(err, image) {
image.width().should.equal(620);
image.height().should.equal(104);
done(err);
});
})
.fail(function(err) {
done(err);
});
});
it('should fail downloading a file in error', function(done) {
var entry = {
method: 'GET',
@@ -103,7 +184,7 @@ describe('weightChecker', function() {
weightChecker.redownloadEntry(entry)
.then(function(errnewEntry) {
.then(function(newEntry) {
newEntry.weightCheck.should.have.a.property('message').that.equals('error while downloading: 404');
done();