info-test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var assert = require('assert-diff');
  2. var nock = require('./nock');
  3. var ytdl = require('..');
  4. var VIDEO_PATH = '/watch?v=';
  5. var VIDEO_BASE = 'https://www.youtube.com' + VIDEO_PATH;
  6. describe('ytdl.getInfo()', function() {
  7. describe('from a video', function() {
  8. var id = 'pJk0p-98Xzc';
  9. var url = VIDEO_BASE + id;
  10. var expectedInfo = require('./files/' + id + '/info.json');
  11. it('Retrieves correct metainfo', function(done) {
  12. var scope = nock(id, {
  13. dashmpd: true,
  14. player: 'new-en_US-vflIUNjzZ',
  15. });
  16. ytdl.getInfo(url, function(err, info) {
  17. scope.done();
  18. if (err) return done(err);
  19. assert.deepEqual(info, expectedInfo);
  20. done();
  21. });
  22. });
  23. describe('hit the same video twice', function() {
  24. it('Gets html5player tokens from cache', function(done) {
  25. var scope = nock(id, { dashmpd: true });
  26. ytdl.getInfo(url, function(err, info) {
  27. scope.done();
  28. if (err) return done(err);
  29. assert.ok(info);
  30. done();
  31. });
  32. });
  33. });
  34. describe('use `ytdl.downloadFromInfo()`', function() {
  35. it('Retrives video file', function(done) {
  36. var stream = ytdl.downloadFromInfo(expectedInfo);
  37. stream.on('info', function(info, format) {
  38. nock.url(format.url)
  39. .reply(200);
  40. });
  41. stream.resume();
  42. stream.on('error', done);
  43. stream.on('end', done);
  44. });
  45. });
  46. });
  47. describe('from a non-existant video', function() {
  48. var id = 'not-found';
  49. var url = VIDEO_BASE + id;
  50. it('Should give an error', function(done) {
  51. var scope = nock(id);
  52. ytdl.getInfo(url, function(err) {
  53. scope.done();
  54. assert.ok(err);
  55. assert.equal(err.message, 'Video not found');
  56. done();
  57. });
  58. });
  59. });
  60. describe('from an age restricted video', function() {
  61. var id = 'rIqCiJKWx9I';
  62. var url = VIDEO_BASE + id;
  63. var expectedInfo = require('./files/' + id + '/info.json');
  64. it('Returns correct video metainfo', function(done) {
  65. var scope = nock(id, {
  66. dashmpd: true,
  67. embed: true,
  68. get_video_info: true,
  69. });
  70. ytdl.getInfo(url, function(err, info) {
  71. scope.done();
  72. if (err) return done(err);
  73. assert.deepEqual(info, expectedInfo);
  74. done();
  75. });
  76. });
  77. });
  78. });