3 # Allow direct execution
8 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
15 from test
.helper
import http_server_port
, try_rm
16 from yt_dlp
import YoutubeDL
17 from yt_dlp
.downloader
.http
import HttpFD
18 from yt_dlp
.utils
._utils
import _YDLLogger
as FakeLogger
20 TEST_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
26 class HTTPTestRequestHandler(http
.server
.BaseHTTPRequestHandler
):
27 def log_message(self
, format
, *args
):
30 def send_content_range(self
, total
=None):
31 range_header
= self
.headers
.get('Range')
34 mobj
= re
.search(r
'^bytes=(\d+)-(\d+)', range_header
)
36 start
= int(mobj
.group(1))
37 end
= int(mobj
.group(2))
38 valid_range
= start
is not None and end
is not None
40 content_range
= f
'bytes {start}-{end}'
42 content_range
+= f
'/{total}'
43 self
.send_header('Content-Range', content_range
)
44 return (end
- start
+ 1) if valid_range
else total
46 def serve(self
, range=True, content_length
=True):
47 self
.send_response(200)
48 self
.send_header('Content-Type', 'video/mp4')
51 size
= self
.send_content_range(TEST_SIZE
)
53 self
.send_header('Content-Length', size
)
55 self
.wfile
.write(b
'#' * size
)
58 if self
.path
== '/regular':
60 elif self
.path
== '/no-content-length':
61 self
.serve(content_length
=False)
62 elif self
.path
== '/no-range':
63 self
.serve(range=False)
64 elif self
.path
== '/no-range-no-content-length':
65 self
.serve(range=False, content_length
=False)
70 class TestHttpFD(unittest
.TestCase
):
72 self
.httpd
= http
.server
.HTTPServer(
73 ('127.0.0.1', 0), HTTPTestRequestHandler
)
74 self
.port
= http_server_port(self
.httpd
)
75 self
.server_thread
= threading
.Thread(target
=self
.httpd
.serve_forever
)
76 self
.server_thread
.daemon
= True
77 self
.server_thread
.start()
79 def download(self
, params
, ep
):
80 params
['logger'] = FakeLogger()
81 ydl
= YoutubeDL(params
)
82 downloader
= HttpFD(ydl
, params
)
83 filename
= 'testfile.mp4'
85 self
.assertTrue(downloader
.real_download(filename
, {
86 'url': f
'http://127.0.0.1:{self.port}/{ep}',
88 self
.assertEqual(os
.path
.getsize(filename
), TEST_SIZE
, ep
)
91 def download_all(self
, params
):
92 for ep
in ('regular', 'no-content-length', 'no-range', 'no-range-no-content-length'):
93 self
.download(params
, ep
)
95 def test_regular(self
):
98 def test_chunked(self
):
100 'http_chunk_size': 1000,
104 if __name__
== '__main__':