[ie/soundcloud] Various fixes (#11820)
[yt-dlp.git] / test / test_downloader_http.py
blobcf2e3fac16b05b789daab126b3d71545cab03d87
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__))))
11 import http.server
12 import re
13 import threading
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__))
23 TEST_SIZE = 10 * 1024
26 class HTTPTestRequestHandler(http.server.BaseHTTPRequestHandler):
27 def log_message(self, format, *args):
28 pass
30 def send_content_range(self, total=None):
31 range_header = self.headers.get('Range')
32 start = end = None
33 if range_header:
34 mobj = re.search(r'^bytes=(\d+)-(\d+)', range_header)
35 if mobj:
36 start = int(mobj.group(1))
37 end = int(mobj.group(2))
38 valid_range = start is not None and end is not None
39 if valid_range:
40 content_range = f'bytes {start}-{end}'
41 if total:
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')
49 size = TEST_SIZE
50 if range:
51 size = self.send_content_range(TEST_SIZE)
52 if content_length:
53 self.send_header('Content-Length', size)
54 self.end_headers()
55 self.wfile.write(b'#' * size)
57 def do_GET(self):
58 if self.path == '/regular':
59 self.serve()
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)
66 else:
67 assert False
70 class TestHttpFD(unittest.TestCase):
71 def setUp(self):
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'
84 try_rm(filename)
85 self.assertTrue(downloader.real_download(filename, {
86 'url': f'http://127.0.0.1:{self.port}/{ep}',
87 }), ep)
88 self.assertEqual(os.path.getsize(filename), TEST_SIZE, ep)
89 try_rm(filename)
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):
96 self.download_all({})
98 def test_chunked(self):
99 self.download_all({
100 'http_chunk_size': 1000,
104 if __name__ == '__main__':
105 unittest.main()