test_bing_images.py 16 KB

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