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.

124 lines
4.2 KiB

10 years ago
  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. from youtube_dl import YoutubeDL
  9. from youtube_dl.compat import compat_http_server, compat_urllib_request
  10. import ssl
  11. import threading
  12. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  13. class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
  14. def log_message(self, format, *args):
  15. pass
  16. def do_GET(self):
  17. if self.path == '/video.html':
  18. self.send_response(200)
  19. self.send_header('Content-Type', 'text/html; charset=utf-8')
  20. self.end_headers()
  21. self.wfile.write(b'<html><video src="/vid.mp4" /></html>')
  22. elif self.path == '/vid.mp4':
  23. self.send_response(200)
  24. self.send_header('Content-Type', 'video/mp4')
  25. self.end_headers()
  26. self.wfile.write(b'\x00\x00\x00\x00\x20\x66\x74[video]')
  27. else:
  28. assert False
  29. class FakeLogger(object):
  30. def debug(self, msg):
  31. pass
  32. def warning(self, msg):
  33. pass
  34. def error(self, msg):
  35. pass
  36. class TestHTTP(unittest.TestCase):
  37. def setUp(self):
  38. certfn = os.path.join(TEST_DIR, 'testcert.pem')
  39. self.httpd = compat_http_server.HTTPServer(
  40. ('localhost', 0), HTTPTestRequestHandler)
  41. self.httpd.socket = ssl.wrap_socket(
  42. self.httpd.socket, certfile=certfn, server_side=True)
  43. if os.name == 'java':
  44. # In Jython SSLSocket is not a subclass of socket.socket
  45. sock = self.httpd.socket.sock
  46. else:
  47. sock = self.httpd.socket
  48. self.port = sock.getsockname()[1]
  49. self.server_thread = threading.Thread(target=self.httpd.serve_forever)
  50. self.server_thread.daemon = True
  51. self.server_thread.start()
  52. def test_nocheckcertificate(self):
  53. if sys.version_info >= (2, 7, 9): # No certificate checking anyways
  54. ydl = YoutubeDL({'logger': FakeLogger()})
  55. self.assertRaises(
  56. Exception,
  57. ydl.extract_info, 'https://localhost:%d/video.html' % self.port)
  58. ydl = YoutubeDL({'logger': FakeLogger(), 'nocheckcertificate': True})
  59. r = ydl.extract_info('https://localhost:%d/video.html' % self.port)
  60. self.assertEqual(r['url'], 'https://localhost:%d/vid.mp4' % self.port)
  61. def _build_proxy_handler(name):
  62. class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
  63. proxy_name = name
  64. def log_message(self, format, *args):
  65. pass
  66. def do_GET(self):
  67. self.send_response(200)
  68. self.send_header('Content-Type', 'text/plain; charset=utf-8')
  69. self.end_headers()
  70. self.wfile.write('{self.proxy_name}: {self.path}'.format(self=self).encode('utf-8'))
  71. return HTTPTestRequestHandler
  72. class TestProxy(unittest.TestCase):
  73. def setUp(self):
  74. self.proxy = compat_http_server.HTTPServer(
  75. ('localhost', 0), _build_proxy_handler('normal'))
  76. self.port = self.proxy.socket.getsockname()[1]
  77. self.proxy_thread = threading.Thread(target=self.proxy.serve_forever)
  78. self.proxy_thread.daemon = True
  79. self.proxy_thread.start()
  80. self.cn_proxy = compat_http_server.HTTPServer(
  81. ('localhost', 0), _build_proxy_handler('cn'))
  82. self.cn_port = self.cn_proxy.socket.getsockname()[1]
  83. self.cn_proxy_thread = threading.Thread(target=self.cn_proxy.serve_forever)
  84. self.cn_proxy_thread.daemon = True
  85. self.cn_proxy_thread.start()
  86. def test_proxy(self):
  87. cn_proxy = 'localhost:{0}'.format(self.cn_port)
  88. ydl = YoutubeDL({
  89. 'proxy': 'localhost:{0}'.format(self.port),
  90. 'cn_verification_proxy': cn_proxy,
  91. })
  92. url = 'http://foo.com/bar'
  93. response = ydl.urlopen(url).read().decode('utf-8')
  94. self.assertEqual(response, 'normal: {0}'.format(url))
  95. req = compat_urllib_request.Request(url)
  96. req.add_header('Ytdl-request-proxy', cn_proxy)
  97. response = ydl.urlopen(req).read().decode('utf-8')
  98. self.assertEqual(response, 'cn: {0}'.format(url))
  99. if __name__ == '__main__':
  100. unittest.main()