weightCheckerTest.js 11 KB

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