You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.7 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import re
  7. import sys
  8. import unittest
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. from test.helper import try_rm
  11. from youtube_dl import YoutubeDL
  12. from youtube_dl.compat import compat_http_server
  13. from youtube_dl.downloader.http import HttpFD
  14. from youtube_dl.utils import encodeFilename
  15. import ssl
  16. import threading
  17. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  18. def http_server_port(httpd):
  19. if os.name == 'java' and isinstance(httpd.socket, ssl.SSLSocket):
  20. # In Jython SSLSocket is not a subclass of socket.socket
  21. sock = httpd.socket.sock
  22. else:
  23. sock = httpd.socket
  24. return sock.getsockname()[1]
  25. TEST_SIZE = 10 * 1024
  26. class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
  27. def log_message(self, format, *args):
  28. pass
  29. def send_content_range(self, total=None):
  30. range_header = self.headers.get('Range')
  31. start = end = None
  32. if range_header:
  33. mobj = re.search(r'^bytes=(\d+)-(\d+)', range_header)
  34. if mobj:
  35. start = int(mobj.group(1))
  36. end = int(mobj.group(2))
  37. valid_range = start is not None and end is not None
  38. if valid_range:
  39. content_range = 'bytes %d-%d' % (start, end)
  40. if total:
  41. content_range += '/%d' % total
  42. self.send_header('Content-Range', content_range)
  43. return (end - start + 1) if valid_range else total
  44. def serve(self, range=True, content_length=True):
  45. self.send_response(200)
  46. self.send_header('Content-Type', 'video/mp4')
  47. size = TEST_SIZE
  48. if range:
  49. size = self.send_content_range(TEST_SIZE)
  50. if content_length:
  51. self.send_header('Content-Length', size)
  52. self.end_headers()
  53. self.wfile.write(b'#' * size)
  54. def do_GET(self):
  55. if self.path == '/regular':
  56. self.serve()
  57. elif self.path == '/no-content-length':
  58. self.serve(content_length=False)
  59. elif self.path == '/no-range':
  60. self.serve(range=False)
  61. elif self.path == '/no-range-no-content-length':
  62. self.serve(range=False, content_length=False)
  63. else:
  64. assert False
  65. class FakeLogger(object):
  66. def debug(self, msg):
  67. pass
  68. def warning(self, msg):
  69. pass
  70. def error(self, msg):
  71. pass
  72. class TestHttpFD(unittest.TestCase):
  73. def setUp(self):
  74. self.httpd = compat_http_server.HTTPServer(
  75. ('127.0.0.1', 0), HTTPTestRequestHandler)
  76. self.port = http_server_port(self.httpd)
  77. self.server_thread = threading.Thread(target=self.httpd.serve_forever)
  78. self.server_thread.daemon = True
  79. 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': 'http://127.0.0.1:%d/%s' % (self.port, ep),
  88. }))
  89. self.assertEqual(os.path.getsize(encodeFilename(filename)), TEST_SIZE)
  90. try_rm(encodeFilename(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)
  94. def test_regular(self):
  95. self.download_all({})
  96. def test_chunked(self):
  97. self.download_all({
  98. 'http_chunk_size': 1000,
  99. })
  100. if __name__ == '__main__':
  101. unittest.main()