123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- var should = require('chai').should();
- var gzipCompressor = require('../../lib/tools/redownload/gzipCompressor');
- var fileMinifier = require('../../lib/tools/redownload/fileMinifier');
- var fs = require('fs');
- var path = require('path');
- describe('gzipCompressor', function() {
- var minifiedJSContent = fs.readFileSync(path.resolve(__dirname, '../www/minified-script.js'));
- var notMinifiedJSContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-script.js'));
- var someTextFileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
-
- it('should gzip a JS file that was not gziped but was minified', function(done) {
- var fileContent = minifiedJSContent;
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/minified-script.js',
- status: 200,
- isJS: true,
- type: 'js',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip a JS file that was not gziped and not minified', function(done) {
- /*jshint expr: true*/
- var fileContent = notMinifiedJSContent;
- var minifiedContent = minifiedJSContent;
- var fileSize = fileContent.length;
- var minifiedSize = minifiedContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/unminified-script.js',
- status: 200,
- isJS: true,
- type: 'js',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- bodyAfterOptimization: minifiedContent.toString('utf8'),
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: false,
- optimized: minifiedSize
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.not.undefined;
- newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.below(newEntry.weightCheck.afterCompression);
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip a JS file that is gziped but not minified', function(done) {
- /*jshint expr: true*/
- var fileContent = notMinifiedJSContent;
- var minifiedContent = minifiedJSContent;
- var fileSize = 6436;
- var gzipedSize = 2646;
- var minifiedSize = 1954;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/unminified-script.js',
- status: 200,
- isJS: true,
- type: 'js',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- bodyAfterOptimization: minifiedContent.toString('utf8'),
- totalWeight: gzipedSize + 200,
- headersSize: 200,
- bodySize: gzipedSize,
- isCompressed: true,
- uncompressedSize: fileSize,
- isOptimized: false,
- optimized: minifiedSize
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.not.undefined;
- newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.below(gzipedSize);
- newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.below(minifiedSize);
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should not gzip a JS file that was gziped and minified', function(done) {
- /*jshint expr: true*/
- var fileContent = notMinifiedJSContent;
- var fileSize = 6436;
- var gzipedSize = 2646;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/unminified-script.js',
- status: 200,
- isJS: true,
- type: 'js',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: gzipedSize + 200,
- headersSize: 200,
- bodySize: gzipedSize,
- isCompressed: true,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.not.have.a.property('minified');
- newEntry.weightCheck.should.not.have.a.property('bodyAfterOptimization');
- newEntry.weightCheck.should.not.have.a.property('afterCompression');
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip a CSS file', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-stylesheet.css'));
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/unminified-stylesheet.css',
- status: 200,
- isCSS: true,
- type: 'css',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip an HTML file', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jquery-page.html'));
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/jquery-page.html',
- status: 200,
- isHTML: true,
- type: 'html',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip an SVG file', 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/svg-image.svg',
- status: 200,
- isImage: true,
- isSVG: true,
- type: 'image',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip an XML file', function(done) {
- var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/someTextFile.xml',
- status: 200,
- isXML: true,
- type: 'xml',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip a JSON file', function(done) {
- var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/someTextFile.json',
- status: 200,
- isJSON: true,
- type: 'json',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip a TTF file', function(done) {
- var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/someTextFile.ttf',
- status: 200,
- isWebFont: true,
- isTTF: true,
- type: 'webfont',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should gzip a favicon file', function(done) {
- var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/someTextFile.ico',
- status: 200,
- isFavicon: true,
- type: 'favicon',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should not gzip a JPEG file', 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/jpeg-image.jpg',
- 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,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.not.have.a.property('afterCompression');
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should not gzip a PNG file', 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/png-image.png',
- 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,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.not.have.a.property('afterCompression');
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should not gzip a GIF file', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png')); // Fake gif, don't tell anyone...
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/gif-image.gif',
- status: 200,
- isImage: true,
- type: 'image',
- contentType: 'image/gif',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.not.have.a.property('afterCompression');
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- it('should not gzip a WEBP file', function(done) {
- var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png')); // Fake webp, don't tell anyone...
- var fileSize = fileContent.length;
- var entry = {
- method: 'GET',
- url: 'http://localhost:8388/webp-image.webp',
- status: 200,
- isImage: true,
- type: 'image',
- contentType: 'image/webp',
- contentLength: 999,
- weightCheck: {
- bodyBuffer: fileContent,
- totalWeight: fileSize + 200,
- headersSize: 200,
- bodySize: fileSize,
- isCompressed: false,
- uncompressedSize: fileSize,
- isOptimized: true
- }
- };
- gzipCompressor.compressFile(entry)
- .then(function(newEntry) {
- newEntry.weightCheck.should.not.have.a.property('afterCompression');
- newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
- done();
- })
- .fail(function(err) {
- done(err);
- });
- });
- });
|