redownloadTest.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // Opening the image in jimp to check if the format is good
  144. var Jimp = require('jimp');
  145. Jimp.read(newEntry.weightCheck.bodyBuffer, function(err, image) {
  146. image.bitmap.width.should.equal(620);
  147. image.bitmap.height.should.equal(104);
  148. done(err);
  149. });
  150. })
  151. .fail(function(err) {
  152. done(err);
  153. });
  154. });
  155. it('should fail downloading a file in error', function(done) {
  156. var entry = {
  157. method: 'GET',
  158. url: 'http://localhost:8388/no-file',
  159. requestHeaders: {
  160. 'User-Agent': 'something',
  161. Referer: 'http://www.google.fr/',
  162. Accept: '*/*'
  163. },
  164. status: 200,
  165. isHTML: true,
  166. contentLength: 999
  167. };
  168. redownload.redownloadEntry(entry)
  169. .then(function(newEntry) {
  170. newEntry.weightCheck.should.have.a.property('message').that.equals('error while downloading: 404');
  171. done();
  172. })
  173. .fail(function(err) {
  174. done(err);
  175. });
  176. });
  177. it('should not download a non 200 request', function(done) {
  178. var entry = {
  179. method: 'GET',
  180. url: 'http://localhost:8388/no-file',
  181. requestHeaders: {
  182. 'User-Agent': 'something',
  183. Referer: 'http://www.google.fr/',
  184. Accept: '*/*'
  185. },
  186. status: 302,
  187. isHTML: true,
  188. contentLength: 999,
  189. notFound: true
  190. };
  191. redownload.redownloadEntry(entry)
  192. .then(function(newEntry) {
  193. newEntry.should.not.have.a.property('weightCheck');
  194. done();
  195. })
  196. .fail(function(err) {
  197. done(err);
  198. });
  199. });
  200. it('should listRequestWeight', function() {
  201. var totalWeightObj = redownload.listRequestWeight([{
  202. method: 'GET',
  203. url: 'http://localhost:8388/jquery1.8.3.js',
  204. requestHeaders: {
  205. 'User-Agent': 'something',
  206. Referer: 'http://www.google.fr/',
  207. Accept: '*/*',
  208. 'Accept-Encoding': 'gzip, deflate'
  209. },
  210. status: 200,
  211. isHTML: true,
  212. type: 'html',
  213. contentLength: 999,
  214. weightCheck: {
  215. bodyBuffer: 'blabla',
  216. headersSize: 200,
  217. bodySize: 500,
  218. isCompressed: false,
  219. uncompressedSize: 500
  220. }
  221. }]);
  222. totalWeightObj.totalWeight.should.equal(700);
  223. totalWeightObj.byType.html.totalWeight.should.equal(700);
  224. totalWeightObj.byType.html.requests.should.have.length(1);
  225. totalWeightObj.byType.html.requests[0].should.have.a.property('url').that.equals('http://localhost:8388/jquery1.8.3.js');
  226. totalWeightObj.byType.html.requests[0].should.have.a.property('weight').that.equals(700);
  227. });
  228. it('should listRequestWeight even if download failed', function() {
  229. var totalWeightObj = redownload.listRequestWeight([{
  230. method: 'GET',
  231. url: 'http://localhost:8388/jquery1.8.3.js',
  232. requestHeaders: {
  233. 'User-Agent': 'something',
  234. Referer: 'http://www.google.fr/',
  235. Accept: '*/*',
  236. 'Accept-Encoding': 'gzip, deflate'
  237. },
  238. status: 200,
  239. isHTML: true,
  240. type: 'html',
  241. contentLength: 999,
  242. weightCheck: {
  243. message: 'error while downloading: 404'
  244. }
  245. }]);
  246. totalWeightObj.totalWeight.should.equal(999);
  247. });
  248. });