test_google.py 6.3 KB

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