]>
jfr.im git - yt-dlp.git/blob - test/test_socks.py
2 # Allow direct execution
6 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
11 from test
.helper
import (
16 from yt_dlp
.compat
import (
18 compat_urllib_request
,
23 class TestMultipleSocks(unittest
.TestCase
):
25 def _check_params(attrs
):
28 if attr
not in params
:
29 print('Missing %s. Skipping.' % attr
)
33 def test_proxy_http(self
):
34 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
38 'proxy': params
['primary_proxy']
41 ydl
.urlopen('http://yt-dl.org/ip').read().decode('utf-8'),
42 params
['primary_server_ip'])
44 def test_proxy_https(self
):
45 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
49 'proxy': params
['primary_proxy']
52 ydl
.urlopen('https://yt-dl.org/ip').read().decode('utf-8'),
53 params
['primary_server_ip'])
55 def test_secondary_proxy_http(self
):
56 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
60 req
= compat_urllib_request
.Request('http://yt-dl.org/ip')
61 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
63 ydl
.urlopen(req
).read().decode('utf-8'),
64 params
['secondary_server_ip'])
66 def test_secondary_proxy_https(self
):
67 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
71 req
= compat_urllib_request
.Request('https://yt-dl.org/ip')
72 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
74 ydl
.urlopen(req
).read().decode('utf-8'),
75 params
['secondary_server_ip'])
79 class TestSocks(unittest
.TestCase
):
80 _SKIP_SOCKS_TEST
= True
83 if self
._SKIP
_SOCKS
_TEST
:
86 self
.port
= random
.randint(20000, 30000)
87 self
.server_process
= subprocess
.Popen([
88 'srelay', '-f', '-i', '127.0.0.1:%d' % self
.port
],
89 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
92 if self
._SKIP
_SOCKS
_TEST
:
95 self
.server_process
.terminate()
96 self
.server_process
.communicate()
98 def _get_ip(self
, protocol
):
99 if self
._SKIP
_SOCKS
_TEST
:
103 'proxy': '%s://127.0.0.1:%d' % (protocol
, self
.port
),
105 return ydl
.urlopen('http://yt-dl.org/ip').read().decode('utf-8')
107 def test_socks4(self
):
108 self
.assertTrue(isinstance(self
._get
_ip
('socks4'), compat_str
))
110 def test_socks4a(self
):
111 self
.assertTrue(isinstance(self
._get
_ip
('socks4a'), compat_str
))
113 def test_socks5(self
):
114 self
.assertTrue(isinstance(self
._get
_ip
('socks5'), compat_str
))
117 if __name__
== '__main__':