test_bing_images.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. dicto['language'] = 'fr_FR'
  12. dicto['safesearch'] = 1
  13. dicto['time_range'] = ''
  14. params = bing_images.request(query, dicto)
  15. self.assertTrue('url' in params)
  16. self.assertTrue(query in params['url'])
  17. self.assertTrue('bing.com' in params['url'])
  18. self.assertTrue('SRCHHPGUSR' in params['cookies'])
  19. self.assertTrue('fr' in params['cookies']['SRCHHPGUSR'])
  20. dicto['language'] = 'all'
  21. params = bing_images.request(query, dicto)
  22. self.assertIn('SRCHHPGUSR', params['cookies'])
  23. self.assertIn('en', params['cookies']['SRCHHPGUSR'])
  24. def test_response(self):
  25. self.assertRaises(AttributeError, bing_images.response, None)
  26. self.assertRaises(AttributeError, bing_images.response, [])
  27. self.assertRaises(AttributeError, bing_images.response, '')
  28. self.assertRaises(AttributeError, bing_images.response, '[]')
  29. response = mock.Mock(text='<html></html>')
  30. self.assertEqual(bing_images.response(response), [])
  31. response = mock.Mock(text='<html></html>')
  32. self.assertEqual(bing_images.response(response), [])
  33. html = """
  34. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px"><div>
  35. <a href="/images/search?q=south&amp;view=detailv2&amp;&amp;id=7E92863981CCFB89FBDD55205C742DFDA3290CF6&amp;selectedIndex=9&amp;ccid=vzvIfv5u&amp;simid=608055786735667000&amp;thid=OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0" ihk="OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0" t1="South Carolina" t2="747 x 589 &#183; 29 kB &#183; gif" t3="www.digital-topo-maps.com/county-map/south-carolina.shtml" hh="236" hw="300" m='{ns:"images",k:"5117",mid:"7E92863981CCFB89FBDD55205C742DFDA3290CF6",md5:"bf3bc87efe6e0e476be8cc34bf6cd80e",surl:"http://www.digital-topo-maps.com/county-map/south-carolina.shtml",imgurl:"http://www.digital-topo-maps.com/county-map/south-carolina-county-map.gif",tid:"OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0",ow:"480",docid:"608055786735667000",oh:"378",tft:"45"}' mid="7E92863981CCFB89FBDD55205C742DFDA3290CF6" h="ID=images,5117.1">
  36. <img class="img_hid" src2="https://tse4.mm.bing.net/th?id=OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0&amp;w=210&amp;h=154&amp;c=7&amp;rs=1&amp;qlt=90&amp;o=4&amp;pid=1.1" style="width:210px;height:154px;" width="210" height="154">
  37. </a>
  38. </div></div>
  39. """ # noqa
  40. html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
  41. response = mock.Mock(text=html)
  42. results = bing_images.response(response)
  43. self.assertEqual(type(results), list)
  44. self.assertEqual(len(results), 1)
  45. self.assertEqual(results[0]['title'], 'South Carolina')
  46. self.assertEqual(results[0]['url'],
  47. 'http://www.digital-topo-maps.com/county-map/south-carolina.shtml')
  48. self.assertEqual(results[0]['content'], '')
  49. self.assertEqual(results[0]['thumbnail_src'],
  50. 'https://www.bing.com/th?id=OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0')
  51. self.assertEqual(results[0]['img_src'],
  52. 'http://www.digital-topo-maps.com/county-map/south-carolina-county-map.gif')
  53. html = """
  54. <a href="#" ihk="HN.608003696942779811"
  55. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  56. mid:&quot;59EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  57. surl:&quot;http://www.page.url/&quot;,
  58. imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,oh:&quot;238&quot;,
  59. tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  60. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  61. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  62. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  63. style="height:144px;" width="178" height="144"/>
  64. </a>
  65. """
  66. response = mock.Mock(text=html)
  67. results = bing_images.response(response)
  68. self.assertEqual(type(results), list)
  69. self.assertEqual(len(results), 0)
  70. html = """
  71. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px"><div>
  72. <a href="#" ihk="HN.608003696942779811"
  73. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  74. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  75. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  76. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  77. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  78. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  79. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  80. style="height:144px;" width="178" height="144"/>
  81. </a>
  82. </div></div>
  83. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px"><div>
  84. <a href="#" ihk="HN.608003696942779811"
  85. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  86. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  87. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  88. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  89. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  90. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  91. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  92. style="height:144px;" width="178" height="144"/>
  93. </a>
  94. </div></div>
  95. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px"><div>
  96. <a href="#" ihk="HN.608003696942779811"
  97. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  98. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  99. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  100. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  101. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  102. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  103. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  104. style="height:144px;" width="178" height="144"/>
  105. </a>
  106. </div></div>
  107. """
  108. html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
  109. response = mock.Mock(text=html)
  110. results = bing_images.response(response)
  111. self.assertEqual(type(results), list)
  112. self.assertEqual(len(results), 3)