weightCheckerTest.js 9.0 KB

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