imageOptimizerTest.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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('should optimize an SVG image losslessly', function(done) {
  113. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/svg-image.svg'));
  114. var fileSize = fileContent.length;
  115. imageOptimizer.compressSvgLosslessly(fileContent).then(function(newFile) {
  116. var newFileSize = newFile.contents.length;
  117. newFileSize.should.be.below(fileSize);
  118. done();
  119. }).fail(function(err) {
  120. done(err);
  121. });
  122. });
  123. it('should optimize an SVG', function(done) {
  124. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/svg-image.svg'));
  125. var fileSize = fileContent.length;
  126. var entry = {
  127. method: 'GET',
  128. url: 'http://localhost:8388/an-image.svg',
  129. requestHeaders: {
  130. 'User-Agent': 'something',
  131. Referer: 'http://www.google.fr/',
  132. Accept: '*/*',
  133. 'Accept-Encoding': 'gzip, deflate'
  134. },
  135. status: 200,
  136. isImage: true,
  137. type: 'image',
  138. contentType: 'image/svg+xml',
  139. contentLength: 999,
  140. weightCheck: {
  141. body: fileContent,
  142. totalWeight: fileSize + 200,
  143. headersSize: 200,
  144. bodySize: fileSize,
  145. isCompressed: false,
  146. uncompressedSize: fileSize
  147. }
  148. };
  149. imageOptimizer.optimizeImage(entry)
  150. .then(function(newEntry) {
  151. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  152. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  153. done();
  154. })
  155. .fail(function(err) {
  156. done(err);
  157. });
  158. });
  159. it('shouldn\'t fail optimizing a corrupted jpeg', function(done) {
  160. // In this test, we try to optimize a PNG but with a falsy "image/jpeg" content type
  161. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/png-image.png'));
  162. var fileSize = fileContent.length;
  163. var entry = {
  164. method: 'GET',
  165. url: 'http://localhost:8388/an-image.png',
  166. requestHeaders: {
  167. 'User-Agent': 'something',
  168. Referer: 'http://www.google.fr/',
  169. Accept: '*/*',
  170. 'Accept-Encoding': 'gzip, deflate'
  171. },
  172. status: 200,
  173. isImage: true,
  174. type: 'image',
  175. contentType: 'image/jpeg',
  176. contentLength: 999,
  177. weightCheck: {
  178. body: fileContent,
  179. totalWeight: fileSize + 200,
  180. headersSize: 200,
  181. bodySize: fileSize,
  182. isCompressed: false,
  183. uncompressedSize: fileSize
  184. }
  185. };
  186. imageOptimizer.optimizeImage(entry)
  187. .then(function(newEntry) {
  188. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  189. done();
  190. })
  191. .fail(function(err) {
  192. done(err);
  193. });
  194. });
  195. it('shouldn\'t fail optimizing a corrupted png', function(done) {
  196. // In this test, we try to optimize a JPEG but with a falsy "image/png" content type
  197. var fileContent = fs.readFileSync(path.resolve(__dirname, '../fixtures/jpeg-image.jpg'));
  198. var fileSize = fileContent.length;
  199. var entry = {
  200. method: 'GET',
  201. url: 'http://localhost:8388/an-image.jpg',
  202. requestHeaders: {
  203. 'User-Agent': 'something',
  204. Referer: 'http://www.google.fr/',
  205. Accept: '*/*',
  206. 'Accept-Encoding': 'gzip, deflate'
  207. },
  208. status: 200,
  209. isImage: true,
  210. type: 'image',
  211. contentType: 'image/png',
  212. contentLength: 999,
  213. weightCheck: {
  214. body: fileContent,
  215. totalWeight: fileSize + 200,
  216. headersSize: 200,
  217. bodySize: fileSize,
  218. isCompressed: false,
  219. uncompressedSize: fileSize
  220. }
  221. };
  222. imageOptimizer.optimizeImage(entry)
  223. .then(function(newEntry) {
  224. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  225. done();
  226. })
  227. .fail(function(err) {
  228. done(err);
  229. });
  230. });
  231. });