download-test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var assert = require('assert');
  2. var path = require('path');
  3. var fs = require('fs');
  4. var streamEqual = require('stream-equal');
  5. var nock = require('./nock');
  6. var ytdl = require('..');
  7. var VIDEO_BASE = 'https://www.youtube.com/watch?v=';
  8. describe('Download video', function() {
  9. var id = '_HSylqgVYQI';
  10. var link = VIDEO_BASE + id;
  11. var video = path.resolve(__dirname, 'files/' + id + '/video.flv');
  12. var filter = function(format) { return format.container === 'mp4'; };
  13. it('Should be pipeable and data equal to stored file', function(done) {
  14. var scope1 = nock(id, { dashmpd: true });
  15. var stream = ytdl(link, { filter: filter });
  16. var infoEmitted = false;
  17. var scope2;
  18. stream.on('info', function(info, format) {
  19. infoEmitted = true;
  20. scope2 = nock.url(format.url)
  21. .replyWithFile(200, video);
  22. });
  23. var filestream = fs.createReadStream(video);
  24. streamEqual(filestream, stream, function(err, equal) {
  25. scope1.done();
  26. scope2.done();
  27. if (err) return done(err);
  28. assert.ok(infoEmitted);
  29. assert.ok(equal);
  30. done();
  31. });
  32. });
  33. describe('that redirects', function() {
  34. it('Should download file after redirect', function(done) {
  35. var id = '_HSylqgVYQI';
  36. var link = VIDEO_BASE + id;
  37. var video = path.resolve(__dirname, 'files/' + id + '/video.flv');
  38. var filter = function(format) { return format.container === 'mp4'; };
  39. var scope1 = nock(id, { dashmpd: true });
  40. var stream = ytdl(link, { filter: filter });
  41. var scope2, scope3;
  42. stream.on('info', function(info, format) {
  43. scope2 = nock.url(format.url)
  44. .reply(302, '', { Location: 'http://somehost.com/somefile.mp4' });
  45. scope3 = nock.url('http://somehost.com/somefile.mp4')
  46. .replyWithFile(200, video);
  47. });
  48. var filestream = fs.createReadStream(video);
  49. streamEqual(filestream, stream, function(err, equal) {
  50. scope1.done();
  51. scope2.done();
  52. scope3.done();
  53. if (err) return done(err);
  54. assert.ok(equal);
  55. done();
  56. });
  57. });
  58. });
  59. });