test_bing_images.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. """ # 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">
  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>
  82. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  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>
  94. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  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>
  106. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  107. <a href="#" ihk="HN.608003696942779811"
  108. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  109. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  110. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  111. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  112. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  113. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  114. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  115. style="height:144px;" width="178" height="144"/>
  116. </a>
  117. </div>
  118. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  119. <a href="#" ihk="HN.608003696942779811"
  120. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  121. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  122. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  123. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  124. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  125. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  126. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  127. style="height:144px;" width="178" height="144"/>
  128. </a>
  129. </div>
  130. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  131. <a href="#" ihk="HN.608003696942779811"
  132. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  133. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  134. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  135. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  136. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  137. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  138. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  139. style="height:144px;" width="178" height="144"/>
  140. </a>
  141. </div>
  142. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  143. <a href="#" ihk="HN.608003696942779811"
  144. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  145. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  146. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  147. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  148. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  149. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  150. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  151. style="height:144px;" width="178" height="144"/>
  152. </a>
  153. </div>
  154. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  155. <a href="#" ihk="HN.608003696942779811"
  156. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  157. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  158. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  159. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  160. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  161. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  162. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  163. style="height:144px;" width="178" height="144"/>
  164. </a>
  165. </div>
  166. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  167. <a href="#" ihk="HN.608003696942779811"
  168. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  169. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  170. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  171. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  172. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  173. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  174. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  175. style="height:144px;" width="178" height="144"/>
  176. </a>
  177. </div>
  178. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  179. <a href="#" ihk="HN.608003696942779811"
  180. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  181. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  182. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  183. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  184. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  185. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  186. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  187. style="height:144px;" width="178" height="144"/>
  188. </a>
  189. </div>
  190. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  191. <a href="#" ihk="HN.608003696942779811"
  192. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  193. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  194. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  195. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  196. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  197. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  198. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  199. style="height:144px;" width="178" height="144"/>
  200. </a>
  201. </div>
  202. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  203. <a href="#" ihk="HN.608003696942779811"
  204. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  205. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  206. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  207. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  208. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  209. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  210. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  211. style="height:144px;" width="178" height="144"/>
  212. </a>
  213. </div>
  214. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  215. <a href="#" ihk="HN.608003696942779811"
  216. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  217. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  218. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  219. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  220. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  221. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  222. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  223. style="height:144px;" width="178" height="144"/>
  224. </a>
  225. </div>
  226. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  227. <a href="#" ihk="HN.608003696942779811"
  228. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  229. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  230. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  231. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  232. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  233. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  234. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  235. style="height:144px;" width="178" height="144"/>
  236. </a>
  237. </div>
  238. <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
  239. <a href="#" ihk="HN.608003696942779811"
  240. m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
  241. mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
  242. surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
  243. oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
  244. mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
  245. t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
  246. <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
  247. style="height:144px;" width="178" height="144"/>
  248. </a>
  249. </div>
  250. """
  251. html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
  252. response = mock.Mock(text=html)
  253. results = bing_images.response(response)
  254. self.assertEqual(type(results), list)
  255. self.assertEqual(len(results), 10)