]>
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__
))))
13 from test
.helper
import FakeYDL
, get_params
, is_download_test
14 from yt_dlp
.compat
import compat_str
18 class TestMultipleSocks(unittest
.TestCase
):
20 def _check_params(attrs
):
23 if attr
not in params
:
24 print('Missing %s. Skipping.' % attr
)
28 def test_proxy_http(self
):
29 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
33 'proxy': params
['primary_proxy']
36 ydl
.urlopen('http://yt-dl.org/ip').read().decode(),
37 params
['primary_server_ip'])
39 def test_proxy_https(self
):
40 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
44 'proxy': params
['primary_proxy']
47 ydl
.urlopen('https://yt-dl.org/ip').read().decode(),
48 params
['primary_server_ip'])
50 def test_secondary_proxy_http(self
):
51 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
55 req
= urllib
.request
.Request('http://yt-dl.org/ip')
56 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
58 ydl
.urlopen(req
).read().decode(),
59 params
['secondary_server_ip'])
61 def test_secondary_proxy_https(self
):
62 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
66 req
= urllib
.request
.Request('https://yt-dl.org/ip')
67 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
69 ydl
.urlopen(req
).read().decode(),
70 params
['secondary_server_ip'])
74 class TestSocks(unittest
.TestCase
):
75 _SKIP_SOCKS_TEST
= True
78 if self
._SKIP
_SOCKS
_TEST
:
81 self
.port
= random
.randint(20000, 30000)
82 self
.server_process
= subprocess
.Popen([
83 'srelay', '-f', '-i', '127.0.0.1:%d' % self
.port
],
84 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
87 if self
._SKIP
_SOCKS
_TEST
:
90 self
.server_process
.terminate()
91 self
.server_process
.communicate()
93 def _get_ip(self
, protocol
):
94 if self
._SKIP
_SOCKS
_TEST
:
98 'proxy': '%s://127.0.0.1:%d' % (protocol
, self
.port
),
100 return ydl
.urlopen('http://yt-dl.org/ip').read().decode()
102 def test_socks4(self
):
103 self
.assertTrue(isinstance(self
._get
_ip
('socks4'), compat_str
))
105 def test_socks4a(self
):
106 self
.assertTrue(isinstance(self
._get
_ip
('socks4a'), compat_str
))
108 def test_socks5(self
):
109 self
.assertTrue(isinstance(self
._get
_ip
('socks5'), compat_str
))
112 if __name__
== '__main__':