# -*- coding: utf-8 -*- from collections import defaultdict import mock import lxml from searx.engines import google from searx.testing import SearxTestCase class TestGoogleEngine(SearxTestCase): def mock_response(self, text): response = mock.Mock(text=text, url='https://www.google.com/search?q=test&start=0&gbv=1&gws_rd=cr') response.search_params = mock.Mock() response.search_params.get = mock.Mock(return_value='www.google.com') return response def test_request(self): google.supported_languages = ['en', 'fr', 'zh-CN', 'iw'] google.language_aliases = {'he': 'iw'} query = 'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'fr-FR' dicto['time_range'] = '' params = google.request(query, dicto) self.assertIn('url', params) self.assertIn(query, params['url']) self.assertIn('google.fr', params['url']) self.assertIn('fr', params['url']) self.assertIn('fr', params['headers']['Accept-Language']) dicto['language'] = 'en-US' params = google.request(query, dicto) self.assertIn('google.com', params['url']) self.assertIn('en', params['url']) self.assertIn('en', params['headers']['Accept-Language']) dicto['language'] = 'zh' params = google.request(query, dicto) self.assertIn('google.com', params['url']) self.assertIn('zh-CN', params['url']) self.assertIn('zh-CN', params['headers']['Accept-Language']) dicto['language'] = 'he' params = google.request(query, dicto) self.assertIn('google.com', params['url']) self.assertIn('iw', params['url']) self.assertIn('iw', params['headers']['Accept-Language']) def test_response(self): self.assertRaises(AttributeError, google.response, None) self.assertRaises(AttributeError, google.response, []) self.assertRaises(AttributeError, google.response, '') self.assertRaises(AttributeError, google.response, '[]') response = self.mock_response('') self.assertEqual(google.response(response), []) html = """