test_bing_images.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import bing_images
  5. from searx.testing import SearxTestCase
  6. class TestBingImagesEngine(SearxTestCase):
  7. def test_request(self):
  8. bing_images.supported_languages = ['fr-FR', 'en-US']
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. dicto['pageno'] = 1
  12. dicto['language'] = 'fr-FR'
  13. dicto['safesearch'] = 1
  14. dicto['time_range'] = ''
  15. params = bing_images.request(query, dicto)
  16. self.assertTrue('url' in params)
  17. self.assertTrue(query in params['url'])
  18. self.assertTrue('bing.com' in params['url'])
  19. self.assertTrue('SRCHHPGUSR' in params['cookies'])
  20. self.assertTrue('DEMOTE' in params['cookies']['SRCHHPGUSR'])
  21. self.assertTrue('_EDGE_S' in params['cookies'])
  22. self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])
  23. dicto['language'] = 'fr'
  24. params = bing_images.request(query, dicto)
  25. self.assertTrue('_EDGE_S' in params['cookies'])
  26. self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])
  27. dicto['language'] = 'all'
  28. params = bing_images.request(query, dicto)
  29. self.assertTrue('_EDGE_S' in params['cookies'])
  30. self.assertTrue('en-us' in params['cookies']['_EDGE_S'])
  31. def test_response(self):
  32. self.assertRaises(AttributeError, bing_images.response, None)
  33. self.assertRaises(AttributeError, bing_images.response, [])
  34. self.assertRaises(AttributeError, bing_images.response, '')
  35. self.assertRaises(AttributeError, bing_images.response, '[]')
  36. response = mock.Mock(text='<html></html>')
  37. self.assertEqual(bing_images.response(response), [])
  38. response = mock.Mock(text='<html></html>')
  39. self.assertEqual(bing_images.response(response), [])
  40. html = """
  41. <div id="mmComponent_images_1">
  42. <ul>
  43. <li>
  44. <div>
  45. <div class="imgpt">
  46. <a m='{"purl":"page_url","murl":"img_url"}' mad='{"turl":"thumb_url"}'>
  47. <img src="" alt="alt text" />
  48. </a>
  49. </div>
  50. <div></div>
  51. </div>
  52. <div>
  53. <div class="imgpt">
  54. <a m='{"purl":"page_url2","murl":"img_url2"}' mad='{"turl":"thumb_url2"}'>
  55. <img src="" alt="alt text 2" />
  56. </a>
  57. </div>
  58. </div>
  59. </li>
  60. </ul>
  61. <ul>
  62. <li>
  63. <div>
  64. <div class="imgpt">
  65. <a m='{"purl":"page_url3","murl":"img_url3"}' mad='{"turl":"thumb_url3"}'>
  66. <img src="" alt="alt text 3" />
  67. </a>
  68. </div>
  69. </div>
  70. </li>
  71. </ul>
  72. </div>
  73. """
  74. html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
  75. response = mock.Mock(text=html)
  76. results = bing_images.response(response)
  77. self.assertEqual(type(results), list)
  78. self.assertEqual(len(results), 3)
  79. self.assertEqual(results[0]['title'], 'alt text')
  80. self.assertEqual(results[0]['url'], 'page_url')
  81. self.assertEqual(results[0]['content'], '')
  82. self.assertEqual(results[0]['thumbnail_src'], 'thumb_url')
  83. self.assertEqual(results[0]['img_src'], 'img_url')
  84. def test_fetch_supported_languages(self):
  85. html = """
  86. <div>
  87. <div id="region-section-content">
  88. <ul class="b_vList">
  89. <li>
  90. <a href="https://bing...&setmkt=de-DE&s...">Germany</a>
  91. <a href="https://bing...&setmkt=nb-NO&s...">Norway</a>
  92. </li>
  93. </ul>
  94. <ul class="b_vList">
  95. <li>
  96. <a href="https://bing...&setmkt=es-AR&s...">Argentina</a>
  97. </li>
  98. </ul>
  99. </div>
  100. </div>
  101. """
  102. response = mock.Mock(text=html)
  103. languages = list(bing_images._fetch_supported_languages(response))
  104. self.assertEqual(len(languages), 3)
  105. self.assertIn('de-DE', languages)
  106. self.assertIn('no-NO', languages)
  107. self.assertIn('es-AR', languages)