1from unittest.mock import patch
2from requests.models import Response
4from searx.testing import SearxTestCase
6import searx.poolrequests
7from searx.poolrequests import get_proxy_cycles, get_proxies
10CONFIG = {'http': ['http://localhost:9090', 'http://localhost:9092'],
11 'https': ['http://localhost:9091', 'http://localhost:9093']}
14class TestProxy(SearxTestCase):
16 def test_noconfig(self):
17 cycles = get_proxy_cycles(None)
18 self.assertIsNone(cycles)
20 cycles = get_proxy_cycles(False)
21 self.assertIsNone(cycles)
23 def test_oldconfig(self):
25 'http': 'http://localhost:9090',
26 'https': 'http://localhost:9091',
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')
34 def test_one_proxy(self):
36 'http': ['http://localhost:9090'],
37 'https': ['http://localhost:9091'],
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')
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')
54 def test_getproxies_none(self):
55 self.assertIsNone(get_proxies(None))
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'
63 self.assertEqual(get_proxies(cycles), {
64 'http': 'http://localhost:9092',
65 'https': 'http://localhost:9093'
68 @patch('searx.poolrequests.get_global_proxies')
69 def test_request(self, mock_get_global_proxies):
71 url = 'http://localhost'
73 'https': 'http://localhost:1080'
76 'http': 'http://localhost:9092',
77 'https': 'http://localhost:9093'
79 mock_get_global_proxies.return_value = global_proxies
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)
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)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list