imageOptimizerTest.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var should = require('chai').should();
  2. var imageOptimizer = require('../../lib/tools/weightChecker/imageOptimizer');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('imageOptimizer', function() {
  6. it('should optimize a JPEG image losslessly', function(done) {
  7. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
  8. var fileSize = fileContent.length;
  9. imageOptimizer.compressJpegLosslessly(fileContent).then(function(newFile) {
  10. var newFileSize = newFile.contents.length;
  11. newFileSize.should.be.below(fileSize);
  12. done();
  13. }).fail(function(err) {
  14. done(err);
  15. });
  16. });
  17. it('should optimize a JPEG image lossly', function(done) {
  18. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
  19. var fileSize = fileContent.length;
  20. imageOptimizer.compressJpegLossly(fileContent).then(function(newFile) {
  21. var newFileSize = newFile.contents.length;
  22. newFileSize.should.be.below(fileSize);
  23. done();
  24. }).fail(function(err) {
  25. done(err);
  26. });
  27. });
  28. it('should find the best optimization for a jpeg', function(done) {
  29. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
  30. var fileSize = fileContent.length;
  31. var entry = {
  32. method: 'GET',
  33. url: 'http://localhost:8388/an-image.jpg',
  34. requestHeaders: {
  35. 'User-Agent': 'something',
  36. Referer: 'http://www.google.fr/',
  37. Accept: '*/*',
  38. 'Accept-Encoding': 'gzip, deflate'
  39. },
  40. status: 200,
  41. isImage: true,
  42. type: 'image',
  43. contentType: 'image/jpeg',
  44. contentLength: 999,
  45. weightCheck: {
  46. body: fileContent,
  47. totalWeight: fileSize + 200,
  48. headersSize: 200,
  49. bodySize: fileSize,
  50. isCompressed: false,
  51. uncompressedSize: fileSize
  52. }
  53. };
  54. imageOptimizer.optimizeImage(entry)
  55. .then(function(newEntry) {
  56. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  57. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  58. newEntry.weightCheck.should.have.a.property('lossy').that.is.below(newEntry.weightCheck.lossless);
  59. done();
  60. })
  61. .fail(function(err) {
  62. done(err);
  63. });
  64. });
  65. });