test_google.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. query = 'test_query'
  15. dicto = defaultdict(dict)
  16. dicto['pageno'] = 1
  17. dicto['language'] = 'fr_FR'
  18. params = google.request(query, dicto)
  19. self.assertIn('url', params)
  20. self.assertIn(query, params['url'])
  21. self.assertIn('google.fr', params['url'])
  22. self.assertIn('fr', params['headers']['Accept-Language'])
  23. dicto['language'] = 'all'
  24. params = google.request(query, dicto)
  25. self.assertIn('google.com', params['url'])
  26. 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, '[]')
  32. response = self.mock_response('<html></html>')
  33. self.assertEqual(google.response(response), [])
  34. html = """
  35. <div class="g">
  36. <h3 class="r">
  37. <a href="http://this.should.be.the.link/">
  38. <b>This</b> is <b>the</b> title
  39. </a>
  40. </h3>
  41. <div class="s">
  42. <div class="kv" style="margin-bottom:2px">
  43. <cite>
  44. <b>test</b>.psychologies.com/
  45. </cite>
  46. <div class="_nBb">‎
  47. <div style="display:inline" onclick="google.sham(this);" aria-expanded="false"
  48. aria-haspopup="true" tabindex="0" data-ved="0CBUQ7B0wAA">
  49. <span class="_O0">
  50. </span>
  51. </div>
  52. <div style="display:none" class="am-dropdown-menu" role="menu" tabindex="-1">
  53. <ul>
  54. <li class="_Ykb">
  55. <a class="_Zkb" href="http://www.google.fr/url?url=http://webcache.googleusercontent
  56. .com/search%3Fcache:R1Z_4pGXjuIJ:http://test.psychologies.com/">
  57. En cache
  58. </a>
  59. </li>
  60. <li class="_Ykb">
  61. <a class="_Zkb" href="/search?safe=off&amp;q=related:test.psy.com/">
  62. Pages similaires
  63. </a>
  64. </li>
  65. </ul>
  66. </div>
  67. </div>
  68. </div>
  69. <span class="st">
  70. This should be the content.
  71. </span>
  72. <br>
  73. <div class="osl">‎
  74. <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/">
  75. Test Personnalité
  76. </a> - ‎
  77. <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/">
  78. Tests - Moi
  79. </a> - ‎
  80. <a href="http://www.google.fr/url?url=http://test.psychologies.com/test/tests-couple">
  81. Test Couple
  82. </a>
  83. - ‎
  84. <a href="http://www.google.fr/url?url=http://test.psychologies.com/tests/tests-amour">
  85. Test Amour
  86. </a>
  87. </div>
  88. </div>
  89. </div>
  90. <div class="g">
  91. <h3 class="r">
  92. <a href="http://www.google.com/images?q=toto">
  93. <b>This</b>
  94. </a>
  95. </h3>
  96. </div>
  97. <div class="g">
  98. <h3 class="r">
  99. <a href="http://www.google.com/search?q=toto">
  100. <b>This</b> is
  101. </a>
  102. </h3>
  103. </div>
  104. <div class="g">
  105. <h3 class="r">
  106. <a href="€">
  107. <b>This</b> is <b>the</b>
  108. </a>
  109. </h3>
  110. </div>
  111. <div class="g">
  112. <h3 class="r">
  113. <a href="/url?q=url">
  114. <b>This</b> is <b>the</b>
  115. </a>
  116. </h3>
  117. </div>
  118. <p class="_Bmc" style="margin:3px 8px">
  119. <a href="/search?num=20&amp;safe=off&amp;q=t&amp;revid=1754833769&amp;sa=X&amp;ei=-&amp;ved=">
  120. suggestion <b>title</b>
  121. </a>
  122. </p>
  123. """
  124. response = self.mock_response(html)
  125. results = google.response(response)
  126. self.assertEqual(type(results), list)
  127. self.assertEqual(len(results), 2)
  128. self.assertEqual(results[0]['title'], 'This is the title')
  129. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  130. self.assertEqual(results[0]['content'], 'This should be the content.')
  131. self.assertEqual(results[1]['suggestion'], 'suggestion title')
  132. html = """
  133. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  134. </li>
  135. """
  136. response = self.mock_response(html)
  137. results = google.response(response)
  138. self.assertEqual(type(results), list)
  139. self.assertEqual(len(results), 0)
  140. response = mock.Mock(text='<html></html>', url='https://sorry.google.com')
  141. response.search_params = mock.Mock()
  142. response.search_params.get = mock.Mock(return_value='www.google.com')
  143. self.assertRaises(RuntimeWarning, google.response, response)
  144. response = mock.Mock(text='<html></html>', url='https://www.google.com/sorry/IndexRedirect')
  145. response.search_params = mock.Mock()
  146. response.search_params.get = mock.Mock(return_value='www.google.com')
  147. self.assertRaises(RuntimeWarning, google.response, response)
  148. def test_parse_images(self):
  149. html = """
  150. <li>
  151. <div>
  152. <a href="http://www.google.com/url?q=http://this.is.the.url/">
  153. <img style="margin:3px 0;margin-right:6px;padding:0" height="90"
  154. src="https://this.is.the.image/image.jpg" width="60" align="middle" alt="" border="0">
  155. </a>
  156. </div>
  157. </li>
  158. """
  159. dom = lxml.html.fromstring(html)
  160. results = google.parse_images(dom, 'www.google.com')
  161. self.assertEqual(type(results), list)
  162. self.assertEqual(len(results), 1)
  163. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  164. self.assertEqual(results[0]['title'], '')
  165. self.assertEqual(results[0]['content'], '')
  166. self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg')