test_google.py 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. class TestGoogleEngine(SearxTestCase):
  8. def mock_response(self, text):
  9. response = mock.Mock(text=text, url='https://www.google.com/search?q=test&start=0&gbv=1&gws_rd=cr')
  10. response.search_params = mock.Mock()
  11. response.search_params.get = mock.Mock(return_value='www.google.com')
  12. return response
  13. def test_request(self):
  14. google.supported_languages = ['en', 'fr', 'zh-CN']
  15. query = 'test_query'
  16. dicto = defaultdict(dict)
  17. dicto['pageno'] = 1
  18. dicto['language'] = 'fr-FR'
  19. dicto['time_range'] = ''
  20. params = google.request(query, dicto)
  21. self.assertIn('url', params)
  22. self.assertIn(query, params['url'])
  23. self.assertIn('google.fr', params['url'])
  24. self.assertIn('fr', params['headers']['Accept-Language'])
  25. dicto['language'] = 'en-US'
  26. params = google.request(query, dicto)
  27. self.assertIn('google.co', params['url'])
  28. self.assertIn('en', params['headers']['Accept-Language'])
  29. dicto['language'] = 'zh'
  30. params = google.request(query, dicto)
  31. self.assertIn('google.com', params['url'])
  32. self.assertIn('zh-CN', params['headers']['Accept-Language'])
  33. def test_response(self):
  34. self.assertRaises(AttributeError, google.response, None)
  35. self.assertRaises(AttributeError, google.response, [])
  36. self.assertRaises(AttributeError, google.response, '')
  37. self.assertRaises(AttributeError, google.response, '[]')
  38. response = self.mock_response('<html></html>')
  39. self.assertEqual(google.response(response), [])
  40. html = """
  41. <div class="g">
  42. <h3 class="r">
  43. <a href="http://this.should.be.the.link/">
  44. <b>This</b> is <b>the</b> title
  45. </a>
  46. </h3>
  47. <div class="s">
  48. <div class="kv" style="margin-bottom:2px">
  49. <cite>
  50. <b>test</b>.psychologies.com/
  51. </cite>
  52. <div class="_nBb">‎
  53. <div style="display:inline" onclick="google.sham(this);" aria-expanded="false"
  54. aria-haspopup="true" tabindex="0" data-ved="0CBUQ7B0wAA">
  55. <span class="_O0">
  56. </span>
  57. </div>
  58. <div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1">
  59. <ul>
  60. <li class="_Ykb">
  61. <a class="_Zkb" href="http://www.google.fr/url?url=http://webcache.googleusercontent
  62. .com/search%3Fcache:R1Z_4pGXjuIJ:http://test.psychologies.com/">
  63. En cache
  64. </a>
  65. </li>
  66. <li class="_Ykb">
  67. <a class="_Zkb" href="/search?safe=off&amp;q=related:test.psy.com/">
  68. Pages similaires
  69. </a>
  70. </li>
  71. </ul>
  72. </div>
  73. </div>
  74. </div>
  75. <span class="st">
  76. This should be the content.
  77. </span>
  78. <br>
  79. <div class="osl">‎
  80. <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/">
  81. Test Personnalité
  82. </a> - ‎
  83. <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/">
  84. Tests - Moi
  85. </a> - ‎
  86. <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/tests-couple">
  87. Test Couple
  88. </a>
  89. - ‎
  90. <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/tests-amour">
  91. Test Amour
  92. </a>
  93. </div>
  94. </div>
  95. </div>
  96. <div class="g">
  97. <h3 class="r">
  98. <a href="http://www.google.com/images?q=toto">
  99. <b>This</b>
  100. </a>
  101. </h3>
  102. </div>
  103. <div class="g">
  104. <h3 class="r">
  105. <a href="http://www.google.com/search?q=toto">
  106. <b>This</b> is
  107. </a>
  108. </h3>
  109. </div>
  110. <div class="g">
  111. <h3 class="r">
  112. <a href="€">
  113. <b>This</b> is <b>the</b>
  114. </a>
  115. </h3>
  116. </div>
  117. <div class="g">
  118. <h3 class="r">
  119. <a href="/url?q=url">
  120. <b>This</b> is <b>the</b>
  121. </a>
  122. </h3>
  123. </div>
  124. <p class="_Bmc" style="margin:3px 8px">
  125. <a href="/search?num=20&amp;safe=off&amp;q=t&amp;revid=1754833769&amp;sa=X&amp;ei=-&amp;ved=">
  126. suggestion <b>title</b>
  127. </a>
  128. </p>
  129. """
  130. response = self.mock_response(html)
  131. results = google.response(response)
  132. self.assertEqual(type(results), list)
  133. self.assertEqual(len(results), 2)
  134. self.assertEqual(results[0]['title'], 'This is the title')
  135. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  136. self.assertEqual(results[0]['content'], 'This should be the content.')
  137. self.assertEqual(results[1]['suggestion'], 'suggestion title')
  138. html = """
  139. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  140. </li>
  141. """
  142. response = self.mock_response(html)
  143. results = google.response(response)
  144. self.assertEqual(type(results), list)
  145. self.assertEqual(len(results), 0)
  146. response = mock.Mock(text='<html></html>', url='https://sorry.google.com')
  147. response.search_params = mock.Mock()
  148. response.search_params.get = mock.Mock(return_value='www.google.com')
  149. self.assertRaises(RuntimeWarning, google.response, response)
  150. response = mock.Mock(text='<html></html>', url='https://www.google.com/sorry/IndexRedirect')
  151. response.search_params = mock.Mock()
  152. response.search_params.get = mock.Mock(return_value='www.google.com')
  153. self.assertRaises(RuntimeWarning, google.response, response)
  154. def test_parse_images(self):
  155. html = """
  156. <li>
  157. <div>
  158. <a href="http://www.google.com/url?q=http://this.is.the.url/">
  159. <img style="margin:3px 0;margin-right:6px;padding:0" height="90"
  160. src="https://this.is.the.image/image.jpg" width="60" align="middle" alt="" border="0">
  161. </a>
  162. </div>
  163. </li>
  164. """
  165. dom = lxml.html.fromstring(html)
  166. results = google.parse_images(dom, 'www.google.com')
  167. self.assertEqual(type(results), list)
  168. self.assertEqual(len(results), 1)
  169. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  170. self.assertEqual(results[0]['title'], '')
  171. self.assertEqual(results[0]['content'], '')
  172. self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')
  173. def test_fetch_supported_languages(self):
  174. html = """<html></html>"""
  175. response = mock.Mock(text=html)
  176. languages = google._fetch_supported_languages(response)
  177. self.assertEqual(type(languages), dict)
  178. self.assertEqual(len(languages), 0)
  179. html = u"""
  180. <html>
  181. <body>
  182. <table>
  183. <tbody>
  184. <tr>
  185. <td>
  186. <font>
  187. <label>
  188. <span id="ten">English</span>
  189. </label>
  190. </font>
  191. </td>
  192. <td>
  193. <font>
  194. <label>
  195. <span id="tzh-CN">中文 (简体)</span>
  196. </label>
  197. <label>
  198. <span id="tzh-TW">中文 (繁體)</span>
  199. </label>
  200. </font>
  201. </td>
  202. </tr>
  203. </tbody>
  204. </table>
  205. </body>
  206. </html>
  207. """
  208. response = mock.Mock(text=html)
  209. languages = google._fetch_supported_languages(response)
  210. self.assertEqual(type(languages), dict)
  211. self.assertEqual(len(languages), 3)
  212. self.assertIn('en', languages)
  213. self.assertIn('zh-CN', languages)
  214. self.assertIn('zh-TW', languages)
  215. self.assertEquals(type(languages['en']), dict)
  216. self.assertEquals(type(languages['zh-CN']), dict)
  217. self.assertEquals(type(languages['zh-TW']), dict)
  218. self.assertIn('name', languages['en'])
  219. self.assertIn('name', languages['zh-CN'])
  220. self.assertIn('name', languages['zh-TW'])
  221. self.assertEquals(languages['en']['name'], 'English')
  222. self.assertEquals(languages['zh-CN']['name'], u'中文 (简体)')
  223. self.assertEquals(languages['zh-TW']['name'], u'中文 (繁體)')