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