123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import yahoo
  5. from searx.testing import SearxTestCase
  6. class TestYahooEngine(SearxTestCase):
  7. def test_parse_url(self):
  8. test_url = 'http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;_ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb' +\
  9. '2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10/RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=' +\
  10. 'dtcJsfP4mEeBOjnVfUQ-'
  11. url = yahoo.parse_url(test_url)
  12. self.assertEqual('https://this.is.the.url/', url)
  13. test_url = 'http://r.search.yahoo.com/_ylt=A0LElb9JUSKcAEGRXNyoA;_ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb' +\
  14. '2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10/RU=https%3a%2f%2fthis.is.the.url%2f/RS=' +\
  15. 'dtcJsfP4mEeBOjnVfUQ-'
  16. url = yahoo.parse_url(test_url)
  17. self.assertEqual('https://this.is.the.url/', url)
  18. test_url = 'https://this.is.the.url/'
  19. url = yahoo.parse_url(test_url)
  20. self.assertEqual('https://this.is.the.url/', url)
  21. def test_request(self):
  22. yahoo.supported_languages = ['en', 'fr', 'zh-CHT', 'zh-CHS']
  23. query = 'test_query'
  24. dicto = defaultdict(dict)
  25. dicto['pageno'] = 1
  26. dicto['time_range'] = ''
  27. dicto['language'] = 'fr-FR'
  28. params = yahoo.request(query, dicto)
  29. self.assertIn('url', params)
  30. self.assertIn(query, params['url'])
  31. self.assertIn('search.yahoo.com', params['url'])
  32. self.assertIn('fr', params['url'])
  33. self.assertIn('cookies', params)
  34. self.assertIn('sB', params['cookies'])
  35. self.assertIn('fr', params['cookies']['sB'])
  36. dicto['language'] = 'zh'
  37. params = yahoo.request(query, dicto)
  38. self.assertIn('zh_chs', params['url'])
  39. self.assertIn('zh_chs', params['cookies']['sB'])
  40. dicto['language'] = 'zh-TW'
  41. params = yahoo.request(query, dicto)
  42. self.assertIn('zh_cht', params['url'])
  43. self.assertIn('zh_cht', params['cookies']['sB'])
  44. def test_no_url_in_request_year_time_range(self):
  45. dicto = defaultdict(dict)
  46. query = 'test_query'
  47. dicto['time_range'] = 'year'
  48. params = yahoo.request(query, dicto)
  49. self.assertEqual({}, params['url'])
  50. def test_response(self):
  51. self.assertRaises(AttributeError, yahoo.response, None)
  52. self.assertRaises(AttributeError, yahoo.response, [])
  53. self.assertRaises(AttributeError, yahoo.response, '')
  54. self.assertRaises(AttributeError, yahoo.response, '[]')
  55. response = mock.Mock(text='<html></html>')
  56. self.assertEqual(yahoo.response(response), [])
  57. html = """
  58. <ol class="reg mb-15 searchCenterMiddle">
  59. <li class="first">
  60. <div class="dd algo fst Sr">
  61. <div class="compTitle">
  62. <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
  63. _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
  64. /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
  65. target="_blank" data-bid="54e712e13671c">
  66. <b><b>This is the title</b></b></a>
  67. </h3>
  68. </div>
  69. <div class="compText aAbs">
  70. <p class="lh-18"><b><b>This is the </b>content</b>
  71. </p>
  72. </div>
  73. </div>
  74. </li>
  75. <li>
  76. <div class="dd algo lst Sr">
  77. <div class="compTitle">
  78. </div>
  79. <div class="compText aAbs">
  80. <p class="lh-18">This is the second content</p>
  81. </div>
  82. </div>
  83. </li>
  84. </ol>
  85. <div class="dd assist fst lst AlsoTry" data-bid="54e712e138d04">
  86. <div class="compTitle mb-4 h-17">
  87. <h3 class="title">Also Try</h3> </div>
  88. <table class="compTable m-0 ac-1st td-u fz-ms">
  89. <tbody>
  90. <tr>
  91. <td class="w-50p pr-28"><a href="https://search.yahoo.com/"><B>This is the </B>suggestion<B></B></a>
  92. </td>
  93. </tr>
  94. </table>
  95. </div>
  96. """
  97. response = mock.Mock(text=html)
  98. results = yahoo.response(response)
  99. self.assertEqual(type(results), list)
  100. self.assertEqual(len(results), 2)
  101. self.assertEqual(results[0]['title'], 'This is the title')
  102. self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
  103. self.assertEqual(results[0]['content'], 'This is the content')
  104. self.assertEqual(results[1]['suggestion'], 'This is the suggestion')
  105. html = """
  106. <ol class="reg mb-15 searchCenterMiddle">
  107. <li class="first">
  108. <div class="dd algo fst Sr">
  109. <div class="compTitle">
  110. <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
  111. _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
  112. /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
  113. target="_blank" data-bid="54e712e13671c">
  114. <b><b>This is the title</b></b></a>
  115. </h3>
  116. </div>
  117. <div class="compText aAbs">
  118. <p class="lh-18"><b><b>This is the </b>content</b>
  119. </p>
  120. </div>
  121. </div>
  122. </li>
  123. </ol>
  124. """
  125. response = mock.Mock(text=html)
  126. results = yahoo.response(response)
  127. self.assertEqual(type(results), list)
  128. self.assertEqual(len(results), 1)
  129. self.assertEqual(results[0]['title'], 'This is the title')
  130. self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
  131. self.assertEqual(results[0]['content'], 'This is the content')
  132. html = """
  133. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  134. </li>
  135. """
  136. response = mock.Mock(text=html)
  137. results = yahoo.response(response)
  138. self.assertEqual(type(results), list)
  139. self.assertEqual(len(results), 0)
  140. def test_fetch_supported_languages(self):
  141. html = """<html></html>"""
  142. response = mock.Mock(text=html)
  143. results = yahoo._fetch_supported_languages(response)
  144. self.assertEqual(type(results), list)
  145. self.assertEqual(len(results), 0)
  146. html = """
  147. <html>
  148. <div>
  149. <div id="yschlang">
  150. <span>
  151. <label><input value="lang_ar"></input></label>
  152. </span>
  153. <span>
  154. <label><input value="lang_zh_chs"></input></label>
  155. <label><input value="lang_zh_cht"></input></label>
  156. </span>
  157. </div>
  158. </div>
  159. </html>
  160. """
  161. response = mock.Mock(text=html)
  162. languages = yahoo._fetch_supported_languages(response)
  163. self.assertEqual(type(languages), list)
  164. self.assertEqual(len(languages), 3)
  165. self.assertIn('ar', languages)
  166. self.assertIn('zh-CHS', languages)
  167. self.assertIn('zh-CHT', languages)