test_bing_images.py 16KB

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