]>
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 (
18 from youtube_dl
.compat
import (
20 compat_urllib_request
,
24 class TestMultipleSocks(unittest
.TestCase
):
26 def _check_params(attrs
):
29 if attr
not in params
:
30 print('Missing %s. Skipping.' % attr
)
34 def test_proxy_http(self
):
35 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
39 'proxy': params
['primary_proxy']
42 ydl
.urlopen('http://yt-dl.org/ip').read().decode('utf-8'),
43 params
['primary_server_ip'])
45 def test_proxy_https(self
):
46 params
= self
._check
_params
(['primary_proxy', 'primary_server_ip'])
50 'proxy': params
['primary_proxy']
53 ydl
.urlopen('https://yt-dl.org/ip').read().decode('utf-8'),
54 params
['primary_server_ip'])
56 def test_secondary_proxy_http(self
):
57 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
61 req
= compat_urllib_request
.Request('http://yt-dl.org/ip')
62 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
64 ydl
.urlopen(req
).read().decode('utf-8'),
65 params
['secondary_server_ip'])
67 def test_secondary_proxy_https(self
):
68 params
= self
._check
_params
(['secondary_proxy', 'secondary_server_ip'])
72 req
= compat_urllib_request
.Request('https://yt-dl.org/ip')
73 req
.add_header('Ytdl-request-proxy', params
['secondary_proxy'])
75 ydl
.urlopen(req
).read().decode('utf-8'),
76 params
['secondary_server_ip'])
79 class TestSocks(unittest
.TestCase
):
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 self
.server_process
.terminate()
88 self
.server_process
.communicate()
90 def _get_ip(self
, protocol
):
92 'proxy': '%s://127.0.0.1:%d' % (protocol
, self
.port
),
94 return ydl
.urlopen('http://yt-dl.org/ip').read().decode('utf-8')
96 def test_socks4(self
):
97 self
.assertTrue(isinstance(self
._get
_ip
('socks4'), compat_str
))
99 def test_socks4a(self
):
100 self
.assertTrue(isinstance(self
._get
_ip
('socks4a'), compat_str
))
102 def test_socks5(self
):
103 self
.assertTrue(isinstance(self
._get
_ip
('socks5'), compat_str
))
106 if __name__
== '__main__':