gzipCompressorTest.js 18 KB

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