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