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