imageOptimizerTest.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. it('should optimize a PNG image losslessly', function(done) {
  66. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/png-image.png'));
  67. var fileSize = fileContent.length;
  68. imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
  69. var newFileSize = newFile.contents.length;
  70. newFileSize.should.be.below(fileSize);
  71. done();
  72. }).fail(function(err) {
  73. done(err);
  74. });
  75. });
  76. it('should optimize a png', function(done) {
  77. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/png-image.png'));
  78. var fileSize = fileContent.length;
  79. var entry = {
  80. method: 'GET',
  81. url: 'http://localhost:8388/an-image.png',
  82. requestHeaders: {
  83. 'User-Agent': 'something',
  84. Referer: 'http://www.google.fr/',
  85. Accept: '*/*',
  86. 'Accept-Encoding': 'gzip, deflate'
  87. },
  88. status: 200,
  89. isImage: true,
  90. type: 'image',
  91. contentType: 'image/png',
  92. contentLength: 999,
  93. weightCheck: {
  94. body: fileContent,
  95. totalWeight: fileSize + 200,
  96. headersSize: 200,
  97. bodySize: fileSize,
  98. isCompressed: false,
  99. uncompressedSize: fileSize
  100. }
  101. };
  102. imageOptimizer.optimizeImage(entry)
  103. .then(function(newEntry) {
  104. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  105. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  106. done();
  107. })
  108. .fail(function(err) {
  109. done(err);
  110. });
  111. });
  112. it('shouldn\'t fail optimizing a corrupted jpeg', function(done) {
  113. // In this test, we try to optimize a PNG but with a falsy "image/jpeg" content type
  114. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/png-image.png'));
  115. var fileSize = fileContent.length;
  116. var entry = {
  117. method: 'GET',
  118. url: 'http://localhost:8388/an-image.png',
  119. requestHeaders: {
  120. 'User-Agent': 'something',
  121. Referer: 'http://www.google.fr/',
  122. Accept: '*/*',
  123. 'Accept-Encoding': 'gzip, deflate'
  124. },
  125. status: 200,
  126. isImage: true,
  127. type: 'image',
  128. contentType: 'image/jpeg',
  129. contentLength: 999,
  130. weightCheck: {
  131. body: fileContent,
  132. totalWeight: fileSize + 200,
  133. headersSize: 200,
  134. bodySize: fileSize,
  135. isCompressed: false,
  136. uncompressedSize: fileSize
  137. }
  138. };
  139. imageOptimizer.optimizeImage(entry)
  140. .then(function(newEntry) {
  141. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  142. done();
  143. })
  144. .fail(function(err) {
  145. done(err);
  146. });
  147. });
  148. it('shouldn\'t fail optimizing a corrupted png', function(done) {
  149. // In this test, we try to optimize a JPEG but with a falsy "image/png" content type
  150. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
  151. var fileSize = fileContent.length;
  152. var entry = {
  153. method: 'GET',
  154. url: 'http://localhost:8388/an-image.jpg',
  155. requestHeaders: {
  156. 'User-Agent': 'something',
  157. Referer: 'http://www.google.fr/',
  158. Accept: '*/*',
  159. 'Accept-Encoding': 'gzip, deflate'
  160. },
  161. status: 200,
  162. isImage: true,
  163. type: 'image',
  164. contentType: 'image/png',
  165. contentLength: 999,
  166. weightCheck: {
  167. body: fileContent,
  168. totalWeight: fileSize + 200,
  169. headersSize: 200,
  170. bodySize: fileSize,
  171. isCompressed: false,
  172. uncompressedSize: fileSize
  173. }
  174. };
  175. imageOptimizer.optimizeImage(entry)
  176. .then(function(newEntry) {
  177. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  178. done();
  179. })
  180. .fail(function(err) {
  181. done(err);
  182. });
  183. });
  184. });