test_downloader_http.py 3.3 KB

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