@@ -0,0 +1,162 @@
1+# -*- coding: utf-8 -*-
2+from collections import defaultdict
5+from searx.engines import google
6+from searx.testing import SearxTestCase
9+class TestGoogleEngine(SearxTestCase):
11+ def test_request(self):
13+ dicto = defaultdict(dict)
15+ dicto['language'] = 'fr_FR'
16+ params = google.request(query, dicto)
17+ self.assertIn('url', params)
18+ self.assertIn(query, params['url'])
19+ self.assertIn('google.com', params['url'])
20+ self.assertIn('PREF', params['cookies'])
21+ self.assertIn('fr', params['headers']['Accept-Language'])
23+ dicto['language'] = 'all'
24+ params = google.request(query, dicto)
25+ self.assertIn('en', params['headers']['Accept-Language'])
27+ def test_response(self):
28+ self.assertRaises(AttributeError, google.response, None)
29+ self.assertRaises(AttributeError, google.response, [])
30+ self.assertRaises(AttributeError, google.response, '')
31+ self.assertRaises(AttributeError, google.response, '[]')
33+ response = mock.Mock(text='<html></html>')
34+ self.assertEqual(google.response(response), [])
39+ <a href="http://this.should.be.the.link/">
40+ <b>This</b> is <b>the</b> title
44+ <div class="kv" style="margin-bottom:2px">
46+ <b>test</b>.psychologies.com/
48+ <div class="_nBb"><U+200E>
49+ <div style="display:inline" onclick="google.sham(this);" aria-expanded="false"
50+ aria-haspopup="true" tabindex="0" data-ved="0CBUQ7B0wAA">
54+ <div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1">
57+ <a class="_Zkb" href="http://www.google.fr/url?url=http://webcache.googleusercontent
58+ .com/search%3Fcache:R1Z_4pGXjuIJ:http://test.psychologies.com/">
63+ <a class="_Zkb" href="/search?safe=off&q=related:test.psy.com/">
72+ This should be the content.
75+ <div class="osl"><U+200E>
76+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/">
79+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/">
82+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/tests-couple">
86+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/tests-amour">
94+ <a href="http://www.google.com/images?q=toto">
101+ <a href="http://www.google.com/search?q=toto">
109+ <b>This</b> is <b>the</b>
115+ <a href="/url?q=url">
116+ <b>This</b> is <b>the</b>
120+ <p class="_Bmc" style="margin:3px 8px">
121+ <a href="/search?num=20&safe=off&q=t&revid=1754833769&sa=X&ei=-&ved=">
122+ suggestion <b>title</b>
126+ response = mock.Mock(text=html)
127+ results = google.response(response)
128+ self.assertEqual(type(results), list)
129+ self.assertEqual(len(results), 2)
130+ self.assertEqual(results[0]['title'], 'This is the title')
131+ self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
132+ self.assertEqual(results[0]['content'], 'This should be the content.')
133+ self.assertEqual(results[1]['suggestion'], 'suggestion title')
136+ <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
139+ response = mock.Mock(text=html)
140+ results = google.response(response)
141+ self.assertEqual(type(results), list)
142+ self.assertEqual(len(results), 0)
144+ def test_parse_images(self):
148+ <a href="http://www.google.com/url?q=http://this.is.the.url/">
149+ <img style="margin:3px 0;margin-right:6px;padding:0" height="90"
150+ src="https://this.is.the.image/image.jpg" width="60" align="middle" alt="" border="0">
155+ dom = lxml.html.fromstring(html)
156+ results = google.parse_images(dom)
157+ self.assertEqual(type(results), list)
158+ self.assertEqual(len(results), 1)
159+ self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
160+ self.assertEqual(results[0]['title'], '')
161+ self.assertEqual(results[0]['content'], '')
162+ self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')