gzipCompressorTest.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. var should = require('chai').should();
  2. var gzipCompressor = require('../../lib/tools/weightChecker/gzipCompressor');
  3. var fileMinifier = require('../../lib/tools/weightChecker/fileMinifier');
  4. var fs = require('fs');
  5. var path = require('path');
  6. describe('gzipCompressor', function() {
  7. var minifiedJSContent = fs.readFileSync(path.resolve(__dirname, '../www/minified-script.js'));
  8. var notMinifiedJSContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-script.js'));
  9. var someTextFileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  10. it('should gzip a JS file that was not gziped but was minified', function(done) {
  11. var fileContent = minifiedJSContent;
  12. var fileSize = fileContent.length;
  13. var entry = {
  14. method: 'GET',
  15. url: 'http://localhost:8388/minified-script.js',
  16. status: 200,
  17. isJS: true,
  18. type: 'js',
  19. contentLength: 999,
  20. weightCheck: {
  21. body: fileContent.toString('utf8'),
  22. totalWeight: fileSize + 200,
  23. headersSize: 200,
  24. bodySize: fileSize,
  25. isCompressed: false,
  26. uncompressedSize: fileSize,
  27. isMinified: true
  28. }
  29. };
  30. gzipCompressor.compressFile(entry)
  31. .then(function(newEntry) {
  32. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  33. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  34. done();
  35. })
  36. .fail(function(err) {
  37. done(err);
  38. });
  39. });
  40. it('should gzip a JS file that was not gziped and not minified', function(done) {
  41. /*jshint expr: true*/
  42. var fileContent = notMinifiedJSContent;
  43. var minifiedContent = minifiedJSContent;
  44. var fileSize = fileContent.length;
  45. var minifiedSize = minifiedContent.length;
  46. var entry = {
  47. method: 'GET',
  48. url: 'http://localhost:8388/unminified-script.js',
  49. status: 200,
  50. isJS: true,
  51. type: 'js',
  52. contentLength: 999,
  53. weightCheck: {
  54. body: fileContent.toString('utf8'),
  55. bodyAfterMinification: minifiedContent.toString('utf8'),
  56. totalWeight: fileSize + 200,
  57. headersSize: 200,
  58. bodySize: fileSize,
  59. isCompressed: false,
  60. uncompressedSize: fileSize,
  61. isMinified: false,
  62. minified: minifiedSize
  63. }
  64. };
  65. gzipCompressor.compressFile(entry)
  66. .then(function(newEntry) {
  67. delete newEntry.weightCheck.body;
  68. delete newEntry.weightCheck.bodyAfterMinification;
  69. console.log(newEntry.weightCheck);
  70. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  71. newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.not.undefined;
  72. newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.below(newEntry.weightCheck.afterCompression);
  73. done();
  74. })
  75. .fail(function(err) {
  76. done(err);
  77. });
  78. });
  79. it('should gzip a JS file that is gziped but not minified', function(done) {
  80. /*jshint expr: true*/
  81. var fileContent = notMinifiedJSContent;
  82. var minifiedContent = minifiedJSContent;
  83. var fileSize = 6436;
  84. var gzipedSize = 2646;
  85. var minifiedSize = 1954;
  86. var entry = {
  87. method: 'GET',
  88. url: 'http://localhost:8388/unminified-script.js',
  89. status: 200,
  90. isJS: true,
  91. type: 'js',
  92. contentLength: 999,
  93. weightCheck: {
  94. body: fileContent.toString('utf8'),
  95. bodyAfterMinification: minifiedContent.toString('utf8'),
  96. totalWeight: gzipedSize + 200,
  97. headersSize: 200,
  98. bodySize: gzipedSize,
  99. isCompressed: true,
  100. uncompressedSize: fileSize,
  101. isMinified: false,
  102. minified: minifiedSize
  103. }
  104. };
  105. gzipCompressor.compressFile(entry)
  106. .then(function(newEntry) {
  107. newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.not.undefined;
  108. newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.below(gzipedSize);
  109. newEntry.weightCheck.should.have.a.property('afterOptimizationAndCompression').that.is.below(minifiedSize);
  110. done();
  111. })
  112. .fail(function(err) {
  113. done(err);
  114. });
  115. });
  116. it('should not gzip a JS file that was gziped and minified', function(done) {
  117. /*jshint expr: true*/
  118. var fileContent = notMinifiedJSContent;
  119. var fileSize = 6436;
  120. var gzipedSize = 2646;
  121. var entry = {
  122. method: 'GET',
  123. url: 'http://localhost:8388/unminified-script.js',
  124. status: 200,
  125. isJS: true,
  126. type: 'js',
  127. contentLength: 999,
  128. weightCheck: {
  129. body: fileContent.toString('utf8'),
  130. totalWeight: gzipedSize + 200,
  131. headersSize: 200,
  132. bodySize: gzipedSize,
  133. isCompressed: true,
  134. uncompressedSize: fileSize,
  135. isMinified: true
  136. }
  137. };
  138. gzipCompressor.compressFile(entry)
  139. .then(function(newEntry) {
  140. newEntry.weightCheck.should.not.have.a.property('minified');
  141. newEntry.weightCheck.should.not.have.a.property('bodyAfterMinification');
  142. newEntry.weightCheck.should.not.have.a.property('afterCompression');
  143. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  144. done();
  145. })
  146. .fail(function(err) {
  147. done(err);
  148. });
  149. });
  150. it('should gzip a CSS file', function(done) {
  151. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/unminified-stylesheet.css'));
  152. var fileSize = fileContent.length;
  153. var entry = {
  154. method: 'GET',
  155. url: 'http://localhost:8388/unminified-stylesheet.css',
  156. status: 200,
  157. isCSS: true,
  158. type: 'css',
  159. contentLength: 999,
  160. weightCheck: {
  161. body: fileContent.toString('utf8'),
  162. totalWeight: fileSize + 200,
  163. headersSize: 200,
  164. bodySize: fileSize,
  165. isCompressed: false,
  166. uncompressedSize: fileSize,
  167. isMinified: true
  168. }
  169. };
  170. gzipCompressor.compressFile(entry)
  171. .then(function(newEntry) {
  172. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  173. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  174. done();
  175. })
  176. .fail(function(err) {
  177. done(err);
  178. });
  179. });
  180. it('should gzip an HTML file', function(done) {
  181. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jquery-page.html'));
  182. var fileSize = fileContent.length;
  183. var entry = {
  184. method: 'GET',
  185. url: 'http://localhost:8388/jquery-page.html',
  186. status: 200,
  187. isHTML: true,
  188. type: 'html',
  189. contentLength: 999,
  190. weightCheck: {
  191. body: fileContent.toString('utf8'),
  192. totalWeight: fileSize + 200,
  193. headersSize: 200,
  194. bodySize: fileSize,
  195. isCompressed: false,
  196. uncompressedSize: fileSize,
  197. isMinified: true
  198. }
  199. };
  200. gzipCompressor.compressFile(entry)
  201. .then(function(newEntry) {
  202. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  203. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  204. done();
  205. })
  206. .fail(function(err) {
  207. done(err);
  208. });
  209. });
  210. it('should gzip an SVG file', function(done) {
  211. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  212. var fileSize = fileContent.length;
  213. var entry = {
  214. method: 'GET',
  215. url: 'http://localhost:8388/svg-image.svg',
  216. status: 200,
  217. isImage: true,
  218. isSVG: true,
  219. type: 'image',
  220. contentLength: 999,
  221. weightCheck: {
  222. body: fileContent.toString('utf8'),
  223. totalWeight: fileSize + 200,
  224. headersSize: 200,
  225. bodySize: fileSize,
  226. isCompressed: false,
  227. uncompressedSize: fileSize,
  228. isMinified: true
  229. }
  230. };
  231. gzipCompressor.compressFile(entry)
  232. .then(function(newEntry) {
  233. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  234. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  235. done();
  236. })
  237. .fail(function(err) {
  238. done(err);
  239. });
  240. });
  241. it('should gzip an XML file', function(done) {
  242. var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
  243. var fileSize = fileContent.length;
  244. var entry = {
  245. method: 'GET',
  246. url: 'http://localhost:8388/someTextFile.xml',
  247. status: 200,
  248. isXML: true,
  249. type: 'xml',
  250. contentLength: 999,
  251. weightCheck: {
  252. body: fileContent.toString('utf8'),
  253. totalWeight: fileSize + 200,
  254. headersSize: 200,
  255. bodySize: fileSize,
  256. isCompressed: false,
  257. uncompressedSize: fileSize,
  258. isMinified: true
  259. }
  260. };
  261. gzipCompressor.compressFile(entry)
  262. .then(function(newEntry) {
  263. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  264. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  265. done();
  266. })
  267. .fail(function(err) {
  268. done(err);
  269. });
  270. });
  271. it('should gzip a JSON file', function(done) {
  272. var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
  273. var fileSize = fileContent.length;
  274. var entry = {
  275. method: 'GET',
  276. url: 'http://localhost:8388/someTextFile.json',
  277. status: 200,
  278. isJSON: true,
  279. type: 'json',
  280. contentLength: 999,
  281. weightCheck: {
  282. body: fileContent.toString('utf8'),
  283. totalWeight: fileSize + 200,
  284. headersSize: 200,
  285. bodySize: fileSize,
  286. isCompressed: false,
  287. uncompressedSize: fileSize,
  288. isMinified: true
  289. }
  290. };
  291. gzipCompressor.compressFile(entry)
  292. .then(function(newEntry) {
  293. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  294. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  295. done();
  296. })
  297. .fail(function(err) {
  298. done(err);
  299. });
  300. });
  301. it('should gzip a TTF file', function(done) {
  302. var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
  303. var fileSize = fileContent.length;
  304. var entry = {
  305. method: 'GET',
  306. url: 'http://localhost:8388/someTextFile.ttf',
  307. status: 200,
  308. isWebFont: true,
  309. isTTF: true,
  310. type: 'webfont',
  311. contentLength: 999,
  312. weightCheck: {
  313. body: fileContent.toString('utf8'),
  314. totalWeight: fileSize + 200,
  315. headersSize: 200,
  316. bodySize: fileSize,
  317. isCompressed: false,
  318. uncompressedSize: fileSize,
  319. isMinified: true
  320. }
  321. };
  322. gzipCompressor.compressFile(entry)
  323. .then(function(newEntry) {
  324. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  325. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  326. done();
  327. })
  328. .fail(function(err) {
  329. done(err);
  330. });
  331. });
  332. it('should gzip a favicon file', function(done) {
  333. var fileContent = someTextFileContent; // it dosn't matter if it's not the correct file type
  334. var fileSize = fileContent.length;
  335. var entry = {
  336. method: 'GET',
  337. url: 'http://localhost:8388/someTextFile.ico',
  338. status: 200,
  339. isFavicon: true,
  340. type: 'favicon',
  341. contentLength: 999,
  342. weightCheck: {
  343. body: fileContent.toString('utf8'),
  344. totalWeight: fileSize + 200,
  345. headersSize: 200,
  346. bodySize: fileSize,
  347. isCompressed: false,
  348. uncompressedSize: fileSize,
  349. isMinified: true
  350. }
  351. };
  352. gzipCompressor.compressFile(entry)
  353. .then(function(newEntry) {
  354. newEntry.weightCheck.should.have.a.property('afterCompression').that.is.below(fileSize);
  355. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  356. done();
  357. })
  358. .fail(function(err) {
  359. done(err);
  360. });
  361. });
  362. it('should not gzip a JPEG file', function(done) {
  363. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  364. var fileSize = fileContent.length;
  365. var entry = {
  366. method: 'GET',
  367. url: 'http://localhost:8388/jpeg-image.jpg',
  368. status: 200,
  369. isImage: true,
  370. type: 'image',
  371. contentType: 'image/jpeg',
  372. contentLength: 999,
  373. weightCheck: {
  374. body: fileContent.toString('utf8'),
  375. totalWeight: fileSize + 200,
  376. headersSize: 200,
  377. bodySize: fileSize,
  378. isCompressed: false,
  379. uncompressedSize: fileSize,
  380. isOptimized: true
  381. }
  382. };
  383. gzipCompressor.compressFile(entry)
  384. .then(function(newEntry) {
  385. newEntry.weightCheck.should.not.have.a.property('afterCompression');
  386. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  387. done();
  388. })
  389. .fail(function(err) {
  390. done(err);
  391. });
  392. });
  393. it('should not gzip a PNG file', function(done) {
  394. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
  395. var fileSize = fileContent.length;
  396. var entry = {
  397. method: 'GET',
  398. url: 'http://localhost:8388/png-image.png',
  399. status: 200,
  400. isImage: true,
  401. type: 'image',
  402. contentType: 'image/png',
  403. contentLength: 999,
  404. weightCheck: {
  405. body: fileContent.toString('utf8'),
  406. totalWeight: fileSize + 200,
  407. headersSize: 200,
  408. bodySize: fileSize,
  409. isCompressed: false,
  410. uncompressedSize: fileSize,
  411. isOptimized: true
  412. }
  413. };
  414. gzipCompressor.compressFile(entry)
  415. .then(function(newEntry) {
  416. newEntry.weightCheck.should.not.have.a.property('afterCompression');
  417. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  418. done();
  419. })
  420. .fail(function(err) {
  421. done(err);
  422. });
  423. });
  424. it('should not gzip a GIF file', function(done) {
  425. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png')); // Fake gif, don't tell anyone...
  426. var fileSize = fileContent.length;
  427. var entry = {
  428. method: 'GET',
  429. url: 'http://localhost:8388/gif-image.gif',
  430. status: 200,
  431. isImage: true,
  432. type: 'image',
  433. contentType: 'image/gif',
  434. contentLength: 999,
  435. weightCheck: {
  436. body: fileContent.toString('utf8'),
  437. totalWeight: fileSize + 200,
  438. headersSize: 200,
  439. bodySize: fileSize,
  440. isCompressed: false,
  441. uncompressedSize: fileSize,
  442. isOptimized: true
  443. }
  444. };
  445. gzipCompressor.compressFile(entry)
  446. .then(function(newEntry) {
  447. newEntry.weightCheck.should.not.have.a.property('afterCompression');
  448. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  449. done();
  450. })
  451. .fail(function(err) {
  452. done(err);
  453. });
  454. });
  455. it('should not gzip a WEBP file', function(done) {
  456. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png')); // Fake webp, don't tell anyone...
  457. var fileSize = fileContent.length;
  458. var entry = {
  459. method: 'GET',
  460. url: 'http://localhost:8388/webp-image.webp',
  461. status: 200,
  462. isImage: true,
  463. type: 'image',
  464. contentType: 'image/webp',
  465. contentLength: 999,
  466. weightCheck: {
  467. body: fileContent.toString('utf8'),
  468. totalWeight: fileSize + 200,
  469. headersSize: 200,
  470. bodySize: fileSize,
  471. isCompressed: false,
  472. uncompressedSize: fileSize,
  473. isOptimized: true
  474. }
  475. };
  476. gzipCompressor.compressFile(entry)
  477. .then(function(newEntry) {
  478. newEntry.weightCheck.should.not.have.a.property('afterCompression');
  479. newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
  480. done();
  481. })
  482. .fail(function(err) {
  483. done(err);
  484. });
  485. });
  486. });