test_bing_images.py 7.1KB

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