2 from __future__ import unicode_literals
4 # Allow direct execution
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10 from youtube_dl import YoutubeDL
11 from youtube_dl.compat import compat_http_server
15 TEST_DIR = os.path.dirname(os.path.abspath(__file__))
17 class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
18 def log_message(self, format, *args):
22 if self.path == '/video.html':
23 self.send_response(200)
24 self.send_header('Content-Type', 'text/html; charset=utf-8')
26 self.wfile.write(b'<html><video src="/vid.mp4" /></html>')
27 elif self.path == '/vid.mp4':
28 self.send_response(200)
29 self.send_header('Content-Type', 'video/mp4')
31 self.wfile.write(b'\x00\x00\x00\x00\x20\x66\x74[video]')
36 class FakeLogger(object):
40 def warning(self, msg):
47 class TestHTTP(unittest.TestCase):
49 certfn = os.path.join(TEST_DIR, 'testcert.pem')
50 self.httpd = compat_http_server.HTTPServer(
51 ('localhost', 0), HTTPTestRequestHandler)
52 self.httpd.socket = ssl.wrap_socket(
53 self.httpd.socket, certfile=certfn, server_side=True)
54 self.port = self.httpd.socket.getsockname()[1]
55 self.server_thread = threading.Thread(target=self.httpd.serve_forever)
56 self.server_thread.daemon = True
57 self.server_thread.start()
59 def test_nocheckcertificate(self):
60 if sys.version_info >= (2, 7, 9): # No certificate checking anyways
61 ydl = YoutubeDL({'logger': FakeLogger()})
64 ydl.extract_info, 'https://localhost:%d/video.html' % self.port)
66 ydl = YoutubeDL({'logger': FakeLogger(), 'nocheckcertificate': True})
67 r = ydl.extract_info('https://localhost:%d/video.html' % self.port)
68 self.assertEqual(r['url'], 'https://localhost:%d/vid.mp4' % self.port)
70 if __name__ == '__main__':