123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'fr_FR'
  11. params = bing.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('bing.com' in params['url'])
  15. self.assertTrue('SRCHHPGUSR' in params['cookies'])
  16. self.assertTrue('fr' in params['cookies']['SRCHHPGUSR'])
  17. dicto['language'] = 'all'
  18. params = bing.request(query, dicto)
  19. self.assertTrue('SRCHHPGUSR' in params['cookies'])
  20. self.assertTrue('en' in params['cookies']['SRCHHPGUSR'])
  21. def test_response(self):
  22. self.assertRaises(AttributeError, bing.response, None)
  23. self.assertRaises(AttributeError, bing.response, [])
  24. self.assertRaises(AttributeError, bing.response, '')
  25. self.assertRaises(AttributeError, bing.response, '[]')
  26. response = mock.Mock(text='<html></html>')
  27. self.assertEqual(bing.response(response), [])
  28. response = mock.Mock(text='<html></html>')
  29. self.assertEqual(bing.response(response), [])
  30. html = """
  31. <div class="sa_cc" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  32. <div Class="sa_mc">
  33. <div class="sb_tlst">
  34. <h3>
  35. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  36. <strong>This</strong> should be the title</a>
  37. </h3>
  38. </div>
  39. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  40. <span class="c_tlbxTrg">
  41. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  42. </span>
  43. </span>
  44. </div>
  45. <p><strong>This</strong> should be the content.</p>
  46. </div>
  47. </div>
  48. """
  49. response = mock.Mock(text=html)
  50. results = bing.response(response)
  51. self.assertEqual(type(results), list)
  52. self.assertEqual(len(results), 1)
  53. self.assertEqual(results[0]['title'], 'This should be the title')
  54. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  55. self.assertEqual(results[0]['content'], 'This should be the content.')
  56. html = """
  57. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  58. <div Class="sa_mc">
  59. <div class="sb_tlst">
  60. <h2>
  61. <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
  62. <strong>This</strong> should be the title</a>
  63. </h2>
  64. </div>
  65. <div class="sb_meta"><cite><strong>this</strong>.meta.com</cite>
  66. <span class="c_tlbxTrg">
  67. <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
  68. </span>
  69. </span>
  70. </div>
  71. <p><strong>This</strong> should be the content.</p>
  72. </div>
  73. </li>
  74. """
  75. response = mock.Mock(text=html)
  76. results = bing.response(response)
  77. self.assertEqual(type(results), list)
  78. self.assertEqual(len(results), 1)
  79. self.assertEqual(results[0]['title'], 'This should be the title')
  80. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  81. self.assertEqual(results[0]['content'], 'This should be the content.')