test_downloader_http.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import http.server
  8. import re
  9. import threading
  10. from test.helper import http_server_port, try_rm
  11. from yt_dlp import YoutubeDL
  12. from yt_dlp.downloader.http import HttpFD
  13. from yt_dlp.utils._utils import _YDLLogger as FakeLogger
  14. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  15. TEST_SIZE = 10 * 1024
  16. class HTTPTestRequestHandler(http.server.BaseHTTPRequestHandler):
  17. def log_message(self, format, *args):
  18. pass
  19. def send_content_range(self, total=None):
  20. range_header = self.headers.get('Range')
  21. start = end = None
  22. if range_header:
  23. mobj = re.search(r'^bytes=(\d+)-(\d+)', range_header)
  24. if mobj:
  25. start = int(mobj.group(1))
  26. end = int(mobj.group(2))
  27. valid_range = start is not None and end is not None
  28. if valid_range:
  29. content_range = f'bytes {start}-{end}'
  30. if total:
  31. content_range += f'/{total}'
  32. self.send_header('Content-Range', content_range)
  33. return (end - start + 1) if valid_range else total
  34. def serve(self, range=True, content_length=True):
  35. self.send_response(200)
  36. self.send_header('Content-Type', 'video/mp4')
  37. size = TEST_SIZE
  38. if range:
  39. size = self.send_content_range(TEST_SIZE)
  40. if content_length:
  41. self.send_header('Content-Length', size)
  42. self.end_headers()
  43. self.wfile.write(b'#' * size)
  44. def do_GET(self):
  45. if self.path == '/regular':
  46. self.serve()
  47. elif self.path == '/no-content-length':
  48. self.serve(content_length=False)
  49. elif self.path == '/no-range':
  50. self.serve(range=False)
  51. elif self.path == '/no-range-no-content-length':
  52. self.serve(range=False, content_length=False)
  53. else:
  54. assert False
  55. class TestHttpFD(unittest.TestCase):
  56. def setUp(self):
  57. self.httpd = http.server.HTTPServer(
  58. ('127.0.0.1', 0), HTTPTestRequestHandler)
  59. self.port = http_server_port(self.httpd)
  60. self.server_thread = threading.Thread(target=self.httpd.serve_forever)
  61. self.server_thread.daemon = True
  62. self.server_thread.start()
  63. def download(self, params, ep):
  64. params['logger'] = FakeLogger()
  65. ydl = YoutubeDL(params)
  66. downloader = HttpFD(ydl, params)
  67. filename = 'testfile.mp4'
  68. try_rm(filename)
  69. self.assertTrue(downloader.real_download(filename, {
  70. 'url': f'http://127.0.0.1:{self.port}/{ep}',
  71. }), ep)
  72. self.assertEqual(os.path.getsize(filename), TEST_SIZE, ep)
  73. try_rm(filename)
  74. def download_all(self, params):
  75. for ep in ('regular', 'no-content-length', 'no-range', 'no-range-no-content-length'):
  76. self.download(params, ep)
  77. def test_regular(self):
  78. self.download_all({})
  79. def test_chunked(self):
  80. self.download_all({
  81. 'http_chunk_size': 1000,
  82. })
  83. if __name__ == '__main__':
  84. unittest.main()