weightCheckerTest.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. var should = require('chai').should();
  2. var weightChecker = require('../../lib/tools/weightChecker');
  3. describe('weightChecker', function() {
  4. it('should download a list of files', function(done) {
  5. var requestsList = [
  6. {
  7. method: 'GET',
  8. url: 'http://localhost:8388/simple-page.html',
  9. requestHeaders: {
  10. 'User-Agent': 'something',
  11. Referer: 'http://www.google.fr/',
  12. Accept: '*/*'
  13. },
  14. status: 200,
  15. isHTML: true
  16. },
  17. {
  18. method: 'GET',
  19. url: 'http://localhost:8388/jquery1.8.3.js',
  20. requestHeaders: {
  21. 'User-Agent': 'something',
  22. Referer: 'http://www.google.fr/',
  23. Accept: '*/*'
  24. },
  25. status: 200,
  26. isJS: true
  27. }
  28. ];
  29. var data = {
  30. toolsResults: {
  31. phantomas: {
  32. metrics: {
  33. requestsList: true
  34. },
  35. offenders: {
  36. requestsList: JSON.stringify(requestsList)
  37. }
  38. }
  39. }
  40. };
  41. weightChecker.recheckAllFiles(data)
  42. .then(function(data) {
  43. data.toolsResults.should.have.a.property('weightChecker');
  44. data.toolsResults.weightChecker.should.have.a.property('metrics');
  45. data.toolsResults.weightChecker.should.have.a.property('offenders');
  46. done();
  47. })
  48. .fail(function(err) {
  49. done(err);
  50. });
  51. });
  52. it('should download a file and measure its size', function(done) {
  53. var entry = {
  54. method: 'GET',
  55. url: 'http://localhost:8388/jquery1.8.3.js',
  56. requestHeaders: {
  57. 'User-Agent': 'something',
  58. Referer: 'http://www.google.fr/',
  59. Accept: '*/*'
  60. },
  61. status: 200,
  62. isJS: true
  63. };
  64. weightChecker.redownloadEntry(entry, function(err, newEntry) {
  65. should.not.exist(err);
  66. newEntry.weightCheck.bodySize.should.equal(93636);
  67. newEntry.weightCheck.uncompressedSize.should.equal(newEntry.weightCheck.bodySize);
  68. newEntry.weightCheck.isCompressed.should.equal(false);
  69. newEntry.weightCheck.headersSize.should.be.above(300).and.below(350);
  70. newEntry.weightCheck.body.should.have.string('1.8.3');
  71. done();
  72. });
  73. });
  74. it('should fail downloading a file in error', function(done) {
  75. var entry = {
  76. method: 'GET',
  77. url: 'http://localhost:8388/no-file',
  78. requestHeaders: {
  79. 'User-Agent': 'something',
  80. Referer: 'http://www.google.fr/',
  81. Accept: '*/*'
  82. },
  83. status: 200,
  84. isHTML: true,
  85. contentLength: 999
  86. };
  87. weightChecker.redownloadEntry(entry, function(err, newEntry) {
  88. should.not.exist(err);
  89. newEntry.weightCheck.should.have.a.property('message').that.equals('error while downloading: 404');
  90. done();
  91. });
  92. });
  93. it('should not download a non 200 request', function(done) {
  94. var entry = {
  95. method: 'GET',
  96. url: 'http://localhost:8388/no-file',
  97. requestHeaders: {
  98. 'User-Agent': 'something',
  99. Referer: 'http://www.google.fr/',
  100. Accept: '*/*'
  101. },
  102. status: 302,
  103. isHTML: true,
  104. contentLength: 999
  105. };
  106. weightChecker.redownloadEntry(entry, function(err, newEntry) {
  107. should.not.exist(err);
  108. newEntry.weightCheck.should.have.a.property('message').that.equals('only downloading requests with status code 200');
  109. done();
  110. });
  111. });
  112. it('should listRequestWeight', function() {
  113. var totalWeightObj = weightChecker.listRequestWeight([{
  114. method: 'GET',
  115. url: 'http://localhost:8388/jquery1.8.3.js',
  116. requestHeaders: {
  117. 'User-Agent': 'something',
  118. Referer: 'http://www.google.fr/',
  119. Accept: '*/*',
  120. 'Accept-Encoding': 'gzip, deflate'
  121. },
  122. status: 200,
  123. isHTML: true,
  124. type: 'html',
  125. contentLength: 999,
  126. weightCheck: {
  127. body: 'blabla',
  128. headersSize: 200,
  129. bodySize: 500,
  130. isCompressed: false,
  131. uncompressedSize: 500
  132. }
  133. }]);
  134. totalWeightObj.totalWeight.should.equal(700);
  135. totalWeightObj.byType.html.totalWeight.should.equal(700);
  136. totalWeightObj.byType.html.requests.should.have.length(1);
  137. totalWeightObj.byType.html.requests[0].should.have.a.property('url').that.equals('http://localhost:8388/jquery1.8.3.js');
  138. totalWeightObj.byType.html.requests[0].should.have.a.property('weight').that.equals(700);
  139. });
  140. it('should listRequestWeight even if download failed', function() {
  141. var totalWeightObj = weightChecker.listRequestWeight([{
  142. method: 'GET',
  143. url: 'http://localhost:8388/jquery1.8.3.js',
  144. requestHeaders: {
  145. 'User-Agent': 'something',
  146. Referer: 'http://www.google.fr/',
  147. Accept: '*/*',
  148. 'Accept-Encoding': 'gzip, deflate'
  149. },
  150. status: 200,
  151. isHTML: true,
  152. type: 'html',
  153. contentLength: 999,
  154. weightCheck: {
  155. message: 'error while downloading: 404'
  156. }
  157. }]);
  158. totalWeightObj.totalWeight.should.equal(999);
  159. });
  160. });