redownloadTest.js 10 KB

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