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.

134 lines
4.6 KiB

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