]>
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
19 class TestMultipleSocks(unittest
.TestCase
):
21 def _check_params(attrs
):
24 if attr
not in params
:
25 print('Missing %s. Skipping.' % attr
)
29 def test_proxy_http(self
):
30 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
34 'proxy': params
['primary_proxy']
37 ydl
.urlopen('http://yt-dl.org/ip').read().decode(),
38 params
['primary_server_ip'])
40 def test_proxy_https(self
):
41 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
45 'proxy': params
['primary_proxy']
48 ydl
.urlopen('https://yt-dl.org/ip').read().decode(),
49 params
['primary_server_ip'])
51 def test_secondary_proxy_http(self
):
52 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
56 req
= urllib
.request
.Request('http://yt-dl.org/ip')
57 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
59 ydl
.urlopen(req
).read().decode(),
60 params
['secondary_server_ip'])
62 def test_secondary_proxy_https(self
):
63 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
67 req
= urllib
.request
.Request('https://yt-dl.org/ip')
68 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
70 ydl
.urlopen(req
).read().decode(),
71 params
['secondary_server_ip'])
75 class TestSocks(unittest
.TestCase
):
76 _SKIP_SOCKS_TEST
= True
79 if self
._SKIP
_SOCKS
_TEST
:
82 self
.port
= random
.randint(20000, 30000)
83 self
.server_process
= subprocess
.Popen([
84 'srelay', '-f', '-i', '127.0.0.1:%d' % self
.port
],
85 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
88 if self
._SKIP
_SOCKS
_TEST
:
91 self
.server_process
.terminate()
92 self
.server_process
.communicate()
94 def _get_ip(self
, protocol
):
95 if self
._SKIP
_SOCKS
_TEST
:
99 'proxy': '%s://127.0.0.1:%d' % (protocol
, self
.port
),
101 return ydl
.urlopen('http://yt-dl.org/ip').read().decode()
103 def test_socks4(self
):
104 self
.assertTrue(isinstance(self
._get
_ip
('socks4'), str))
106 def test_socks4a(self
):
107 self
.assertTrue(isinstance(self
._get
_ip
('socks4a'), str))
109 def test_socks5(self
):
110 self
.assertTrue(isinstance(self
._get
_ip
('socks5'), str))
113 if __name__
== '__main__':