|
@@ -0,0 +1,162 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+from collections import defaultdict
|
|
3
|
+import mock
|
|
4
|
+import lxml
|
|
5
|
+from searx.engines import google
|
|
6
|
+from searx.testing import SearxTestCase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+class TestGoogleEngine(SearxTestCase):
|
|
10
|
+
|
|
11
|
+ def test_request(self):
|
|
12
|
+ query = 'test_query'
|
|
13
|
+ dicto = defaultdict(dict)
|
|
14
|
+ dicto['pageno'] = 1
|
|
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'])
|
|
22
|
+
|
|
23
|
+ dicto['language'] = 'all'
|
|
24
|
+ params = google.request(query, dicto)
|
|
25
|
+ self.assertIn('en', params['headers']['Accept-Language'])
|
|
26
|
+
|
|
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, '[]')
|
|
32
|
+
|
|
33
|
+ response = mock.Mock(text='<html></html>')
|
|
34
|
+ self.assertEqual(google.response(response), [])
|
|
35
|
+
|
|
36
|
+ html = """
|
|
37
|
+ <li class="g">
|
|
38
|
+ <h3 class="r">
|
|
39
|
+ <a href="http://this.should.be.the.link/">
|
|
40
|
+ <b>This</b> is <b>the</b> title
|
|
41
|
+ </a>
|
|
42
|
+ </h3>
|
|
43
|
+ <div class="s">
|
|
44
|
+ <div class="kv" style="margin-bottom:2px">
|
|
45
|
+ <cite>
|
|
46
|
+ <b>test</b>.psychologies.com/
|
|
47
|
+ </cite>
|
|
48
|
+ <div class="_nBb">
|
|
49
|
+ <div style="display:inline" onclick="google.sham(this);" aria-expanded="false"
|
|
50
|
+ aria-haspopup="true" tabindex="0" data-ved="0CBUQ7B0wAA">
|
|
51
|
+ <span class="_O0">
|
|
52
|
+ </span>
|
|
53
|
+ </div>
|
|
54
|
+ <div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1">
|
|
55
|
+ <ul>
|
|
56
|
+ <li class="_Ykb">
|
|
57
|
+ <a class="_Zkb" href="http://www.google.fr/url?url=http://webcache.googleusercontent
|
|
58
|
+ .com/search%3Fcache:R1Z_4pGXjuIJ:http://test.psychologies.com/">
|
|
59
|
+ En cache
|
|
60
|
+ </a>
|
|
61
|
+ </li>
|
|
62
|
+ <li class="_Ykb">
|
|
63
|
+ <a class="_Zkb" href="/search?safe=off&q=related:test.psy.com/">
|
|
64
|
+ Pages similaires
|
|
65
|
+ </a>
|
|
66
|
+ </li>
|
|
67
|
+ </ul>
|
|
68
|
+ </div>
|
|
69
|
+ </div>
|
|
70
|
+ </div>
|
|
71
|
+ <span class="st">
|
|
72
|
+ This should be the content.
|
|
73
|
+ </span>
|
|
74
|
+ <br>
|
|
75
|
+ <div class="osl">
|
|
76
|
+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/">
|
|
77
|
+ Test Personnalité
|
|
78
|
+ </a> -
|
|
79
|
+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/">
|
|
80
|
+ Tests - Moi
|
|
81
|
+ </a> -
|
|
82
|
+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/tests-couple">
|
|
83
|
+ Test Couple
|
|
84
|
+ </a>
|
|
85
|
+ -
|
|
86
|
+ <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/tests-amour">
|
|
87
|
+ Test Amour
|
|
88
|
+ </a>
|
|
89
|
+ </div>
|
|
90
|
+ </div>
|
|
91
|
+ </li>
|
|
92
|
+ <li class="g">
|
|
93
|
+ <h3 class="r">
|
|
94
|
+ <a href="http://www.google.com/images?q=toto">
|
|
95
|
+ <b>This</b>
|
|
96
|
+ </a>
|
|
97
|
+ </h3>
|
|
98
|
+ </li>
|
|
99
|
+ <li class="g">
|
|
100
|
+ <h3 class="r">
|
|
101
|
+ <a href="http://www.google.com/search?q=toto">
|
|
102
|
+ <b>This</b> is
|
|
103
|
+ </a>
|
|
104
|
+ </h3>
|
|
105
|
+ </li>
|
|
106
|
+ <li class="g">
|
|
107
|
+ <h3 class="r">
|
|
108
|
+ <a href="€">
|
|
109
|
+ <b>This</b> is <b>the</b>
|
|
110
|
+ </a>
|
|
111
|
+ </h3>
|
|
112
|
+ </li>
|
|
113
|
+ <li class="g">
|
|
114
|
+ <h3 class="r">
|
|
115
|
+ <a href="/url?q=url">
|
|
116
|
+ <b>This</b> is <b>the</b>
|
|
117
|
+ </a>
|
|
118
|
+ </h3>
|
|
119
|
+ </li>
|
|
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>
|
|
123
|
+ </a>
|
|
124
|
+ </p>
|
|
125
|
+ """
|
|
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')
|
|
134
|
+
|
|
135
|
+ html = """
|
|
136
|
+ <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
|
|
137
|
+ </li>
|
|
138
|
+ """
|
|
139
|
+ response = mock.Mock(text=html)
|
|
140
|
+ results = google.response(response)
|
|
141
|
+ self.assertEqual(type(results), list)
|
|
142
|
+ self.assertEqual(len(results), 0)
|
|
143
|
+
|
|
144
|
+ def test_parse_images(self):
|
|
145
|
+ html = """
|
|
146
|
+ <li>
|
|
147
|
+ <div>
|
|
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">
|
|
151
|
+ </a>
|
|
152
|
+ </div>
|
|
153
|
+ </li>
|
|
154
|
+ """
|
|
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')
|