]>
jfr.im git - yt-dlp.git/blob - test/test_socks.py
3 from __future__
import unicode_literals
5 # Allow direct execution
9 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
14 from test
.helper
import (
19 from yt_dlp
.compat
import (
21 compat_urllib_request
,
26 class TestMultipleSocks(unittest
.TestCase
):
28 def _check_params(attrs
):
31 if attr
not in params
:
32 print('Missing %s. Skipping.' % attr
)
36 def test_proxy_http(self
):
37 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
41 'proxy': params
['primary_proxy']
44 ydl
.urlopen('http://yt-dl.org/ip').read().decode('utf-8'),
45 params
['primary_server_ip'])
47 def test_proxy_https(self
):
48 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
52 'proxy': params
['primary_proxy']
55 ydl
.urlopen('https://yt-dl.org/ip').read().decode('utf-8'),
56 params
['primary_server_ip'])
58 def test_secondary_proxy_http(self
):
59 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
63 req
= compat_urllib_request
.Request('http://yt-dl.org/ip')
64 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
66 ydl
.urlopen(req
).read().decode('utf-8'),
67 params
['secondary_server_ip'])
69 def test_secondary_proxy_https(self
):
70 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
74 req
= compat_urllib_request
.Request('https://yt-dl.org/ip')
75 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
77 ydl
.urlopen(req
).read().decode('utf-8'),
78 params
['secondary_server_ip'])
82 class TestSocks(unittest
.TestCase
):
83 _SKIP_SOCKS_TEST
= True
86 if self
._SKIP
_SOCKS
_TEST
:
89 self
.port
= random
.randint(20000, 30000)
90 self
.server_process
= subprocess
.Popen([
91 'srelay', '-f', '-i', '127.0.0.1:%d' % self
.port
],
92 stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
95 if self
._SKIP
_SOCKS
_TEST
:
98 self
.server_process
.terminate()
99 self
.server_process
.communicate()
101 def _get_ip(self
, protocol
):
102 if self
._SKIP
_SOCKS
_TEST
:
106 'proxy': '%s://127.0.0.1:%d' % (protocol
, self
.port
),
108 return ydl
.urlopen('http://yt-dl.org/ip').read().decode('utf-8')
110 def test_socks4(self
):
111 self
.assertTrue(isinstance(self
._get
_ip
('socks4'), compat_str
))
113 def test_socks4a(self
):
114 self
.assertTrue(isinstance(self
._get
_ip
('socks4a'), compat_str
))
116 def test_socks5(self
):
117 self
.assertTrue(isinstance(self
._get
_ip
('socks5'), compat_str
))
120 if __name__
== '__main__':