]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
72f3289a YCH |
2 | # Allow direct execution |
3 | import os | |
4 | import sys | |
5 | import unittest | |
6 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
7 | ||
e21f17fc YCH |
8 | import random |
9 | import subprocess | |
72f3289a | 10 | |
e21f17fc YCH |
11 | from test.helper import ( |
12 | FakeYDL, | |
13 | get_params, | |
060ac762 | 14 | is_download_test, |
e21f17fc | 15 | ) |
7a5c1cfe | 16 | from yt_dlp.compat import ( |
e21f17fc YCH |
17 | compat_str, |
18 | compat_urllib_request, | |
19 | ) | |
72f3289a | 20 | |
e21f17fc | 21 | |
060ac762 | 22 | @is_download_test |
e21f17fc | 23 | class TestMultipleSocks(unittest.TestCase): |
72f3289a YCH |
24 | @staticmethod |
25 | def _check_params(attrs): | |
26 | params = get_params() | |
27 | for attr in attrs: | |
28 | if attr not in params: | |
29 | print('Missing %s. Skipping.' % attr) | |
30 | return | |
31 | return params | |
32 | ||
33 | def test_proxy_http(self): | |
34 | params = self._check_params(['primary_proxy', 'primary_server_ip']) | |
35 | if params is None: | |
36 | return | |
37 | ydl = FakeYDL({ | |
38 | 'proxy': params['primary_proxy'] | |
39 | }) | |
40 | self.assertEqual( | |
41 | ydl.urlopen('http://yt-dl.org/ip').read().decode('utf-8'), | |
42 | params['primary_server_ip']) | |
43 | ||
44 | def test_proxy_https(self): | |
45 | params = self._check_params(['primary_proxy', 'primary_server_ip']) | |
46 | if params is None: | |
47 | return | |
48 | ydl = FakeYDL({ | |
49 | 'proxy': params['primary_proxy'] | |
50 | }) | |
51 | self.assertEqual( | |
52 | ydl.urlopen('https://yt-dl.org/ip').read().decode('utf-8'), | |
53 | params['primary_server_ip']) | |
54 | ||
55 | def test_secondary_proxy_http(self): | |
56 | params = self._check_params(['secondary_proxy', 'secondary_server_ip']) | |
57 | if params is None: | |
58 | return | |
59 | ydl = FakeYDL() | |
60 | req = compat_urllib_request.Request('http://yt-dl.org/ip') | |
61 | req.add_header('Ytdl-request-proxy', params['secondary_proxy']) | |
62 | self.assertEqual( | |
63 | ydl.urlopen(req).read().decode('utf-8'), | |
64 | params['secondary_server_ip']) | |
65 | ||
66 | def test_secondary_proxy_https(self): | |
67 | params = self._check_params(['secondary_proxy', 'secondary_server_ip']) | |
68 | if params is None: | |
69 | return | |
70 | ydl = FakeYDL() | |
71 | req = compat_urllib_request.Request('https://yt-dl.org/ip') | |
72 | req.add_header('Ytdl-request-proxy', params['secondary_proxy']) | |
73 | self.assertEqual( | |
74 | ydl.urlopen(req).read().decode('utf-8'), | |
75 | params['secondary_server_ip']) | |
76 | ||
77 | ||
060ac762 | 78 | @is_download_test |
e21f17fc | 79 | class TestSocks(unittest.TestCase): |
98d560f2 YCH |
80 | _SKIP_SOCKS_TEST = True |
81 | ||
e21f17fc | 82 | def setUp(self): |
98d560f2 YCH |
83 | if self._SKIP_SOCKS_TEST: |
84 | return | |
85 | ||
c2876afa | 86 | self.port = random.randint(20000, 30000) |
e21f17fc YCH |
87 | self.server_process = subprocess.Popen([ |
88 | 'srelay', '-f', '-i', '127.0.0.1:%d' % self.port], | |
89 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
90 | ||
91 | def tearDown(self): | |
98d560f2 YCH |
92 | if self._SKIP_SOCKS_TEST: |
93 | return | |
94 | ||
e21f17fc YCH |
95 | self.server_process.terminate() |
96 | self.server_process.communicate() | |
97 | ||
98 | def _get_ip(self, protocol): | |
98d560f2 YCH |
99 | if self._SKIP_SOCKS_TEST: |
100 | return '127.0.0.1' | |
101 | ||
e21f17fc YCH |
102 | ydl = FakeYDL({ |
103 | 'proxy': '%s://127.0.0.1:%d' % (protocol, self.port), | |
104 | }) | |
105 | return ydl.urlopen('http://yt-dl.org/ip').read().decode('utf-8') | |
106 | ||
107 | def test_socks4(self): | |
108 | self.assertTrue(isinstance(self._get_ip('socks4'), compat_str)) | |
109 | ||
110 | def test_socks4a(self): | |
111 | self.assertTrue(isinstance(self._get_ip('socks4a'), compat_str)) | |
112 | ||
113 | def test_socks5(self): | |
114 | self.assertTrue(isinstance(self._get_ip('socks5'), compat_str)) | |
115 | ||
116 | ||
72f3289a YCH |
117 | if __name__ == '__main__': |
118 | unittest.main() |