test_bing_news.py 6.7KB

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