You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.2 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import sys
  7. import unittest
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. import random
  10. import subprocess
  11. from test.helper import (
  12. FakeYDL,
  13. get_params,
  14. )
  15. from youtube_dl.compat import (
  16. compat_str,
  17. compat_urllib_request,
  18. )
  19. class TestMultipleSocks(unittest.TestCase):
  20. @staticmethod
  21. def _check_params(attrs):
  22. params = get_params()
  23. for attr in attrs:
  24. if attr not in params:
  25. print('Missing %s. Skipping.' % attr)
  26. return
  27. return params
  28. def test_proxy_http(self):
  29. params = self._check_params(['primary_proxy', 'primary_server_ip'])
  30. if params is None:
  31. return
  32. ydl = FakeYDL({
  33. 'proxy': params['primary_proxy']
  34. })
  35. self.assertEqual(
  36. ydl.urlopen('http://yt-dl.org/ip').read().decode('utf-8'),
  37. params['primary_server_ip'])
  38. def test_proxy_https(self):
  39. params = self._check_params(['primary_proxy', 'primary_server_ip'])
  40. if params is None:
  41. return
  42. ydl = FakeYDL({
  43. 'proxy': params['primary_proxy']
  44. })
  45. self.assertEqual(
  46. ydl.urlopen('https://yt-dl.org/ip').read().decode('utf-8'),
  47. params['primary_server_ip'])
  48. def test_secondary_proxy_http(self):
  49. params = self._check_params(['secondary_proxy', 'secondary_server_ip'])
  50. if params is None:
  51. return
  52. ydl = FakeYDL()
  53. req = compat_urllib_request.Request('http://yt-dl.org/ip')
  54. req.add_header('Ytdl-request-proxy', params['secondary_proxy'])
  55. self.assertEqual(
  56. ydl.urlopen(req).read().decode('utf-8'),
  57. params['secondary_server_ip'])
  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. class TestSocks(unittest.TestCase):
  69. def setUp(self):
  70. self.port = random.randint(20000, 30000)
  71. self.server_process = subprocess.Popen([
  72. 'srelay', '-f', '-i', '127.0.0.1:%d' % self.port],
  73. stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  74. def tearDown(self):
  75. self.server_process.terminate()
  76. self.server_process.communicate()
  77. def _get_ip(self, protocol):
  78. ydl = FakeYDL({
  79. 'proxy': '%s://127.0.0.1:%d' % (protocol, self.port),
  80. })
  81. return ydl.urlopen('http://yt-dl.org/ip').read().decode('utf-8')
  82. def test_socks4(self):
  83. self.assertTrue(isinstance(self._get_ip('socks4'), compat_str))
  84. def test_socks4a(self):
  85. self.assertTrue(isinstance(self._get_ip('socks4a'), compat_str))
  86. def test_socks5(self):
  87. self.assertTrue(isinstance(self._get_ip('socks5'), compat_str))
  88. if __name__ == '__main__':
  89. unittest.main()