2015-02-11 17:16:52 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from collections import defaultdict
|
|
|
|
import mock
|
|
|
|
import lxml
|
|
|
|
from searx.engines import google
|
|
|
|
from searx.testing import SearxTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class TestGoogleEngine(SearxTestCase):
|
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
def mock_response(self, text):
|
2015-12-22 20:05:42 +01:00
|
|
|
response = mock.Mock(text=text, url='https://www.google.com/search?q=test&start=0&gbv=1&gws_rd=cr')
|
2015-05-30 17:41:40 +02:00
|
|
|
response.search_params = mock.Mock()
|
|
|
|
response.search_params.get = mock.Mock(return_value='www.google.com')
|
|
|
|
return response
|
|
|
|
|
2015-02-11 17:16:52 +01:00
|
|
|
def test_request(self):
|
2018-11-26 06:32:48 +01:00
|
|
|
google.supported_languages = ['en', 'fr', 'zh-CN', 'iw']
|
|
|
|
google.language_aliases = {'he': 'iw'}
|
2018-03-01 05:30:48 +01:00
|
|
|
|
2015-02-11 17:16:52 +01:00
|
|
|
query = 'test_query'
|
|
|
|
dicto = defaultdict(dict)
|
|
|
|
dicto['pageno'] = 1
|
2016-10-30 03:04:01 +01:00
|
|
|
dicto['language'] = 'fr-FR'
|
2016-07-26 00:22:05 +02:00
|
|
|
dicto['time_range'] = ''
|
2015-02-11 17:16:52 +01:00
|
|
|
params = google.request(query, dicto)
|
|
|
|
self.assertIn('url', params)
|
|
|
|
self.assertIn(query, params['url'])
|
2015-05-30 17:41:40 +02:00
|
|
|
self.assertIn('google.fr', params['url'])
|
2018-04-19 05:55:37 +02:00
|
|
|
self.assertIn('fr', params['url'])
|
2015-02-11 17:16:52 +01:00
|
|
|
self.assertIn('fr', params['headers']['Accept-Language'])
|
|
|
|
|
2017-07-20 22:47:20 +02:00
|
|
|
dicto['language'] = 'en-US'
|
2015-02-11 17:16:52 +01:00
|
|
|
params = google.request(query, dicto)
|
2018-04-19 05:55:37 +02:00
|
|
|
self.assertIn('google.com', params['url'])
|
|
|
|
self.assertIn('en', params['url'])
|
2015-02-11 17:16:52 +01:00
|
|
|
self.assertIn('en', params['headers']['Accept-Language'])
|
|
|
|
|
2018-03-01 05:30:48 +01:00
|
|
|
dicto['language'] = 'zh'
|
|
|
|
params = google.request(query, dicto)
|
|
|
|
self.assertIn('google.com', params['url'])
|
2018-04-19 05:55:37 +02:00
|
|
|
self.assertIn('zh-CN', params['url'])
|
2018-03-01 05:30:48 +01:00
|
|
|
self.assertIn('zh-CN', params['headers']['Accept-Language'])
|
|
|
|
|
2018-11-26 06:32:48 +01:00
|
|
|
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'])
|
|
|
|
|
2015-02-11 17:16:52 +01:00
|
|
|
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, '[]')
|
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
response = self.mock_response('<html></html>')
|
2015-02-11 17:16:52 +01:00
|
|
|
self.assertEqual(google.response(response), [])
|
|
|
|
|
|
|
|
html = """
|
2019-06-26 07:45:20 +02:00
|
|
|
<div class="ZINbbc xpd O9g5cc uUPGi">
|
|
|
|
<div>
|
|
|
|
<div class="kCrYT">
|
|
|
|
<a href="/url?q=http://this.should.be.the.link/">
|
|
|
|
<div class="BNeawe">
|
|
|
|
<b>This</b> is <b>the</b> title
|
2015-02-11 17:16:52 +01:00
|
|
|
</div>
|
2019-06-26 07:45:20 +02:00
|
|
|
<div class="BNeawe">
|
|
|
|
http://website
|
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
<div class="kCrYT">
|
|
|
|
<div>
|
|
|
|
<div class="BNeawe">
|
|
|
|
<div>
|
|
|
|
<div class="BNeawe">
|
|
|
|
This should be the content.
|
|
|
|
</div>
|
|
|
|
</div>
|
2015-02-11 17:16:52 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-06-26 07:45:20 +02:00
|
|
|
</div>
|
|
|
|
</p>
|
|
|
|
<div class="ZINbbc xpd O9g5cc uUPGi">
|
|
|
|
<div>
|
|
|
|
<div class="kCrYT">
|
|
|
|
<span>
|
|
|
|
<div class="BNeawe">
|
|
|
|
Related searches
|
|
|
|
</div>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div class="rVLSBd">
|
|
|
|
<a>
|
|
|
|
<div>
|
|
|
|
<div class="BNeawe">
|
|
|
|
suggestion title
|
|
|
|
</div>
|
|
|
|
</div>
|
2015-02-11 17:16:52 +01:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</p>
|
|
|
|
"""
|
2015-05-30 17:41:40 +02:00
|
|
|
response = self.mock_response(html)
|
2015-02-11 17:16:52 +01:00
|
|
|
results = google.response(response)
|
|
|
|
self.assertEqual(type(results), list)
|
|
|
|
self.assertEqual(len(results), 2)
|
|
|
|
self.assertEqual(results[0]['title'], 'This is the title')
|
|
|
|
self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
|
|
|
|
self.assertEqual(results[0]['content'], 'This should be the content.')
|
|
|
|
self.assertEqual(results[1]['suggestion'], 'suggestion title')
|
|
|
|
|
|
|
|
html = """
|
|
|
|
<li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
|
|
|
|
</li>
|
|
|
|
"""
|
2015-05-30 17:41:40 +02:00
|
|
|
response = self.mock_response(html)
|
2015-02-11 17:16:52 +01:00
|
|
|
results = google.response(response)
|
|
|
|
self.assertEqual(type(results), list)
|
|
|
|
self.assertEqual(len(results), 0)
|
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
response = mock.Mock(text='<html></html>', url='https://sorry.google.com')
|
|
|
|
response.search_params = mock.Mock()
|
|
|
|
response.search_params.get = mock.Mock(return_value='www.google.com')
|
|
|
|
self.assertRaises(RuntimeWarning, google.response, response)
|
|
|
|
|
|
|
|
response = mock.Mock(text='<html></html>', url='https://www.google.com/sorry/IndexRedirect')
|
|
|
|
response.search_params = mock.Mock()
|
|
|
|
response.search_params.get = mock.Mock(return_value='www.google.com')
|
|
|
|
self.assertRaises(RuntimeWarning, google.response, response)
|
|
|
|
|
2015-02-11 17:16:52 +01:00
|
|
|
def test_parse_images(self):
|
|
|
|
html = """
|
|
|
|
<li>
|
|
|
|
<div>
|
|
|
|
<a href="http://www.google.com/url?q=http://this.is.the.url/">
|
|
|
|
<img style="margin:3px 0;margin-right:6px;padding:0" height="90"
|
|
|
|
src="https://this.is.the.image/image.jpg" width="60" align="middle" alt="" border="0">
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
"""
|
|
|
|
dom = lxml.html.fromstring(html)
|
2015-05-30 17:41:40 +02:00
|
|
|
results = google.parse_images(dom, 'www.google.com')
|
2015-02-11 17:16:52 +01:00
|
|
|
self.assertEqual(type(results), list)
|
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
|
|
|
|
self.assertEqual(results[0]['title'], '')
|
|
|
|
self.assertEqual(results[0]['content'], '')
|
|
|
|
self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')
|
2016-12-15 07:34:43 +01:00
|
|
|
|
|
|
|
def test_fetch_supported_languages(self):
|
|
|
|
html = """<html></html>"""
|
|
|
|
response = mock.Mock(text=html)
|
|
|
|
languages = google._fetch_supported_languages(response)
|
|
|
|
self.assertEqual(type(languages), dict)
|
|
|
|
self.assertEqual(len(languages), 0)
|
|
|
|
|
|
|
|
html = u"""
|
|
|
|
<html>
|
|
|
|
<body>
|
2019-01-07 04:31:45 +01:00
|
|
|
<div id="langSec">
|
|
|
|
<div>
|
|
|
|
<input name="lr" data-name="english" value="lang_en" />
|
|
|
|
<input name="lr" data-name="中文 (简体)" value="lang_zh-CN" />
|
|
|
|
<input name="lr" data-name="中文 (繁體)" value="lang_zh-TW" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2016-12-15 07:34:43 +01:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
response = mock.Mock(text=html)
|
|
|
|
languages = google._fetch_supported_languages(response)
|
|
|
|
self.assertEqual(type(languages), dict)
|
|
|
|
self.assertEqual(len(languages), 3)
|
|
|
|
|
|
|
|
self.assertIn('en', languages)
|
|
|
|
self.assertIn('zh-CN', languages)
|
|
|
|
self.assertIn('zh-TW', languages)
|
|
|
|
|
|
|
|
self.assertEquals(type(languages['en']), dict)
|
|
|
|
self.assertEquals(type(languages['zh-CN']), dict)
|
|
|
|
self.assertEquals(type(languages['zh-TW']), dict)
|
|
|
|
|
|
|
|
self.assertIn('name', languages['en'])
|
|
|
|
self.assertIn('name', languages['zh-CN'])
|
|
|
|
self.assertIn('name', languages['zh-TW'])
|
|
|
|
|
|
|
|
self.assertEquals(languages['en']['name'], 'English')
|
|
|
|
self.assertEquals(languages['zh-CN']['name'], u'中文 (简体)')
|
|
|
|
self.assertEquals(languages['zh-TW']['name'], u'中文 (繁體)')
|