| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  | from collections import defaultdict | 
					
						
							|  |  |  | import mock | 
					
						
							|  |  |  | from searx.engines import bing_news | 
					
						
							|  |  |  | from searx.testing import SearxTestCase | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  | import lxml | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestBingNewsEngine(SearxTestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_request(self): | 
					
						
							| 
									
										
										
										
											2018-03-01 05:30:48 +01:00
										 |  |  |         bing_news.supported_languages = ['en', 'fr'] | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         query = 'test_query' | 
					
						
							|  |  |  |         dicto = defaultdict(dict) | 
					
						
							|  |  |  |         dicto['pageno'] = 1 | 
					
						
							| 
									
										
										
										
											2018-03-01 05:30:48 +01:00
										 |  |  |         dicto['language'] = 'fr-FR' | 
					
						
							| 
									
										
										
										
											2016-10-30 18:14:42 +01:00
										 |  |  |         dicto['time_range'] = '' | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         params = bing_news.request(query, dicto) | 
					
						
							|  |  |  |         self.assertIn('url', params) | 
					
						
							|  |  |  |         self.assertIn(query, params['url']) | 
					
						
							|  |  |  |         self.assertIn('bing.com', params['url']) | 
					
						
							|  |  |  |         self.assertIn('fr', params['url']) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 16:41:14 +01:00
										 |  |  |     def test_no_url_in_request_year_time_range(self): | 
					
						
							|  |  |  |         dicto = defaultdict(dict) | 
					
						
							|  |  |  |         query = 'test_query' | 
					
						
							|  |  |  |         dicto['time_range'] = 'year' | 
					
						
							|  |  |  |         params = bing_news.request(query, dicto) | 
					
						
							|  |  |  |         self.assertEqual({}, params['url']) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |     def test_response(self): | 
					
						
							|  |  |  |         self.assertRaises(AttributeError, bing_news.response, None) | 
					
						
							|  |  |  |         self.assertRaises(AttributeError, bing_news.response, []) | 
					
						
							|  |  |  |         self.assertRaises(AttributeError, bing_news.response, '') | 
					
						
							|  |  |  |         self.assertRaises(AttributeError, bing_news.response, '[]') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 15:36:52 +02:00
										 |  |  |         response = mock.Mock(content='<html></html>') | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         self.assertEqual(bing_news.response(response), []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 15:36:52 +02:00
										 |  |  |         response = mock.Mock(content='<html></html>') | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         self.assertEqual(bing_news.response(response), []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  |         html = """<?xml version="1.0" encoding="utf-8" ?>
 | 
					
						
							|  |  |  | <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS"> | 
					
						
							|  |  |  |     <channel> | 
					
						
							|  |  |  |         <title>python - Bing News</title> | 
					
						
							|  |  |  |         <link>https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS</link> | 
					
						
							|  |  |  |         <description>Search results</description> | 
					
						
							|  |  |  |         <image> | 
					
						
							|  |  |  |             <url>http://10.53.64.9/rsslogo.gif</url> | 
					
						
							|  |  |  |             <title>test</title> | 
					
						
							|  |  |  |             <link>https://www.bing.com:443/news/search?q=test&setmkt=en-US&first=1&format=RSS</link> | 
					
						
							|  |  |  |         </image> | 
					
						
							|  |  |  |         <copyright>Copyright</copyright> | 
					
						
							|  |  |  |         <item> | 
					
						
							|  |  |  |             <title>Title</title> | 
					
						
							|  |  |  |             <link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&aid=&tid=c237eccc50bd4758b106a5e3c94fce09&url=http%3a%2f%2furl.of.article%2f&c=xxxxxxxxx&mkt=en-us</link> | 
					
						
							|  |  |  |             <description>Article Content</description> | 
					
						
							|  |  |  |             <pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate> | 
					
						
							|  |  |  |             <News:Source>Infoworld</News:Source> | 
					
						
							|  |  |  |             <News:Image>http://a1.bing4.com/th?id=ON.13371337133713371337133713371337&pid=News</News:Image> | 
					
						
							|  |  |  |             <News:ImageSize>w={0}&h={1}&c=7</News:ImageSize> | 
					
						
							|  |  |  |             <News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio> | 
					
						
							|  |  |  |             <News:ImageMaxWidth>620</News:ImageMaxWidth> | 
					
						
							|  |  |  |             <News:ImageMaxHeight>413</News:ImageMaxHeight> | 
					
						
							|  |  |  |         </item> | 
					
						
							|  |  |  |         <item> | 
					
						
							|  |  |  |             <title>Another Title</title> | 
					
						
							|  |  |  |             <link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&aid=&tid=c237eccc50bd4758b106a5e3c94fce09&url=http%3a%2f%2fanother.url.of.article%2f&c=xxxxxxxxx&mkt=en-us</link> | 
					
						
							|  |  |  |             <description>Another Article Content</description> | 
					
						
							|  |  |  |             <pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate> | 
					
						
							|  |  |  |         </item> | 
					
						
							|  |  |  |     </channel> | 
					
						
							|  |  |  | </rss>"""  # noqa
 | 
					
						
							| 
									
										
										
										
											2017-05-22 15:36:52 +02:00
										 |  |  |         response = mock.Mock(content=html.encode('utf-8')) | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         results = bing_news.response(response) | 
					
						
							|  |  |  |         self.assertEqual(type(results), list) | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  |         self.assertEqual(len(results), 2) | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         self.assertEqual(results[0]['title'], 'Title') | 
					
						
							|  |  |  |         self.assertEqual(results[0]['url'], 'http://url.of.article/') | 
					
						
							|  |  |  |         self.assertEqual(results[0]['content'], 'Article Content') | 
					
						
							| 
									
										
										
										
											2017-02-12 14:58:49 +01:00
										 |  |  |         self.assertEqual(results[0]['img_src'], 'https://www.bing.com/th?id=ON.13371337133713371337133713371337') | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  |         self.assertEqual(results[1]['title'], 'Another Title') | 
					
						
							|  |  |  |         self.assertEqual(results[1]['url'], 'http://another.url.of.article/') | 
					
						
							|  |  |  |         self.assertEqual(results[1]['content'], 'Another Article Content') | 
					
						
							| 
									
										
										
										
											2017-02-12 14:58:49 +01:00
										 |  |  |         self.assertNotIn('img_src', results[1]) | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  |         html = """<?xml version="1.0" encoding="utf-8" ?>
 | 
					
						
							|  |  |  | <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS"> | 
					
						
							|  |  |  |     <channel> | 
					
						
							|  |  |  |         <title>python - Bing News</title> | 
					
						
							|  |  |  |         <link>https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS</link> | 
					
						
							|  |  |  |         <description>Search results</description> | 
					
						
							|  |  |  |         <image> | 
					
						
							|  |  |  |             <url>http://10.53.64.9/rsslogo.gif</url> | 
					
						
							|  |  |  |             <title>test</title> | 
					
						
							|  |  |  |             <link>https://www.bing.com:443/news/search?q=test&setmkt=en-US&first=1&format=RSS</link> | 
					
						
							|  |  |  |         </image> | 
					
						
							|  |  |  |         <copyright>Copyright</copyright> | 
					
						
							|  |  |  |         <item> | 
					
						
							|  |  |  |             <title>Title</title> | 
					
						
							|  |  |  |             <link>http://another.url.of.article/</link> | 
					
						
							|  |  |  |             <description>Article Content</description> | 
					
						
							|  |  |  |             <pubDate>garbage</pubDate> | 
					
						
							|  |  |  |             <News:Source>Infoworld</News:Source> | 
					
						
							|  |  |  |             <News:Image>http://another.bing.com/image</News:Image> | 
					
						
							|  |  |  |             <News:ImageSize>w={0}&h={1}&c=7</News:ImageSize> | 
					
						
							|  |  |  |             <News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio> | 
					
						
							|  |  |  |             <News:ImageMaxWidth>620</News:ImageMaxWidth> | 
					
						
							|  |  |  |             <News:ImageMaxHeight>413</News:ImageMaxHeight> | 
					
						
							|  |  |  |         </item> | 
					
						
							|  |  |  |     </channel> | 
					
						
							|  |  |  | </rss>"""  # noqa
 | 
					
						
							| 
									
										
										
										
											2017-05-22 15:36:52 +02:00
										 |  |  |         response = mock.Mock(content=html.encode('utf-8')) | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         results = bing_news.response(response) | 
					
						
							|  |  |  |         self.assertEqual(type(results), list) | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  |         self.assertEqual(len(results), 1) | 
					
						
							|  |  |  |         self.assertEqual(results[0]['title'], 'Title') | 
					
						
							|  |  |  |         self.assertEqual(results[0]['url'], 'http://another.url.of.article/') | 
					
						
							|  |  |  |         self.assertEqual(results[0]['content'], 'Article Content') | 
					
						
							| 
									
										
										
										
											2017-02-12 14:58:49 +01:00
										 |  |  |         self.assertEqual(results[0]['img_src'], 'http://another.bing.com/image') | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         html = """<?xml version="1.0" encoding="utf-8" ?>
 | 
					
						
							|  |  |  | <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS"> | 
					
						
							|  |  |  |     <channel> | 
					
						
							|  |  |  |         <title>python - Bing News</title> | 
					
						
							|  |  |  |         <link>https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS</link> | 
					
						
							|  |  |  |         <description>Search results</description> | 
					
						
							|  |  |  |         <image> | 
					
						
							|  |  |  |             <url>http://10.53.64.9/rsslogo.gif</url> | 
					
						
							|  |  |  |             <title>test</title> | 
					
						
							|  |  |  |             <link>https://www.bing.com:443/news/search?q=test&setmkt=en-US&first=1&format=RSS</link> | 
					
						
							|  |  |  |         </image> | 
					
						
							|  |  |  |     </channel> | 
					
						
							|  |  |  | </rss>"""  # noqa
 | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 15:36:52 +02:00
										 |  |  |         response = mock.Mock(content=html.encode('utf-8')) | 
					
						
							| 
									
										
										
										
											2015-01-29 20:56:57 +01:00
										 |  |  |         results = bing_news.response(response) | 
					
						
							|  |  |  |         self.assertEqual(type(results), list) | 
					
						
							|  |  |  |         self.assertEqual(len(results), 0) | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         html = """<?xml version="1.0" encoding="utf-8" ?>gabarge""" | 
					
						
							| 
									
										
										
										
											2017-05-22 15:36:52 +02:00
										 |  |  |         response = mock.Mock(content=html.encode('utf-8')) | 
					
						
							| 
									
										
										
										
											2015-06-04 18:30:08 +02:00
										 |  |  |         self.assertRaises(lxml.etree.XMLSyntaxError, bing_news.response, response) |