test_bing_news.py 6.5KB

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