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