]>
jfr.im git - yt-dlp.git/blob - test/test_socks.py
2 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
11 from test
.helper
import FakeYDL
, get_params
, is_download_test
13 from yt_dlp
.compat
import compat_str
, compat_urllib_request
17 class TestMultipleSocks(unittest
.TestCase
):
19 def _check_params(attrs
):
22 if attr
not in params
:
23 print('Missing %s. Skipping.' % attr
)
27 def test_proxy_http(self
):
28 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
32 'proxy': params
['primary_proxy']
35 ydl
.urlopen('http://yt-dl.org/ip').read().decode(),
36 params
['primary_server_ip'])
38 def test_proxy_https(self
):
39 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
43 'proxy': params
['primary_proxy']
46 ydl
.urlopen('https://yt-dl.org/ip').read().decode(),
47 params
['primary_server_ip'])
49 def test_secondary_proxy_http(self
):
50 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
54 req
= compat_urllib_request
.Request('http://yt-dl.org/ip')
55 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
57 ydl
.urlopen(req
).read().decode(),
58 params
['secondary_server_ip'])
60 def test_secondary_proxy_https(self
):
61 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
65 req
= compat_urllib_request
.Request('https://yt-dl.org/ip')
66 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
68 ydl
.urlopen(req
).read().decode(),
69 params
['secondary_server_ip'])
73 class TestSocks(unittest
.TestCase
):
74 _SKIP_SOCKS_TEST
= True
77 if self
._SKIP
_SOCKS
_TEST
:
80 self
.port
= random
.randint(20000, 30000)
81 self
.server_process
= subprocess
.Popen([
82 'srelay', '-f', '-i', '127.0.0.1:%d' % self
.port
],
83 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
86 if self
._SKIP
_SOCKS
_TEST
:
89 self
.server_process
.terminate()
90 self
.server_process
.communicate()
92 def _get_ip(self
, protocol
):
93 if self
._SKIP
_SOCKS
_TEST
:
97 'proxy': '%s://127.0.0.1:%d' % (protocol
, self
.port
),
99 return ydl
.urlopen('http://yt-dl.org/ip').read().decode()
101 def test_socks4(self
):
102 self
.assertTrue(isinstance(self
._get
_ip
('socks4'), compat_str
))
104 def test_socks4a(self
):
105 self
.assertTrue(isinstance(self
._get
_ip
('socks4a'), compat_str
))
107 def test_socks5(self
):
108 self
.assertTrue(isinstance(self
._get
_ip
('socks5'), compat_str
))
111 if __name__
== '__main__':