Skip to content

Commit 72f3289

Browse files
committed
[test/test_socks] Add tests for SOCKS proxies
1 parent 71aff18 commit 72f3289

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ updates_key.pem
3131
*.part
3232
*.swp
3333
test/testdata
34+
test/local_parameters.json
3435
.tox
3536
youtube-dl.zsh
3637
.idea

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test:
3737
ot: offlinetest
3838

3939
offlinetest: codetest
40-
$(PYTHON) -m nose --verbose test --exclude test_download.py --exclude test_age_restriction.py --exclude test_subtitles.py --exclude test_write_annotations.py --exclude test_youtube_lists.py --exclude test_iqiyi_sdk_interpreter.py
40+
$(PYTHON) -m nose --verbose test --exclude test_download.py --exclude test_age_restriction.py --exclude test_subtitles.py --exclude test_write_annotations.py --exclude test_youtube_lists.py --exclude test_iqiyi_sdk_interpreter.py --exclude test_socks.py
4141

4242
tar: youtube-dl.tar.gz
4343

test/helper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@
2424
def get_params(override=None):
2525
PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
2626
"parameters.json")
27+
LOCAL_PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
28+
"local_parameters.json")
2729
with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
2830
parameters = json.load(pf)
31+
if os.path.exists(LOCAL_PARAMETERS_FILE):
32+
with io.open(LOCAL_PARAMETERS_FILE, encoding='utf-8') as pf:
33+
parameters.update(json.load(pf))
2934
if override:
3035
parameters.update(override)
3136
return parameters

test/test_socks.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
from __future__ import unicode_literals
4+
5+
# Allow direct execution
6+
import os
7+
import sys
8+
import unittest
9+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10+
11+
from test.helper import (FakeYDL, get_params)
12+
from youtube_dl.compat import compat_urllib_request
13+
14+
15+
class TestSocks(unittest.TestCase):
16+
@staticmethod
17+
def _check_params(attrs):
18+
params = get_params()
19+
for attr in attrs:
20+
if attr not in params:
21+
print('Missing %s. Skipping.' % attr)
22+
return
23+
return params
24+
25+
def test_proxy_http(self):
26+
params = self._check_params(['primary_proxy', 'primary_server_ip'])
27+
if params is None:
28+
return
29+
ydl = FakeYDL({
30+
'proxy': params['primary_proxy']
31+
})
32+
self.assertEqual(
33+
ydl.urlopen('http://yt-dl.org/ip').read().decode('utf-8'),
34+
params['primary_server_ip'])
35+
36+
def test_proxy_https(self):
37+
params = self._check_params(['primary_proxy', 'primary_server_ip'])
38+
if params is None:
39+
return
40+
ydl = FakeYDL({
41+
'proxy': params['primary_proxy']
42+
})
43+
self.assertEqual(
44+
ydl.urlopen('https://yt-dl.org/ip').read().decode('utf-8'),
45+
params['primary_server_ip'])
46+
47+
def test_secondary_proxy_http(self):
48+
params = self._check_params(['secondary_proxy', 'secondary_server_ip'])
49+
if params is None:
50+
return
51+
ydl = FakeYDL()
52+
req = compat_urllib_request.Request('http://yt-dl.org/ip')
53+
req.add_header('Ytdl-request-proxy', params['secondary_proxy'])
54+
self.assertEqual(
55+
ydl.urlopen(req).read().decode('utf-8'),
56+
params['secondary_server_ip'])
57+
58+
def test_secondary_proxy_https(self):
59+
params = self._check_params(['secondary_proxy', 'secondary_server_ip'])
60+
if params is None:
61+
return
62+
ydl = FakeYDL()
63+
req = compat_urllib_request.Request('https://yt-dl.org/ip')
64+
req.add_header('Ytdl-request-proxy', params['secondary_proxy'])
65+
self.assertEqual(
66+
ydl.urlopen(req).read().decode('utf-8'),
67+
params['secondary_server_ip'])
68+
69+
70+
if __name__ == '__main__':
71+
unittest.main()

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ passenv = HOME
99
defaultargs = test --exclude test_download.py --exclude test_age_restriction.py
1010
--exclude test_subtitles.py --exclude test_write_annotations.py
1111
--exclude test_youtube_lists.py --exclude test_iqiyi_sdk_interpreter.py
12+
--exclude test_socks.py
1213
commands = nosetests --verbose {posargs:{[testenv]defaultargs}} # --with-coverage --cover-package=youtube_dl --cover-html
1314
# test.test_download:TestDownload.test_NowVideo

0 commit comments

Comments
 (0)