]>
jfr.im git - yt-dlp.git/blob - test/test_socks.py
3 # Allow direct execution
8 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
15 from test
.helper
import FakeYDL
, get_params
, is_download_test
16 from yt_dlp
.compat
import compat_str
20 class TestMultipleSocks(unittest
.TestCase
):
22 def _check_params(attrs
):
25 if attr
not in params
:
26 print('Missing %s. Skipping.' % attr
)
30 def test_proxy_http(self
):
31 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
35 'proxy': params
['primary_proxy']
38 ydl
.urlopen('http://yt-dl.org/ip').read().decode(),
39 params
['primary_server_ip'])
41 def test_proxy_https(self
):
42 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
46 'proxy': params
['primary_proxy']
49 ydl
.urlopen('https://yt-dl.org/ip').read().decode(),
50 params
['primary_server_ip'])
52 def test_secondary_proxy_http(self
):
53 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
57 req
= urllib
.request
.Request('http://yt-dl.org/ip')
58 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
60 ydl
.urlopen(req
).read().decode(),
61 params
['secondary_server_ip'])
63 def test_secondary_proxy_https(self
):
64 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
68 req
= urllib
.request
.Request('https://yt-dl.org/ip')
69 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
71 ydl
.urlopen(req
).read().decode(),
72 params
['secondary_server_ip'])
76 class TestSocks(unittest
.TestCase
):
77 _SKIP_SOCKS_TEST
= True
80 if self
._SKIP
_SOCKS
_TEST
:
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
)
89 if self
._SKIP
_SOCKS
_TEST
:
92 self
.server_process
.terminate()
93 self
.server_process
.communicate()
95 def _get_ip(self
, protocol
):
96 if self
._SKIP
_SOCKS
_TEST
:
100 'proxy': '%s://127.0.0.1:%d' % (protocol
, self
.port
),
102 return ydl
.urlopen('http://yt-dl.org/ip').read().decode()
104 def test_socks4(self
):
105 self
.assertTrue(isinstance(self
._get
_ip
('socks4'), compat_str
))
107 def test_socks4a(self
):
108 self
.assertTrue(isinstance(self
._get
_ip
('socks4a'), compat_str
))
110 def test_socks5(self
):
111 self
.assertTrue(isinstance(self
._get
_ip
('socks5'), compat_str
))
114 if __name__
== '__main__':