test_poolrequests.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from unittest.mock import patch
  2. from requests.models import Response
  3. from searx.testing import SearxTestCase
  4. import searx.poolrequests
  5. from searx.poolrequests import get_proxy_cycles, get_proxies
  6. CONFIG = {'http': ['http://localhost:9090', 'http://localhost:9092'],
  7. 'https': ['http://localhost:9091', 'http://localhost:9093']}
  8. class TestProxy(SearxTestCase):
  9. def test_noconfig(self):
  10. cycles = get_proxy_cycles(None)
  11. self.assertIsNone(cycles)
  12. cycles = get_proxy_cycles(False)
  13. self.assertIsNone(cycles)
  14. def test_oldconfig(self):
  15. config = {
  16. 'http': 'http://localhost:9090',
  17. 'https': 'http://localhost:9091',
  18. }
  19. cycles = get_proxy_cycles(config)
  20. self.assertEqual(next(cycles['http']), 'http://localhost:9090')
  21. self.assertEqual(next(cycles['http']), 'http://localhost:9090')
  22. self.assertEqual(next(cycles['https']), 'http://localhost:9091')
  23. self.assertEqual(next(cycles['https']), 'http://localhost:9091')
  24. def test_one_proxy(self):
  25. config = {
  26. 'http': ['http://localhost:9090'],
  27. 'https': ['http://localhost:9091'],
  28. }
  29. cycles = get_proxy_cycles(config)
  30. self.assertEqual(next(cycles['http']), 'http://localhost:9090')
  31. self.assertEqual(next(cycles['http']), 'http://localhost:9090')
  32. self.assertEqual(next(cycles['https']), 'http://localhost:9091')
  33. self.assertEqual(next(cycles['https']), 'http://localhost:9091')
  34. def test_multiple_proxies(self):
  35. cycles = get_proxy_cycles(CONFIG)
  36. self.assertEqual(next(cycles['http']), 'http://localhost:9090')
  37. self.assertEqual(next(cycles['http']), 'http://localhost:9092')
  38. self.assertEqual(next(cycles['http']), 'http://localhost:9090')
  39. self.assertEqual(next(cycles['https']), 'http://localhost:9091')
  40. self.assertEqual(next(cycles['https']), 'http://localhost:9093')
  41. self.assertEqual(next(cycles['https']), 'http://localhost:9091')
  42. def test_getproxies_none(self):
  43. self.assertIsNone(get_proxies(None))
  44. def test_getproxies_config(self):
  45. cycles = get_proxy_cycles(CONFIG)
  46. self.assertEqual(get_proxies(cycles), {
  47. 'http': 'http://localhost:9090',
  48. 'https': 'http://localhost:9091'
  49. })
  50. self.assertEqual(get_proxies(cycles), {
  51. 'http': 'http://localhost:9092',
  52. 'https': 'http://localhost:9093'
  53. })
  54. @patch('searx.poolrequests.get_global_proxies')
  55. def test_request(self, mock_get_global_proxies):
  56. method = 'GET'
  57. url = 'http://localhost'
  58. custom_proxies = {
  59. 'https': 'http://localhost:1080'
  60. }
  61. global_proxies = {
  62. 'http': 'http://localhost:9092',
  63. 'https': 'http://localhost:9093'
  64. }
  65. mock_get_global_proxies.return_value = global_proxies
  66. # check the global proxies usage
  67. with patch.object(searx.poolrequests.SessionSinglePool, 'request', return_value=Response()) as mock_method:
  68. searx.poolrequests.request(method, url)
  69. mock_method.assert_called_once_with(method=method, url=url, proxies=global_proxies)
  70. # check if the proxies parameter overrides the global proxies
  71. with patch.object(searx.poolrequests.SessionSinglePool, 'request', return_value=Response()) as mock_method:
  72. searx.poolrequests.request(method, url, proxies=custom_proxies)
  73. mock_method.assert_called_once_with(method=method, url=url, proxies=custom_proxies)