]> jfr.im git - yt-dlp.git/blob - test/test_downloader_external.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / test / test_downloader_external.py
1 #!/usr/bin/env python3
2
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
7
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10 import http.cookiejar
11
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,
20 )
21
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': {},
39 }
40
41 TEST_INFO = {'url': 'http://www.example.com/'}
42
43
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/'])
51
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'])
57
58
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/'])
66
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/'])
72
73
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))
82
83
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))
93
94
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'))
101
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)
106
107
108 @unittest.skipUnless(FFmpegFD.available(), 'ffmpeg not found')
109 class TestFFmpegFD(unittest.TestCase):
110 _args = []
111
112 def _test_cmd(self, args):
113 self._args = args
114
115 def test_make_cmd(self):
116 with FakeYDL() as ydl:
117 downloader = FFmpegFD(ydl, {})
118 downloader._debug_cmd = self._test_cmd
119
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'])
124
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'])
131
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'])
136
137
138 if __name__ == '__main__':
139 unittest.main()