diff options
author | Alexandre Flament <alex@al-f.net> | 2020-11-16 12:44:07 +0100 |
---|---|---|
committer | Alexandre Flament <alex@al-f.net> | 2020-11-20 15:29:21 +0100 |
commit | 3786920df975b11c0feb7d8564eb19b634d32977 (patch) | |
tree | 3d0bdc1fb29ef3a5a400dd78960cc2cae22f8e1a /tests | |
parent | 2fc3b17c85512280173bb47f449cc2faa64b0501 (diff) | |
download | searxng-3786920df975b11c0feb7d8564eb19b634d32977.tar.gz searxng-3786920df975b11c0feb7d8564eb19b634d32977.zip |
[enh] Add multiple outgoing proxies
credits go to @bauruine see https://github.com/searx/searx/pull/1958
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/test_poolrequests.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/unit/test_poolrequests.py b/tests/unit/test_poolrequests.py new file mode 100644 index 000000000..b22685fd0 --- /dev/null +++ b/tests/unit/test_poolrequests.py @@ -0,0 +1,89 @@ +from unittest.mock import patch +from requests.models import Response + +from searx.testing import SearxTestCase + +import searx.poolrequests +from searx.poolrequests import get_proxy_cycles, get_proxies + + +CONFIG = {'http': ['http://localhost:9090', 'http://localhost:9092'], + 'https': ['http://localhost:9091', 'http://localhost:9093']} + + +class TestProxy(SearxTestCase): + + def test_noconfig(self): + cycles = get_proxy_cycles(None) + self.assertIsNone(cycles) + + cycles = get_proxy_cycles(False) + self.assertIsNone(cycles) + + def test_oldconfig(self): + config = { + 'http': 'http://localhost:9090', + 'https': 'http://localhost:9091', + } + cycles = get_proxy_cycles(config) + self.assertEqual(next(cycles['http']), 'http://localhost:9090') + self.assertEqual(next(cycles['http']), 'http://localhost:9090') + self.assertEqual(next(cycles['https']), 'http://localhost:9091') + self.assertEqual(next(cycles['https']), 'http://localhost:9091') + + def test_one_proxy(self): + config = { + 'http': ['http://localhost:9090'], + 'https': ['http://localhost:9091'], + } + cycles = get_proxy_cycles(config) + self.assertEqual(next(cycles['http']), 'http://localhost:9090') + self.assertEqual(next(cycles['http']), 'http://localhost:9090') + self.assertEqual(next(cycles['https']), 'http://localhost:9091') + self.assertEqual(next(cycles['https']), 'http://localhost:9091') + + def test_multiple_proxies(self): + cycles = get_proxy_cycles(CONFIG) + self.assertEqual(next(cycles['http']), 'http://localhost:9090') + self.assertEqual(next(cycles['http']), 'http://localhost:9092') + self.assertEqual(next(cycles['http']), 'http://localhost:9090') + self.assertEqual(next(cycles['https']), 'http://localhost:9091') + self.assertEqual(next(cycles['https']), 'http://localhost:9093') + self.assertEqual(next(cycles['https']), 'http://localhost:9091') + + def test_getproxies_none(self): + self.assertIsNone(get_proxies(None)) + + def test_getproxies_config(self): + cycles = get_proxy_cycles(CONFIG) + self.assertEqual(get_proxies(cycles), { + 'http': 'http://localhost:9090', + 'https': 'http://localhost:9091' + }) + self.assertEqual(get_proxies(cycles), { + 'http': 'http://localhost:9092', + 'https': 'http://localhost:9093' + }) + + @patch('searx.poolrequests.get_global_proxies') + def test_request(self, mock_get_global_proxies): + method = 'GET' + url = 'http://localhost' + custom_proxies = { + 'https': 'http://localhost:1080' + } + global_proxies = { + 'http': 'http://localhost:9092', + 'https': 'http://localhost:9093' + } + mock_get_global_proxies.return_value = global_proxies + + # check the global proxies usage + with patch.object(searx.poolrequests.SessionSinglePool, 'request', return_value=Response()) as mock_method: + searx.poolrequests.request(method, url) + mock_method.assert_called_once_with(method=method, url=url, proxies=global_proxies) + + # check if the proxies parameter overrides the global proxies + with patch.object(searx.poolrequests.SessionSinglePool, 'request', return_value=Response()) as mock_method: + searx.poolrequests.request(method, url, proxies=custom_proxies) + mock_method.assert_called_once_with(method=method, url=url, proxies=custom_proxies) |