This is a backup of https://github.com/ytdl-org/youtube-dl since it was taken down by the RIAA.
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.

test_socks.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. from test.helper import (FakeYDL, get_params)
  10. from youtube_dl.compat import compat_urllib_request
  11. class TestSocks(unittest.TestCase):
  12. @staticmethod
  13. def _check_params(attrs):
  14. params = get_params()
  15. for attr in attrs:
  16. if attr not in params:
  17. print('Missing %s. Skipping.' % attr)
  18. return
  19. return params
  20. def test_proxy_http(self):
  21. params = self._check_params(['primary_proxy', 'primary_server_ip'])
  22. if params is None:
  23. return
  24. ydl = FakeYDL({
  25. 'proxy': params['primary_proxy']
  26. })
  27. self.assertEqual(
  28. ydl.urlopen('http://yt-dl.org/ip').read().decode('utf-8'),
  29. params['primary_server_ip'])
  30. def test_proxy_https(self):
  31. params = self._check_params(['primary_proxy', 'primary_server_ip'])
  32. if params is None:
  33. return
  34. ydl = FakeYDL({
  35. 'proxy': params['primary_proxy']
  36. })
  37. self.assertEqual(
  38. ydl.urlopen('https://yt-dl.org/ip').read().decode('utf-8'),
  39. params['primary_server_ip'])
  40. def test_secondary_proxy_http(self):
  41. params = self._check_params(['secondary_proxy', 'secondary_server_ip'])
  42. if params is None:
  43. return
  44. ydl = FakeYDL()
  45. req = compat_urllib_request.Request('http://yt-dl.org/ip')
  46. req.add_header('Ytdl-request-proxy', params['secondary_proxy'])
  47. self.assertEqual(
  48. ydl.urlopen(req).read().decode('utf-8'),
  49. params['secondary_server_ip'])
  50. def test_secondary_proxy_https(self):
  51. params = self._check_params(['secondary_proxy', 'secondary_server_ip'])
  52. if params is None:
  53. return
  54. ydl = FakeYDL()
  55. req = compat_urllib_request.Request('https://yt-dl.org/ip')
  56. req.add_header('Ytdl-request-proxy', params['secondary_proxy'])
  57. self.assertEqual(
  58. ydl.urlopen(req).read().decode('utf-8'),
  59. params['secondary_server_ip'])
  60. if __name__ == '__main__':
  61. unittest.main()