123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import bing
  4. from searx.testing import SearxTestCase
  5. class TestBingEngine(SearxTestCase):
  6. def test_request(self):
  7. bing.supported_languages = ['en', 'fr', 'zh-CHS', 'zh-CHT', 'pt-PT', 'pt-BR']
  8. query = u'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 0
  11. dicto['language'] = 'fr-FR'
  12. params = bing.request(query.encode('utf-8'), dicto)
  13. self.assertTrue('url' in params)
  14. self.assertTrue(query in params['url'])
  15. self.assertTrue('language%3AFR' in params['url'])
  16. self.assertTrue('bing.com' in params['url'])
  17. def test_response(self):
  18. self.assertRaises(AttributeError, bing.response, None)
  19. self.assertRaises(AttributeError, bing.response, [])
  20. self.assertRaises(AttributeError, bing.response, '')
  21. self.assertRaises(AttributeError, bing.response, '[]')
  22. response = mock.Mock(text='<html></html>')
  23. self.assertEqual(bing.response(response), [])
  24. response = mock.Mock(text='<html></html>')
  25. self.assertEqual(bing.response(response), [])
  26. html = """
  27. <div class="sa_cc" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  28. <div Class="sa_mc">
  29. <div class="sb_tlst">
  30. <h3>
  31. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  32. <strong>This</strong> should be the title</a>
  33. </h3>
  34. </div>
  35. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  36. <span class="c_tlbxTrg">
  37. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  38. </span>
  39. </span>
  40. </div>
  41. <p><strong>This</strong> should be the content.</p>
  42. </div>
  43. </div>
  44. """
  45. response = mock.Mock(text=html)
  46. results = bing.response(response)
  47. self.assertEqual(type(results), list)
  48. self.assertEqual(len(results), 1)
  49. self.assertEqual(results[0]['title'], 'This should be the title')
  50. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  51. self.assertEqual(results[0]['content'], 'This should be the content.')
  52. html = """
  53. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  54. <div Class="sa_mc">
  55. <div class="sb_tlst">
  56. <h2>
  57. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  58. <strong>This</strong> should be the title</a>
  59. </h2>
  60. </div>
  61. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  62. <span class="c_tlbxTrg">
  63. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  64. </span>
  65. </span>
  66. </div>
  67. <p><strong>This</strong> should be the content.</p>
  68. </div>
  69. </li>
  70. """
  71. response = mock.Mock(text=html)
  72. results = bing.response(response)
  73. self.assertEqual(type(results), list)
  74. self.assertEqual(len(results), 1)
  75. self.assertEqual(results[0]['title'], 'This should be the title')
  76. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  77. self.assertEqual(results[0]['content'], 'This should be the content.')
  78. def test_fetch_supported_languages(self):
  79. html = """<html></html>"""
  80. response = mock.Mock(text=html)
  81. results = bing._fetch_supported_languages(response)
  82. self.assertEqual(type(results), list)
  83. self.assertEqual(len(results), 0)
  84. html = """
  85. <html>
  86. <body>
  87. <form>
  88. <div id="limit-languages">
  89. <div>
  90. <div><input id="es" value="es"></input></div>
  91. </div>
  92. <div>
  93. <div><input id="pt_BR" value="pt_BR"></input></div>
  94. <div><input id="pt_PT" value="pt_PT"></input></div>
  95. </div>
  96. </div>
  97. </form>
  98. </body>
  99. </html>
  100. """
  101. response = mock.Mock(text=html)
  102. languages = bing._fetch_supported_languages(response)
  103. self.assertEqual(type(languages), list)
  104. self.assertEqual(len(languages), 3)
  105. self.assertIn('es', languages)
  106. self.assertIn('pt-BR', languages)
  107. self.assertIn('pt-PT', languages)