imageOptimizerTest.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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, '../www/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, '../www/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, '../www/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, '../www/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 fail to optimize an already optimized PNG', function(done) {
  77. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/logo-large.png'));
  78. var fileSize = fileContent.length;
  79. imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
  80. var newFileSize = newFile.contents.length;
  81. newFileSize.should.equal(fileSize);
  82. done();
  83. }).fail(function(err) {
  84. done(err);
  85. });
  86. });
  87. it('should fail to optimize a non-PNG', function(done) {
  88. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  89. var fileSize = fileContent.length;
  90. imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
  91. var newFileSize = newFile.contents.length;
  92. newFileSize.should.equal(fileSize);
  93. done();
  94. }).fail(function(err) {
  95. done(err);
  96. });
  97. });
  98. it('should optimize a png', function(done) {
  99. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
  100. var fileSize = fileContent.length;
  101. var entry = {
  102. method: 'GET',
  103. url: 'http://localhost:8388/an-image.png',
  104. requestHeaders: {
  105. 'User-Agent': 'something',
  106. Referer: 'http://www.google.fr/',
  107. Accept: '*/*',
  108. 'Accept-Encoding': 'gzip, deflate'
  109. },
  110. status: 200,
  111. isImage: true,
  112. type: 'image',
  113. contentType: 'image/png',
  114. contentLength: 999,
  115. weightCheck: {
  116. body: fileContent,
  117. totalWeight: fileSize + 200,
  118. headersSize: 200,
  119. bodySize: fileSize,
  120. isCompressed: false,
  121. uncompressedSize: fileSize
  122. }
  123. };
  124. imageOptimizer.optimizeImage(entry)
  125. .then(function(newEntry) {
  126. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  127. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  128. done();
  129. })
  130. .fail(function(err) {
  131. done(err);
  132. });
  133. });
  134. it('should optimize an SVG image losslessly', function(done) {
  135. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  136. var fileSize = fileContent.length;
  137. imageOptimizer.compressSvgLosslessly(fileContent).then(function(newFile) {
  138. var newFileSize = newFile.contents.length;
  139. newFileSize.should.be.below(fileSize);
  140. done();
  141. }).fail(function(err) {
  142. done(err);
  143. });
  144. });
  145. it('should optimize an SVG', function(done) {
  146. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  147. var fileSize = fileContent.length;
  148. var entry = {
  149. method: 'GET',
  150. url: 'http://localhost:8388/an-image.svg',
  151. requestHeaders: {
  152. 'User-Agent': 'something',
  153. Referer: 'http://www.google.fr/',
  154. Accept: '*/*',
  155. 'Accept-Encoding': 'gzip, deflate'
  156. },
  157. status: 200,
  158. isImage: true,
  159. type: 'image',
  160. contentType: 'image/svg+xml',
  161. contentLength: 999,
  162. weightCheck: {
  163. body: fileContent,
  164. totalWeight: fileSize + 200,
  165. headersSize: 200,
  166. bodySize: fileSize,
  167. isCompressed: false,
  168. uncompressedSize: fileSize
  169. }
  170. };
  171. imageOptimizer.optimizeImage(entry)
  172. .then(function(newEntry) {
  173. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  174. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  175. done();
  176. })
  177. .fail(function(err) {
  178. done(err);
  179. });
  180. });
  181. it('shouldn\'t fail optimizing a corrupted jpeg', function(done) {
  182. // In this test, we try to optimize a PNG but with a falsy "image/jpeg" content type
  183. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
  184. var fileSize = fileContent.length;
  185. var entry = {
  186. method: 'GET',
  187. url: 'http://localhost:8388/an-image.png',
  188. requestHeaders: {
  189. 'User-Agent': 'something',
  190. Referer: 'http://www.google.fr/',
  191. Accept: '*/*',
  192. 'Accept-Encoding': 'gzip, deflate'
  193. },
  194. status: 200,
  195. isImage: true,
  196. type: 'image',
  197. contentType: 'image/jpeg',
  198. contentLength: 999,
  199. weightCheck: {
  200. body: fileContent,
  201. totalWeight: fileSize + 200,
  202. headersSize: 200,
  203. bodySize: fileSize,
  204. isCompressed: false,
  205. uncompressedSize: fileSize
  206. }
  207. };
  208. imageOptimizer.optimizeImage(entry)
  209. .then(function(newEntry) {
  210. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  211. done();
  212. })
  213. .fail(function(err) {
  214. done(err);
  215. });
  216. });
  217. it('shouldn\'t fail optimizing a corrupted png', function(done) {
  218. // In this test, we try to optimize a JPEG but with a falsy "image/png" content type
  219. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  220. var fileSize = fileContent.length;
  221. var entry = {
  222. method: 'GET',
  223. url: 'http://localhost:8388/an-image.jpg',
  224. requestHeaders: {
  225. 'User-Agent': 'something',
  226. Referer: 'http://www.google.fr/',
  227. Accept: '*/*',
  228. 'Accept-Encoding': 'gzip, deflate'
  229. },
  230. status: 200,
  231. isImage: true,
  232. type: 'image',
  233. contentType: 'image/png',
  234. contentLength: 999,
  235. weightCheck: {
  236. body: fileContent,
  237. totalWeight: fileSize + 200,
  238. headersSize: 200,
  239. bodySize: fileSize,
  240. isCompressed: false,
  241. uncompressedSize: fileSize
  242. }
  243. };
  244. imageOptimizer.optimizeImage(entry)
  245. .then(function(newEntry) {
  246. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  247. done();
  248. })
  249. .fail(function(err) {
  250. done(err);
  251. });
  252. });
  253. it('should determine if gain is enough', function() {
  254. imageOptimizer.gainIsEnough(20000, 10000).should.equal(true);
  255. imageOptimizer.gainIsEnough(2000, 1000).should.equal(true);
  256. imageOptimizer.gainIsEnough(20000, 21000).should.equal(false);
  257. imageOptimizer.gainIsEnough(20000, 40000).should.equal(false);
  258. imageOptimizer.gainIsEnough(20000, 19500).should.equal(false);
  259. imageOptimizer.gainIsEnough(250, 120).should.equal(true);
  260. imageOptimizer.gainIsEnough(200, 120).should.equal(false);
  261. imageOptimizer.gainIsEnough(2000, 1900).should.equal(false);
  262. imageOptimizer.gainIsEnough(200000, 197000).should.equal(true);
  263. });
  264. });