weightCheckerTest.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. var should = require('chai').should();
  2. var weightChecker = require('../../lib/tools/weightChecker/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)
  65. .then(function(newEntry) {
  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(200).and.below(400);
  70. newEntry.weightCheck.body.should.have.string('1.8.3');
  71. done();
  72. })
  73. .fail(function(err) {
  74. done(err);
  75. });
  76. });
  77. it('should fail downloading a file in error', function(done) {
  78. var entry = {
  79. method: 'GET',
  80. url: 'http://localhost:8388/no-file',
  81. requestHeaders: {
  82. 'User-Agent': 'something',
  83. Referer: 'http://www.google.fr/',
  84. Accept: '*/*'
  85. },
  86. status: 200,
  87. isHTML: true,
  88. contentLength: 999
  89. };
  90. weightChecker.redownloadEntry(entry)
  91. .then(function(errnewEntry) {
  92. newEntry.weightCheck.should.have.a.property('message').that.equals('error while downloading: 404');
  93. done();
  94. })
  95. .fail(function(err) {
  96. done(err);
  97. });
  98. });
  99. it('should not download a non 200 request', function(done) {
  100. var entry = {
  101. method: 'GET',
  102. url: 'http://localhost:8388/no-file',
  103. requestHeaders: {
  104. 'User-Agent': 'something',
  105. Referer: 'http://www.google.fr/',
  106. Accept: '*/*'
  107. },
  108. status: 302,
  109. isHTML: true,
  110. contentLength: 999
  111. };
  112. weightChecker.redownloadEntry(entry)
  113. .then(function(newEntry) {
  114. newEntry.weightCheck.should.have.a.property('message').that.equals('only downloading requests with status code 200');
  115. done();
  116. })
  117. .fail(function(err) {
  118. done(err);
  119. });
  120. });
  121. it('should listRequestWeight', function() {
  122. var totalWeightObj = weightChecker.listRequestWeight([{
  123. method: 'GET',
  124. url: 'http://localhost:8388/jquery1.8.3.js',
  125. requestHeaders: {
  126. 'User-Agent': 'something',
  127. Referer: 'http://www.google.fr/',
  128. Accept: '*/*',
  129. 'Accept-Encoding': 'gzip, deflate'
  130. },
  131. status: 200,
  132. isHTML: true,
  133. type: 'html',
  134. contentLength: 999,
  135. weightCheck: {
  136. body: 'blabla',
  137. headersSize: 200,
  138. bodySize: 500,
  139. isCompressed: false,
  140. uncompressedSize: 500
  141. }
  142. }]);
  143. totalWeightObj.totalWeight.should.equal(700);
  144. totalWeightObj.byType.html.totalWeight.should.equal(700);
  145. totalWeightObj.byType.html.requests.should.have.length(1);
  146. totalWeightObj.byType.html.requests[0].should.have.a.property('url').that.equals('http://localhost:8388/jquery1.8.3.js');
  147. totalWeightObj.byType.html.requests[0].should.have.a.property('weight').that.equals(700);
  148. });
  149. it('should listRequestWeight even if download failed', function() {
  150. var totalWeightObj = weightChecker.listRequestWeight([{
  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. 'Accept-Encoding': 'gzip, deflate'
  158. },
  159. status: 200,
  160. isHTML: true,
  161. type: 'html',
  162. contentLength: 999,
  163. weightCheck: {
  164. message: 'error while downloading: 404'
  165. }
  166. }]);
  167. totalWeightObj.totalWeight.should.equal(999);
  168. });
  169. });