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