test_downloader_external.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import http.cookiejar
  8. from test.helper import FakeYDL
  9. from yt_dlp.downloader.external import (
  10. Aria2cFD,
  11. AxelFD,
  12. CurlFD,
  13. FFmpegFD,
  14. HttpieFD,
  15. WgetFD,
  16. )
  17. TEST_COOKIE = {
  18. 'version': 0,
  19. 'name': 'test',
  20. 'value': 'ytdlp',
  21. 'port': None,
  22. 'port_specified': False,
  23. 'domain': '.example.com',
  24. 'domain_specified': True,
  25. 'domain_initial_dot': False,
  26. 'path': '/',
  27. 'path_specified': True,
  28. 'secure': False,
  29. 'expires': None,
  30. 'discard': False,
  31. 'comment': None,
  32. 'comment_url': None,
  33. 'rest': {},
  34. }
  35. TEST_INFO = {'url': 'http://www.example.com/'}
  36. class TestHttpieFD(unittest.TestCase):
  37. def test_make_cmd(self):
  38. with FakeYDL() as ydl:
  39. downloader = HttpieFD(ydl, {})
  40. self.assertEqual(
  41. downloader._make_cmd('test', TEST_INFO),
  42. ['http', '--download', '--output', 'test', 'http://www.example.com/'])
  43. # Test cookie header is added
  44. ydl.cookiejar.set_cookie(http.cookiejar.Cookie(**TEST_COOKIE))
  45. self.assertEqual(
  46. downloader._make_cmd('test', TEST_INFO),
  47. ['http', '--download', '--output', 'test', 'http://www.example.com/', 'Cookie:test=ytdlp'])
  48. class TestAxelFD(unittest.TestCase):
  49. def test_make_cmd(self):
  50. with FakeYDL() as ydl:
  51. downloader = AxelFD(ydl, {})
  52. self.assertEqual(
  53. downloader._make_cmd('test', TEST_INFO),
  54. ['axel', '-o', 'test', '--', 'http://www.example.com/'])
  55. # Test cookie header is added
  56. ydl.cookiejar.set_cookie(http.cookiejar.Cookie(**TEST_COOKIE))
  57. self.assertEqual(
  58. downloader._make_cmd('test', TEST_INFO),
  59. ['axel', '-o', 'test', '-H', 'Cookie: test=ytdlp', '--max-redirect=0', '--', 'http://www.example.com/'])
  60. class TestWgetFD(unittest.TestCase):
  61. def test_make_cmd(self):
  62. with FakeYDL() as ydl:
  63. downloader = WgetFD(ydl, {})
  64. self.assertNotIn('--load-cookies', downloader._make_cmd('test', TEST_INFO))
  65. # Test cookiejar tempfile arg is added
  66. ydl.cookiejar.set_cookie(http.cookiejar.Cookie(**TEST_COOKIE))
  67. self.assertIn('--load-cookies', downloader._make_cmd('test', TEST_INFO))
  68. class TestCurlFD(unittest.TestCase):
  69. def test_make_cmd(self):
  70. with FakeYDL() as ydl:
  71. downloader = CurlFD(ydl, {})
  72. self.assertNotIn('--cookie', downloader._make_cmd('test', TEST_INFO))
  73. # Test cookie header is added
  74. ydl.cookiejar.set_cookie(http.cookiejar.Cookie(**TEST_COOKIE))
  75. self.assertIn('--cookie', downloader._make_cmd('test', TEST_INFO))
  76. self.assertIn('test=ytdlp', downloader._make_cmd('test', TEST_INFO))
  77. class TestAria2cFD(unittest.TestCase):
  78. def test_make_cmd(self):
  79. with FakeYDL() as ydl:
  80. downloader = Aria2cFD(ydl, {})
  81. downloader._make_cmd('test', TEST_INFO)
  82. self.assertFalse(hasattr(downloader, '_cookies_tempfile'))
  83. # Test cookiejar tempfile arg is added
  84. ydl.cookiejar.set_cookie(http.cookiejar.Cookie(**TEST_COOKIE))
  85. cmd = downloader._make_cmd('test', TEST_INFO)
  86. self.assertIn(f'--load-cookies={downloader._cookies_tempfile}', cmd)
  87. @unittest.skipUnless(FFmpegFD.available(), 'ffmpeg not found')
  88. class TestFFmpegFD(unittest.TestCase):
  89. _args = []
  90. def _test_cmd(self, args):
  91. self._args = args
  92. def test_make_cmd(self):
  93. with FakeYDL() as ydl:
  94. downloader = FFmpegFD(ydl, {})
  95. downloader._debug_cmd = self._test_cmd
  96. downloader._call_downloader('test', {**TEST_INFO, 'ext': 'mp4'})
  97. self.assertEqual(self._args, [
  98. 'ffmpeg', '-y', '-hide_banner', '-i', 'http://www.example.com/',
  99. '-c', 'copy', '-f', 'mp4', 'file:test'])
  100. # Test cookies arg is added
  101. ydl.cookiejar.set_cookie(http.cookiejar.Cookie(**TEST_COOKIE))
  102. downloader._call_downloader('test', {**TEST_INFO, 'ext': 'mp4'})
  103. self.assertEqual(self._args, [
  104. 'ffmpeg', '-y', '-hide_banner', '-cookies', 'test=ytdlp; path=/; domain=.example.com;\r\n',
  105. '-i', 'http://www.example.com/', '-c', 'copy', '-f', 'mp4', 'file:test'])
  106. # Test with non-url input (ffmpeg reads from stdin '-' for websockets)
  107. downloader._call_downloader('test', {'url': 'x', 'ext': 'mp4'})
  108. self.assertEqual(self._args, [
  109. 'ffmpeg', '-y', '-hide_banner', '-i', 'x', '-c', 'copy', '-f', 'mp4', 'file:test'])
  110. if __name__ == '__main__':
  111. unittest.main()