test_bing_images.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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">
  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>
  38. """
  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'], 'http://www.digital-topo-maps.com/county-map/south-carolina.shtml')
  46. self.assertEqual(results[0]['content'], '')
  47. self.assertEqual(results[0]['thumbnail_src'], 'https://www.bing.com/th?id=OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0')
  48. self.assertEqual(results[0]['img_src'], 'http://www.digital-topo-maps.com/county-map/south-carolina-county-map.gif')
  49. html = """
  50. <a href="#" ihk="HN.608003696942779811"
  51. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  52. mid:&quot;59EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  53. surl:&quot;http://www.page.url/&quot;,
  54. imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,oh:&quot;238&quot;,
  55. tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  56. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  57. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  58. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  59. style="height:144px;" width="178" height="144"/>
  60. </a>
  61. """
  62. response = mock.Mock(text=html)
  63. results = bing_images.response(response)
  64. self.assertEqual(type(results), list)
  65. self.assertEqual(len(results), 0)
  66. html = """
  67. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  68. <a href="#" ihk="HN.608003696942779811"
  69. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  70. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  71. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  72. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  73. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  74. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  75. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  76. style="height:144px;" width="178" height="144"/>
  77. </a>
  78. </div>
  79. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  80. <a href="#" ihk="HN.608003696942779811"
  81. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  82. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  83. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  84. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  85. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  86. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  87. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  88. style="height:144px;" width="178" height="144"/>
  89. </a>
  90. </div>
  91. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  92. <a href="#" ihk="HN.608003696942779811"
  93. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  94. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  95. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  96. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  97. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  98. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  99. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  100. style="height:144px;" width="178" height="144"/>
  101. </a>
  102. </div>
  103. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  104. <a href="#" ihk="HN.608003696942779811"
  105. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  106. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  107. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  108. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  109. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  110. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  111. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  112. style="height:144px;" width="178" height="144"/>
  113. </a>
  114. </div>
  115. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  116. <a href="#" ihk="HN.608003696942779811"
  117. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  118. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  119. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  120. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  121. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  122. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  123. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  124. style="height:144px;" width="178" height="144"/>
  125. </a>
  126. </div>
  127. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  128. <a href="#" ihk="HN.608003696942779811"
  129. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  130. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  131. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  132. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  133. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  134. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  135. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  136. style="height:144px;" width="178" height="144"/>
  137. </a>
  138. </div>
  139. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  140. <a href="#" ihk="HN.608003696942779811"
  141. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  142. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  143. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  144. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  145. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  146. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  147. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  148. style="height:144px;" width="178" height="144"/>
  149. </a>
  150. </div>
  151. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  152. <a href="#" ihk="HN.608003696942779811"
  153. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  154. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  155. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  156. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  157. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  158. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  159. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  160. style="height:144px;" width="178" height="144"/>
  161. </a>
  162. </div>
  163. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  164. <a href="#" ihk="HN.608003696942779811"
  165. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  166. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  167. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  168. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  169. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  170. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  171. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  172. style="height:144px;" width="178" height="144"/>
  173. </a>
  174. </div>
  175. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  176. <a href="#" ihk="HN.608003696942779811"
  177. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  178. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  179. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  180. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  181. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  182. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  183. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  184. style="height:144px;" width="178" height="144"/>
  185. </a>
  186. </div>
  187. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  188. <a href="#" ihk="HN.608003696942779811"
  189. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  190. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  191. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  192. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  193. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  194. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  195. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  196. style="height:144px;" width="178" height="144"/>
  197. </a>
  198. </div>
  199. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  200. <a href="#" ihk="HN.608003696942779811"
  201. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  202. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  203. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  204. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  205. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  206. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  207. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  208. style="height:144px;" width="178" height="144"/>
  209. </a>
  210. </div>
  211. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  212. <a href="#" ihk="HN.608003696942779811"
  213. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  214. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  215. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  216. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  217. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  218. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  219. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  220. style="height:144px;" width="178" height="144"/>
  221. </a>
  222. </div>
  223. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  224. <a href="#" ihk="HN.608003696942779811"
  225. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  226. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  227. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  228. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  229. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  230. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  231. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  232. style="height:144px;" width="178" height="144"/>
  233. </a>
  234. </div>
  235. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  236. <a href="#" ihk="HN.608003696942779811"
  237. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  238. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  239. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  240. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  241. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  242. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  243. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  244. style="height:144px;" width="178" height="144"/>
  245. </a>
  246. </div>
  247. """
  248. html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
  249. response = mock.Mock(text=html)
  250. results = bing_images.response(response)
  251. self.assertEqual(type(results), list)
  252. self.assertEqual(len(results), 10)