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
import encodeFilename
19 from yt_dlp
.utils
._utils
import _YDLLogger
as FakeLogger
21 TEST_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
27 class HTTPTestRequestHandler(http
.server
.BaseHTTPRequestHandler
):
28 def log_message(self
, format
, *args
):
31 def send_content_range(self
, total
=None):
32 range_header
= self
.headers
.get('Range')
35 mobj
= re
.search(r
'^bytes=(\d+)-(\d+)', range_header
)
37 start
= int(mobj
.group(1))
38 end
= int(mobj
.group(2))
39 valid_range
= start
is not None and end
is not None
41 content_range
= f
'bytes {start}-{end}'
43 content_range
+= f
'/{total}'
44 self
.send_header('Content-Range', content_range
)
45 return (end
- start
+ 1) if valid_range
else total
47 def serve(self
, range=True, content_length
=True):
48 self
.send_response(200)
49 self
.send_header('Content-Type', 'video/mp4')
52 size
= self
.send_content_range(TEST_SIZE
)
54 self
.send_header('Content-Length', size
)
56 self
.wfile
.write(b
'#' * size
)
59 if self
.path
== '/regular':
61 elif self
.path
== '/no-content-length':
62 self
.serve(content_length
=False)
63 elif self
.path
== '/no-range':
64 self
.serve(range=False)
65 elif self
.path
== '/no-range-no-content-length':
66 self
.serve(range=False, content_length
=False)
71 class TestHttpFD(unittest
.TestCase
):
73 self
.httpd
= http
.server
.HTTPServer(
74 ('127.0.0.1', 0), HTTPTestRequestHandler
)
75 self
.port
= http_server_port(self
.httpd
)
76 self
.server_thread
= threading
.Thread(target
=self
.httpd
.serve_forever
)
77 self
.server_thread
.daemon
= True
78 self
.server_thread
.start()
80 def download(self
, params
, ep
):
81 params
['logger'] = FakeLogger()
82 ydl
= YoutubeDL(params
)
83 downloader
= HttpFD(ydl
, params
)
84 filename
= 'testfile.mp4'
85 try_rm(encodeFilename(filename
))
86 self
.assertTrue(downloader
.real_download(filename
, {
87 'url': f
'http://127.0.0.1:{self.port}/{ep}',
89 self
.assertEqual(os
.path
.getsize(encodeFilename(filename
)), TEST_SIZE
, ep
)
90 try_rm(encodeFilename(filename
))
92 def download_all(self
, params
):
93 for ep
in ('regular', 'no-content-length', 'no-range', 'no-range-no-content-length'):
94 self
.download(params
, ep
)
96 def test_regular(self
):
99 def test_chunked(self
):
101 'http_chunk_size': 1000,
105 if __name__
== '__main__':