redownloadTest.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. var should = require('chai').should();
  2. var redownload = require('../../lib/tools/redownload/redownload');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('redownload', function() {
  6. it('should download a list of files', function(done) {
  7. this.timeout(10000);
  8. var requestsList = [
  9. {
  10. method: 'GET',
  11. url: 'http://localhost:8388/simple-page.html',
  12. requestHeaders: {
  13. 'User-Agent': 'something',
  14. Referer: 'http://www.google.fr/',
  15. Accept: '*/*'
  16. },
  17. status: 200,
  18. isHTML: true,
  19. type: 'html'
  20. },
  21. {
  22. method: 'GET',
  23. url: 'http://localhost:8388/jquery1.8.3.js',
  24. requestHeaders: {
  25. 'User-Agent': 'something',
  26. Referer: 'http://www.google.fr/',
  27. Accept: '*/*'
  28. },
  29. status: 200,
  30. isJS: true,
  31. type: 'js'
  32. },
  33. {
  34. method: 'GET',
  35. url: 'http://localhost:8388/jpeg-image.jpg',
  36. requestHeaders: {
  37. 'User-Agent': 'something',
  38. Referer: 'http://www.google.fr/',
  39. Accept: '*/*'
  40. },
  41. status: 200,
  42. isImage: true,
  43. type: 'image',
  44. contentType: 'image/jpeg'
  45. },
  46. {
  47. method: 'GET',
  48. url: 'http://localhost:8388/svg-image.svg',
  49. requestHeaders: {
  50. 'User-Agent': 'something',
  51. Referer: 'http://www.google.fr/',
  52. Accept: '*/*'
  53. },
  54. status: 200,
  55. isImage: true,
  56. isSVG: true,
  57. type: 'image',
  58. contentType: 'image/svg+xml'
  59. },
  60. {
  61. method: 'GET',
  62. url: 'http://localhost:8388/unminified-script.js',
  63. requestHeaders: {
  64. 'User-Agent': 'something',
  65. Referer: 'http://www.google.fr/',
  66. Accept: '*/*'
  67. },
  68. status: 200,
  69. isJS: true,
  70. type: 'js'
  71. },
  72. {
  73. method: 'GET',
  74. url: 'http://localhost:8388/unminified-stylesheet.css',
  75. requestHeaders: {
  76. 'User-Agent': 'something',
  77. Referer: 'http://www.google.fr/',
  78. Accept: '*/*'
  79. },
  80. status: 200,
  81. isCSS: true,
  82. type: 'css'
  83. },
  84. {
  85. method: 'GET',
  86. url: 'http://localhost:8388/xml.xml',
  87. requestHeaders: {
  88. 'User-Agent': 'something',
  89. Referer: 'http://www.google.fr/',
  90. Accept: '*/*'
  91. },
  92. status: 200,
  93. isXML: true,
  94. type: 'xml'
  95. },
  96. {
  97. method: 'GET',
  98. url: 'about:blank',
  99. requestHeaders: {
  100. 'User-Agent': 'something',
  101. Referer: 'http://www.google.fr/',
  102. Accept: '*/*'
  103. },
  104. status: 200
  105. }
  106. ];
  107. var data = {
  108. params: {
  109. options: {
  110. device: 'phone'
  111. }
  112. },
  113. toolsResults: {
  114. phantomas: {
  115. metrics: {
  116. requestsList: true
  117. },
  118. offenders: {
  119. requestsList: JSON.stringify(requestsList)
  120. }
  121. }
  122. }
  123. };
  124. redownload.recheckAllFiles(data)
  125. .then(function(data) {
  126. data.toolsResults.should.have.a.property('redownload');
  127. data.toolsResults.redownload.should.have.a.property('metrics');
  128. data.toolsResults.redownload.should.have.a.property('offenders');
  129. data.toolsResults.redownload.offenders.should.have.a.property('totalWeight');
  130. data.toolsResults.redownload.offenders.totalWeight.totalWeight.should.be.above(0);
  131. data.toolsResults.redownload.offenders.totalWeight.byType.html.requests.length.should.equal(1);
  132. data.toolsResults.redownload.offenders.totalWeight.byType.js.requests.length.should.equal(2);
  133. data.toolsResults.redownload.offenders.totalWeight.byType.css.requests.length.should.equal(1);
  134. data.toolsResults.redownload.offenders.totalWeight.byType.image.requests.length.should.equal(2);
  135. data.toolsResults.redownload.offenders.totalWeight.byType.other.requests.length.should.equal(1);
  136. data.toolsResults.redownload.offenders.should.have.a.property('imageOptimization');
  137. data.toolsResults.redownload.offenders.imageOptimization.totalGain.should.be.above(0);
  138. data.toolsResults.redownload.offenders.imageOptimization.images.length.should.equal(2);
  139. data.toolsResults.redownload.offenders.should.have.a.property('imagesTooLarge');
  140. data.toolsResults.redownload.offenders.imagesTooLarge.length.should.equal(0);
  141. data.toolsResults.redownload.offenders.should.have.a.property('gzipCompression');
  142. data.toolsResults.redownload.offenders.gzipCompression.totalGain.should.be.above(0);
  143. data.toolsResults.redownload.offenders.gzipCompression.files.length.should.equal(5);
  144. data.toolsResults.redownload.offenders.should.have.a.property('fileMinification');
  145. data.toolsResults.redownload.offenders.fileMinification.totalGain.should.be.above(0);
  146. data.toolsResults.redownload.offenders.fileMinification.files.length.should.equal(2);
  147. data.toolsResults.redownload.metrics.should.have.a.property('totalRequests').that.equals(7);
  148. data.toolsResults.redownload.offenders.should.have.a.property('totalRequests');
  149. data.toolsResults.redownload.offenders.totalRequests.byType.html.length.should.equal(1);
  150. data.toolsResults.redownload.offenders.totalRequests.byType.js.length.should.equal(2);
  151. data.toolsResults.redownload.offenders.totalRequests.byType.css.length.should.equal(1);
  152. data.toolsResults.redownload.offenders.totalRequests.byType.image.length.should.equal(2);
  153. data.toolsResults.redownload.offenders.totalRequests.byType.json.length.should.equal(0);
  154. data.toolsResults.redownload.offenders.totalRequests.byType.webfont.length.should.equal(0);
  155. data.toolsResults.redownload.offenders.totalRequests.byType.video.length.should.equal(0);
  156. data.toolsResults.redownload.offenders.totalRequests.byType.other.length.should.equal(1);
  157. data.toolsResults.redownload.metrics.should.have.a.property('smallRequests').that.equals(0);
  158. data.toolsResults.redownload.offenders.should.have.a.property('smallRequests');
  159. data.toolsResults.redownload.offenders.smallRequests.byType.js.length.should.equal(0);
  160. data.toolsResults.redownload.offenders.smallRequests.byType.css.length.should.equal(0);
  161. data.toolsResults.redownload.offenders.smallRequests.byType.image.length.should.equal(0);
  162. done();
  163. })
  164. .fail(function(err) {
  165. done(err);
  166. });
  167. });
  168. it('should download a file and measure its size', function(done) {
  169. var entry = {
  170. method: 'GET',
  171. url: 'http://localhost:8388/jquery1.8.3.js',
  172. requestHeaders: {
  173. 'User-Agent': 'something',
  174. Referer: 'http://www.google.fr/',
  175. Accept: '*/*'
  176. },
  177. status: 200,
  178. isJS: true,
  179. type: 'js'
  180. };
  181. redownload.redownloadEntry(entry)
  182. .then(function(newEntry) {
  183. newEntry.weightCheck.bodySize.should.equal(93636);
  184. newEntry.weightCheck.uncompressedSize.should.equal(newEntry.weightCheck.bodySize);
  185. newEntry.weightCheck.isCompressed.should.equal(false);
  186. newEntry.weightCheck.headersSize.should.be.above(200).and.below(400);
  187. newEntry.weightCheck.bodyBuffer.toString().should.have.string('1.8.3');
  188. done();
  189. })
  190. .fail(function(err) {
  191. done(err);
  192. });
  193. });
  194. it('should download a PNG image and find the same body as fs.readFile', function(done) {
  195. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/logo-large.png'));
  196. var entry = {
  197. method: 'GET',
  198. url: 'http://localhost:8388/logo-large.png',
  199. requestHeaders: {
  200. 'User-Agent': 'something',
  201. Referer: 'http://www.google.fr/',
  202. Accept: '*/*'
  203. },
  204. status: 200,
  205. isImage: true,
  206. contentType: 'image/png'
  207. };
  208. redownload.redownloadEntry(entry)
  209. .then(function(newEntry) {
  210. newEntry.weightCheck.bodySize.should.equal(4193);
  211. newEntry.weightCheck.bodyBuffer.should.deep.equal(fileContent);
  212. // Opening the image in jimp to check if the format is good
  213. var Jimp = require('jimp');
  214. Jimp.read(newEntry.weightCheck.bodyBuffer, function(err, image) {
  215. image.bitmap.width.should.equal(620);
  216. image.bitmap.height.should.equal(104);
  217. done(err);
  218. });
  219. })
  220. .fail(function(err) {
  221. done(err);
  222. });
  223. });
  224. it('should fail downloading a file in error', function(done) {
  225. var entry = {
  226. method: 'GET',
  227. url: 'http://localhost:8388/no-file',
  228. requestHeaders: {
  229. 'User-Agent': 'something',
  230. Referer: 'http://www.google.fr/',
  231. Accept: '*/*'
  232. },
  233. status: 200,
  234. isHTML: true,
  235. contentLength: 999
  236. };
  237. redownload.redownloadEntry(entry)
  238. .then(function(newEntry) {
  239. newEntry.weightCheck.should.have.a.property('message').that.equals('error while downloading: 404');
  240. done();
  241. })
  242. .fail(function(err) {
  243. done(err);
  244. });
  245. });
  246. it('should not download a non 200 request', function(done) {
  247. var entry = {
  248. method: 'GET',
  249. url: 'http://localhost:8388/no-file',
  250. requestHeaders: {
  251. 'User-Agent': 'something',
  252. Referer: 'http://www.google.fr/',
  253. Accept: '*/*'
  254. },
  255. status: 302,
  256. isHTML: true,
  257. contentLength: 999
  258. };
  259. redownload.redownloadEntry(entry)
  260. .then(function(newEntry) {
  261. newEntry.should.not.have.a.property('weightCheck');
  262. done();
  263. })
  264. .fail(function(err) {
  265. done(err);
  266. });
  267. });
  268. it('should listRequestWeight', function() {
  269. var totalWeightObj = redownload.listRequestWeight([{
  270. method: 'GET',
  271. url: 'http://localhost:8388/jquery1.8.3.js',
  272. requestHeaders: {
  273. 'User-Agent': 'something',
  274. Referer: 'http://www.google.fr/',
  275. Accept: '*/*',
  276. 'Accept-Encoding': 'gzip, deflate'
  277. },
  278. status: 200,
  279. isHTML: true,
  280. type: 'html',
  281. contentLength: 999,
  282. weightCheck: {
  283. bodyBuffer: 'blabla',
  284. headersSize: 200,
  285. bodySize: 500,
  286. isCompressed: false,
  287. uncompressedSize: 500
  288. }
  289. }]);
  290. totalWeightObj.totalWeight.should.equal(700);
  291. totalWeightObj.byType.html.totalWeight.should.equal(700);
  292. totalWeightObj.byType.html.requests.should.have.length(1);
  293. totalWeightObj.byType.html.requests[0].should.have.a.property('url').that.equals('http://localhost:8388/jquery1.8.3.js');
  294. totalWeightObj.byType.html.requests[0].should.have.a.property('weight').that.equals(700);
  295. });
  296. it('should listRequestWeight even if download failed', function() {
  297. var totalWeightObj = redownload.listRequestWeight([{
  298. method: 'GET',
  299. url: 'http://localhost:8388/jquery1.8.3.js',
  300. requestHeaders: {
  301. 'User-Agent': 'something',
  302. Referer: 'http://www.google.fr/',
  303. Accept: '*/*',
  304. 'Accept-Encoding': 'gzip, deflate'
  305. },
  306. status: 200,
  307. isHTML: true,
  308. type: 'html',
  309. contentLength: 999,
  310. weightCheck: {
  311. message: 'error while downloading: 404'
  312. }
  313. }]);
  314. totalWeightObj.totalWeight.should.equal(999);
  315. });
  316. });