test_bing_news.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import bing_news
  4. from searx.testing import SearxTestCase
  5. import lxml
  6. class TestBingNewsEngine(SearxTestCase):
  7. def test_request(self):
  8. bing_news.supported_languages = ['en', 'fr']
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. dicto['pageno'] = 1
  12. dicto['language'] = 'fr-FR'
  13. dicto['time_range'] = ''
  14. params = bing_news.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn(query, params['url'])
  17. self.assertIn('bing.com', params['url'])
  18. self.assertIn('fr', params['url'])
  19. dicto['language'] = 'all'
  20. params = bing_news.request(query, dicto)
  21. self.assertIn('en', params['url'])
  22. def test_no_url_in_request_year_time_range(self):
  23. dicto = defaultdict(dict)
  24. query = 'test_query'
  25. dicto['time_range'] = 'year'
  26. params = bing_news.request(query, dicto)
  27. self.assertEqual({}, params['url'])
  28. def test_response(self):
  29. self.assertRaises(AttributeError, bing_news.response, None)
  30. self.assertRaises(AttributeError, bing_news.response, [])
  31. self.assertRaises(AttributeError, bing_news.response, '')
  32. self.assertRaises(AttributeError, bing_news.response, '[]')
  33. response = mock.Mock(content='<html></html>')
  34. self.assertEqual(bing_news.response(response), [])
  35. response = mock.Mock(content='<html></html>')
  36. self.assertEqual(bing_news.response(response), [])
  37. html = """<?xml version="1.0" encoding="utf-8" ?>
  38. <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
  39. <channel>
  40. <title>python - Bing News</title>
  41. <link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  42. <description>Search results</description>
  43. <image>
  44. <url>http://10.53.64.9/rsslogo.gif</url>
  45. <title>test</title>
  46. <link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  47. </image>
  48. <copyright>Copyright</copyright>
  49. <item>
  50. <title>Title</title>
  51. <link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&amp;aid=&amp;tid=c237eccc50bd4758b106a5e3c94fce09&amp;url=http%3a%2f%2furl.of.article%2f&amp;c=xxxxxxxxx&amp;mkt=en-us</link>
  52. <description>Article Content</description>
  53. <pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate>
  54. <News:Source>Infoworld</News:Source>
  55. <News:Image>http://a1.bing4.com/th?id=ON.13371337133713371337133713371337&amp;pid=News</News:Image>
  56. <News:ImageSize>w={0}&amp;h={1}&amp;c=7</News:ImageSize>
  57. <News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio>
  58. <News:ImageMaxWidth>620</News:ImageMaxWidth>
  59. <News:ImageMaxHeight>413</News:ImageMaxHeight>
  60. </item>
  61. <item>
  62. <title>Another Title</title>
  63. <link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&amp;aid=&amp;tid=c237eccc50bd4758b106a5e3c94fce09&amp;url=http%3a%2f%2fanother.url.of.article%2f&amp;c=xxxxxxxxx&amp;mkt=en-us</link>
  64. <description>Another Article Content</description>
  65. <pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate>
  66. </item>
  67. </channel>
  68. </rss>""" # noqa
  69. response = mock.Mock(content=html.encode('utf-8'))
  70. results = bing_news.response(response)
  71. self.assertEqual(type(results), list)
  72. self.assertEqual(len(results), 2)
  73. self.assertEqual(results[0]['title'], 'Title')
  74. self.assertEqual(results[0]['url'], 'http://url.of.article/')
  75. self.assertEqual(results[0]['content'], 'Article Content')
  76. self.assertEqual(results[0]['img_src'], 'https://www.bing.com/th?id=ON.13371337133713371337133713371337')
  77. self.assertEqual(results[1]['title'], 'Another Title')
  78. self.assertEqual(results[1]['url'], 'http://another.url.of.article/')
  79. self.assertEqual(results[1]['content'], 'Another Article Content')
  80. self.assertNotIn('img_src', results[1])
  81. html = """<?xml version="1.0" encoding="utf-8" ?>
  82. <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
  83. <channel>
  84. <title>python - Bing News</title>
  85. <link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  86. <description>Search results</description>
  87. <image>
  88. <url>http://10.53.64.9/rsslogo.gif</url>
  89. <title>test</title>
  90. <link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  91. </image>
  92. <copyright>Copyright</copyright>
  93. <item>
  94. <title>Title</title>
  95. <link>http://another.url.of.article/</link>
  96. <description>Article Content</description>
  97. <pubDate>garbage</pubDate>
  98. <News:Source>Infoworld</News:Source>
  99. <News:Image>http://another.bing.com/image</News:Image>
  100. <News:ImageSize>w={0}&amp;h={1}&amp;c=7</News:ImageSize>
  101. <News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio>
  102. <News:ImageMaxWidth>620</News:ImageMaxWidth>
  103. <News:ImageMaxHeight>413</News:ImageMaxHeight>
  104. </item>
  105. </channel>
  106. </rss>""" # noqa
  107. response = mock.Mock(content=html.encode('utf-8'))
  108. results = bing_news.response(response)
  109. self.assertEqual(type(results), list)
  110. self.assertEqual(len(results), 1)
  111. self.assertEqual(results[0]['title'], 'Title')
  112. self.assertEqual(results[0]['url'], 'http://another.url.of.article/')
  113. self.assertEqual(results[0]['content'], 'Article Content')
  114. self.assertEqual(results[0]['img_src'], 'http://another.bing.com/image')
  115. html = """<?xml version="1.0" encoding="utf-8" ?>
  116. <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
  117. <channel>
  118. <title>python - Bing News</title>
  119. <link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  120. <description>Search results</description>
  121. <image>
  122. <url>http://10.53.64.9/rsslogo.gif</url>
  123. <title>test</title>
  124. <link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  125. </image>
  126. </channel>
  127. </rss>""" # noqa
  128. response = mock.Mock(content=html.encode('utf-8'))
  129. results = bing_news.response(response)
  130. self.assertEqual(type(results), list)
  131. self.assertEqual(len(results), 0)
  132. html = """<?xml version="1.0" encoding="utf-8" ?>gabarge"""
  133. response = mock.Mock(content=html.encode('utf-8'))
  134. self.assertRaises(lxml.etree.XMLSyntaxError, bing_news.response, response)