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