weightCheckerTest.js 11 KB

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