weightCheckerTest.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. var should = require('chai').should();
  2. var weightChecker = require('../../lib/tools/weightChecker/weightChecker');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('weightChecker', 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: 'about:blank',
  87. requestHeaders: {
  88. 'User-Agent': 'something',
  89. Referer: 'http://www.google.fr/',
  90. Accept: '*/*'
  91. },
  92. status: 200
  93. }
  94. ];
  95. var data = {
  96. toolsResults: {
  97. phantomas: {
  98. metrics: {
  99. requestsList: true
  100. },
  101. offenders: {
  102. requestsList: JSON.stringify(requestsList)
  103. }
  104. }
  105. }
  106. };
  107. weightChecker.recheckAllFiles(data)
  108. .then(function(data) {
  109. data.toolsResults.should.have.a.property('weightChecker');
  110. data.toolsResults.weightChecker.should.have.a.property('metrics');
  111. data.toolsResults.weightChecker.should.have.a.property('offenders');
  112. data.toolsResults.weightChecker.offenders.should.have.a.property('totalWeight');
  113. data.toolsResults.weightChecker.offenders.totalWeight.totalWeight.should.be.above(0);
  114. data.toolsResults.weightChecker.offenders.totalWeight.byType.html.requests.length.should.equal(1);
  115. data.toolsResults.weightChecker.offenders.totalWeight.byType.js.requests.length.should.equal(2);
  116. data.toolsResults.weightChecker.offenders.totalWeight.byType.css.requests.length.should.equal(1);
  117. data.toolsResults.weightChecker.offenders.totalWeight.byType.image.requests.length.should.equal(2);
  118. data.toolsResults.weightChecker.offenders.totalWeight.byType.other.requests.length.should.equal(0);
  119. data.toolsResults.weightChecker.offenders.should.have.a.property('imageOptimization');
  120. data.toolsResults.weightChecker.offenders.imageOptimization.totalGain.should.be.above(0);
  121. data.toolsResults.weightChecker.offenders.imageOptimization.images.length.should.equal(2);
  122. data.toolsResults.weightChecker.offenders.should.have.a.property('gzipCompression');
  123. data.toolsResults.weightChecker.offenders.gzipCompression.totalGain.should.be.above(0);
  124. data.toolsResults.weightChecker.offenders.gzipCompression.files.length.should.equal(4);
  125. data.toolsResults.weightChecker.offenders.should.have.a.property('fileMinification');
  126. data.toolsResults.weightChecker.offenders.fileMinification.totalGain.should.be.above(0);
  127. data.toolsResults.weightChecker.offenders.fileMinification.files.length.should.equal(2);
  128. data.toolsResults.weightChecker.metrics.should.have.a.property('totalRequests').that.equals(6);
  129. data.toolsResults.weightChecker.offenders.should.have.a.property('totalRequests');
  130. data.toolsResults.weightChecker.offenders.totalRequests.byType.html.length.should.equal(1);
  131. data.toolsResults.weightChecker.offenders.totalRequests.byType.js.length.should.equal(2);
  132. data.toolsResults.weightChecker.offenders.totalRequests.byType.css.length.should.equal(1);
  133. data.toolsResults.weightChecker.offenders.totalRequests.byType.image.length.should.equal(2);
  134. data.toolsResults.weightChecker.offenders.totalRequests.byType.json.length.should.equal(0);
  135. data.toolsResults.weightChecker.offenders.totalRequests.byType.webfont.length.should.equal(0);
  136. data.toolsResults.weightChecker.offenders.totalRequests.byType.video.length.should.equal(0);
  137. data.toolsResults.weightChecker.offenders.totalRequests.byType.other.length.should.equal(0);
  138. data.toolsResults.weightChecker.metrics.should.have.a.property('smallRequests').that.equals(0);
  139. data.toolsResults.weightChecker.offenders.should.have.a.property('smallRequests');
  140. data.toolsResults.weightChecker.offenders.smallRequests.byType.js.length.should.equal(0);
  141. data.toolsResults.weightChecker.offenders.smallRequests.byType.css.length.should.equal(0);
  142. data.toolsResults.weightChecker.offenders.smallRequests.byType.image.length.should.equal(0);
  143. done();
  144. })
  145. .fail(function(err) {
  146. done(err);
  147. });
  148. });
  149. it('should download a file and measure its size', function(done) {
  150. var entry = {
  151. method: 'GET',
  152. url: 'http://localhost:8388/jquery1.8.3.js',
  153. requestHeaders: {
  154. 'User-Agent': 'something',
  155. Referer: 'http://www.google.fr/',
  156. Accept: '*/*'
  157. },
  158. status: 200,
  159. isJS: true,
  160. type: 'js'
  161. };
  162. weightChecker.redownloadEntry(entry)
  163. .then(function(newEntry) {
  164. newEntry.weightCheck.bodySize.should.equal(93636);
  165. newEntry.weightCheck.uncompressedSize.should.equal(newEntry.weightCheck.bodySize);
  166. newEntry.weightCheck.isCompressed.should.equal(false);
  167. newEntry.weightCheck.headersSize.should.be.above(200).and.below(400);
  168. newEntry.weightCheck.body.toString().should.have.string('1.8.3');
  169. done();
  170. })
  171. .fail(function(err) {
  172. done(err);
  173. });
  174. });
  175. it('should download a PNG image and find the same body as fs.readFile', function(done) {
  176. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/logo-large.png'));
  177. var entry = {
  178. method: 'GET',
  179. url: 'http://localhost:8388/logo-large.png',
  180. requestHeaders: {
  181. 'User-Agent': 'something',
  182. Referer: 'http://www.google.fr/',
  183. Accept: '*/*'
  184. },
  185. status: 200,
  186. isImage: true,
  187. contentType: 'image/png'
  188. };
  189. weightChecker.redownloadEntry(entry)
  190. .then(function(newEntry) {
  191. newEntry.weightCheck.bodySize.should.equal(4193);
  192. newEntry.weightCheck.body.should.equal(fileContent.toString('binary'));
  193. // Opening the image in lwip to check if the format is good
  194. var lwip = require('lwip');
  195. var buffer = new Buffer(newEntry.weightCheck.body, 'binary');
  196. lwip.open(buffer, 'png', function(err, image) {
  197. image.width().should.equal(620);
  198. image.height().should.equal(104);
  199. done(err);
  200. });
  201. })
  202. .fail(function(err) {
  203. done(err);
  204. });
  205. });
  206. it('should fail downloading a file in error', function(done) {
  207. var entry = {
  208. method: 'GET',
  209. url: 'http://localhost:8388/no-file',
  210. requestHeaders: {
  211. 'User-Agent': 'something',
  212. Referer: 'http://www.google.fr/',
  213. Accept: '*/*'
  214. },
  215. status: 200,
  216. isHTML: true,
  217. contentLength: 999
  218. };
  219. weightChecker.redownloadEntry(entry)
  220. .then(function(newEntry) {
  221. newEntry.weightCheck.should.have.a.property('message').that.equals('error while downloading: 404');
  222. done();
  223. })
  224. .fail(function(err) {
  225. done(err);
  226. });
  227. });
  228. it('should not download a non 200 request', function(done) {
  229. var entry = {
  230. method: 'GET',
  231. url: 'http://localhost:8388/no-file',
  232. requestHeaders: {
  233. 'User-Agent': 'something',
  234. Referer: 'http://www.google.fr/',
  235. Accept: '*/*'
  236. },
  237. status: 302,
  238. isHTML: true,
  239. contentLength: 999
  240. };
  241. weightChecker.redownloadEntry(entry)
  242. .then(function(newEntry) {
  243. newEntry.should.not.have.a.property('weightCheck');
  244. done();
  245. })
  246. .fail(function(err) {
  247. done(err);
  248. });
  249. });
  250. it('should listRequestWeight', function() {
  251. var totalWeightObj = weightChecker.listRequestWeight([{
  252. method: 'GET',
  253. url: 'http://localhost:8388/jquery1.8.3.js',
  254. requestHeaders: {
  255. 'User-Agent': 'something',
  256. Referer: 'http://www.google.fr/',
  257. Accept: '*/*',
  258. 'Accept-Encoding': 'gzip, deflate'
  259. },
  260. status: 200,
  261. isHTML: true,
  262. type: 'html',
  263. contentLength: 999,
  264. weightCheck: {
  265. body: 'blabla',
  266. headersSize: 200,
  267. bodySize: 500,
  268. isCompressed: false,
  269. uncompressedSize: 500
  270. }
  271. }]);
  272. totalWeightObj.totalWeight.should.equal(700);
  273. totalWeightObj.byType.html.totalWeight.should.equal(700);
  274. totalWeightObj.byType.html.requests.should.have.length(1);
  275. totalWeightObj.byType.html.requests[0].should.have.a.property('url').that.equals('http://localhost:8388/jquery1.8.3.js');
  276. totalWeightObj.byType.html.requests[0].should.have.a.property('weight').that.equals(700);
  277. });
  278. it('should listRequestWeight even if download failed', function() {
  279. var totalWeightObj = weightChecker.listRequestWeight([{
  280. method: 'GET',
  281. url: 'http://localhost:8388/jquery1.8.3.js',
  282. requestHeaders: {
  283. 'User-Agent': 'something',
  284. Referer: 'http://www.google.fr/',
  285. Accept: '*/*',
  286. 'Accept-Encoding': 'gzip, deflate'
  287. },
  288. status: 200,
  289. isHTML: true,
  290. type: 'html',
  291. contentLength: 999,
  292. weightCheck: {
  293. message: 'error while downloading: 404'
  294. }
  295. }]);
  296. totalWeightObj.totalWeight.should.equal(999);
  297. });
  298. });