]>
Commit | Line | Data |
---|---|---|
83fda3c0 PH |
1 | #!/usr/bin/env python |
2 | from __future__ import unicode_literals | |
3 | ||
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 | ||
10 | from youtube_dl import YoutubeDL | |
11 | from youtube_dl.compat import compat_http_server | |
12 | import ssl | |
13 | import threading | |
14 | ||
15 | TEST_DIR = os.path.dirname(os.path.abspath(__file__)) | |
16 | ||
17 | class HTTPTestRequestHandler(compat_http_server.BaseHTTPRequestHandler): | |
18 | def log_message(self, format, *args): | |
19 | pass | |
20 | ||
21 | def do_GET(self): | |
22 | if self.path == '/video.html': | |
23 | self.send_response(200) | |
24 | self.send_header('Content-Type', 'text/html; charset=utf-8') | |
25 | self.end_headers() | |
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') | |
30 | self.end_headers() | |
31 | self.wfile.write(b'\x00\x00\x00\x00\x20\x66\x74[video]') | |
32 | else: | |
33 | assert False | |
34 | ||
35 | ||
36 | class FakeLogger(object): | |
37 | def debug(self, msg): | |
38 | pass | |
39 | ||
40 | def warning(self, msg): | |
41 | pass | |
42 | ||
43 | def error(self, msg): | |
44 | pass | |
45 | ||
46 | ||
47 | class TestHTTP(unittest.TestCase): | |
48 | def setUp(self): | |
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() | |
58 | ||
59 | def test_nocheckcertificate(self): | |
60 | if sys.version_info >= (2, 7, 9): # No certificate checking anyways | |
61 | ydl = YoutubeDL({'logger': FakeLogger()}) | |
62 | self.assertRaises( | |
63 | Exception, | |
64 | ydl.extract_info, 'https://localhost:%d/video.html' % self.port) | |
65 | ||
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) | |
69 | ||
70 | if __name__ == '__main__': | |
71 | unittest.main() |