redownloadTest.js 11 KB

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