redownloadTest.js 9.6 KB

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