fileMinifierTest.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. var should = require('chai').should();
  2. var fileMinifier = require('../../lib/tools/redownload/fileMinifier');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('fileMinifier', function() {
  6. it('should minify a JS file with minifyJs', function(done) {
  7. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-script.js'));
  8. var fileSize = fileContent.length;
  9. fileMinifier.minifyJs(fileContent.toString()).then(function(newFile) {
  10. var newFileSize = newFile.length;
  11. newFileSize.should.be.below(fileSize);
  12. done();
  13. }).fail(function(err) {
  14. done(err);
  15. });
  16. });
  17. it('should minify a JS file with minifyFile', function(done) {
  18. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-script.js'));
  19. var fileSize = fileContent.length;
  20. var entry = {
  21. method: 'GET',
  22. url: 'http://localhost:8388/unminified-script.js',
  23. requestHeaders: {
  24. 'User-Agent': 'something',
  25. Referer: 'http://www.google.fr/',
  26. Accept: '*/*',
  27. 'Accept-Encoding': 'gzip, deflate'
  28. },
  29. status: 200,
  30. isJS: true,
  31. type: 'js',
  32. contentLength: 999,
  33. weightCheck: {
  34. bodyBuffer: fileContent,
  35. totalWeight: fileSize + 200,
  36. headersSize: 200,
  37. bodySize: fileSize,
  38. isCompressed: false,
  39. uncompressedSize: fileSize
  40. }
  41. };
  42. fileMinifier.minifyFile(entry)
  43. .then(function(newEntry) {
  44. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  45. newEntry.weightCheck.should.have.a.property('optimized').that.is.below(fileSize);
  46. newEntry.weightCheck.should.have.a.property('bodyAfterOptimization').that.is.a('string');
  47. done();
  48. })
  49. .fail(function(err) {
  50. done(err);
  51. });
  52. });
  53. it('should fail minifying an already minified JS', function(done) {
  54. this.timeout(5000);
  55. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jquery1.8.3.js'));
  56. var fileSize = fileContent.length;
  57. var entry = {
  58. method: 'GET',
  59. url: 'http://localhost:8388/jquery1.8.3.js',
  60. requestHeaders: {
  61. 'User-Agent': 'something',
  62. Referer: 'http://www.google.fr/',
  63. Accept: '*/*',
  64. 'Accept-Encoding': 'gzip, deflate'
  65. },
  66. status: 200,
  67. isJS: true,
  68. type: 'js',
  69. contentLength: 999,
  70. weightCheck: {
  71. body: fileContent.toString('utf8'),
  72. totalWeight: fileSize + 200,
  73. headersSize: 200,
  74. bodySize: fileSize,
  75. isCompressed: false,
  76. uncompressedSize: fileSize
  77. }
  78. };
  79. fileMinifier.minifyFile(entry)
  80. .then(function(newEntry) {
  81. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  82. newEntry.weightCheck.should.not.have.a.property('optimized');
  83. newEntry.weightCheck.should.not.have.a.property('bodyAfterOptimization');
  84. done();
  85. })
  86. .fail(function(err) {
  87. done(err);
  88. });
  89. });
  90. it('should fail minifying a JS with syntax errors', function(done) {
  91. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  92. var fileSize = fileContent.length;
  93. var entry = {
  94. method: 'GET',
  95. url: 'http://localhost:8388/svg-image.svg',
  96. requestHeaders: {
  97. 'User-Agent': 'something',
  98. Referer: 'http://www.google.fr/',
  99. Accept: '*/*',
  100. 'Accept-Encoding': 'gzip, deflate'
  101. },
  102. status: 200,
  103. isJS: true,
  104. type: 'js',
  105. contentLength: 999,
  106. weightCheck: {
  107. body: fileContent.toString('utf8'),
  108. totalWeight: fileSize + 200,
  109. headersSize: 200,
  110. bodySize: fileSize,
  111. isCompressed: false,
  112. uncompressedSize: fileSize
  113. }
  114. };
  115. fileMinifier.minifyFile(entry)
  116. .then(function(newEntry) {
  117. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  118. newEntry.weightCheck.should.not.have.a.property('optimized');
  119. newEntry.weightCheck.should.not.have.a.property('bodyAfterOptimization');
  120. done();
  121. })
  122. .fail(function(err) {
  123. done(err);
  124. });
  125. });
  126. it('should minify a CSS file with clean-css', function(done) {
  127. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-stylesheet.css'));
  128. var fileSize = fileContent.length;
  129. fileMinifier.minifyCss(fileContent.toString()).then(function(newFile) {
  130. var newFileSize = newFile.length;
  131. newFileSize.should.be.below(fileSize);
  132. done();
  133. }).fail(function(err) {
  134. done(err);
  135. });
  136. });
  137. it('should minify a CSS file with minifyFile', function(done) {
  138. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-stylesheet.css'));
  139. var fileSize = fileContent.length;
  140. var entry = {
  141. method: 'GET',
  142. url: 'http://localhost:8388/unminified-stylesheet.css',
  143. requestHeaders: {
  144. 'User-Agent': 'something',
  145. Referer: 'http://www.google.fr/',
  146. Accept: '*/*',
  147. 'Accept-Encoding': 'gzip, deflate'
  148. },
  149. status: 200,
  150. isCSS: true,
  151. type: 'css',
  152. contentLength: 999,
  153. weightCheck: {
  154. bodyBuffer: fileContent,
  155. totalWeight: fileSize + 200,
  156. headersSize: 200,
  157. bodySize: fileSize,
  158. isCompressed: false,
  159. uncompressedSize: fileSize
  160. }
  161. };
  162. fileMinifier.minifyFile(entry)
  163. .then(function(newEntry) {
  164. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  165. newEntry.weightCheck.should.have.a.property('optimized').that.is.below(fileSize);
  166. newEntry.weightCheck.should.have.a.property('bodyAfterOptimization').that.is.a('string');
  167. done();
  168. })
  169. .fail(function(err) {
  170. done(err);
  171. });
  172. });
  173. it('should minify an HTML file with ', function(done) {
  174. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jquery-page.html'));
  175. var fileSize = fileContent.length;
  176. fileMinifier.minifyHtml(fileContent.toString()).then(function(newFile) {
  177. var newFileSize = newFile.length;
  178. newFileSize.should.be.below(fileSize);
  179. done();
  180. }).fail(function(err) {
  181. done(err);
  182. });
  183. });
  184. it('should avoid minifying some JS files known as minified', function() {
  185. fileMinifier.isKnownAsMinified('https://platform.twitter.com/widgets.js').should.equal(true);
  186. fileMinifier.isKnownAsMinified('http://platform.twitter.com/widgets.js').should.equal(true);
  187. fileMinifier.isKnownAsMinified('https://connect.facebook.net/fr_FR/sdk.js').should.equal(true);
  188. fileMinifier.isKnownAsMinified('https://connect.facebook.net/en_EN/sdk.js').should.equal(true);
  189. fileMinifier.isKnownAsMinified('https://connect.facebook.net/fr_FR/all.js').should.equal(true);
  190. fileMinifier.isKnownAsMinified('https://connect.facebook.net/en_EN/all.js').should.equal(true);
  191. fileMinifier.isKnownAsMinified('https://apis.google.com/js/plusone.js').should.equal(true);
  192. fileMinifier.isKnownAsMinified('https://code.jquery.com/jquery-2.1.4.min.js').should.equal(true);
  193. fileMinifier.isKnownAsMinified('http://code.jquery.com/jquery-2.1.4.min.js').should.equal(true);
  194. fileMinifier.isKnownAsMinified('https://code.jquery.com/jquery-2.1.4.js').should.equal(false);
  195. fileMinifier.isKnownAsMinified('http://code.jquery.com/jquery-2.1.4.js').should.equal(false);
  196. fileMinifier.isKnownAsMinified('https://ssl.google-analytics.com/ga.js').should.equal(true);
  197. fileMinifier.isKnownAsMinified('http://www.google-analytics.com/ga.js').should.equal(true);
  198. fileMinifier.isKnownAsMinified('https://www.google-analytics.com/analytics.js').should.equal(true);
  199. fileMinifier.isKnownAsMinified('http://www.google-analytics.com/analytics.js').should.equal(true);
  200. fileMinifier.isKnownAsMinified('http://anydomain.com/anyurl').should.equal(false);
  201. });
  202. });