test_startpage.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import startpage
  5. from searx.testing import SearxTestCase
  6. class TestStartpageEngine(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 = startpage.request(query, dicto)
  13. self.assertIn('url', params)
  14. self.assertIn('startpage.com', params['url'])
  15. self.assertIn('data', params)
  16. self.assertIn('query', params['data'])
  17. self.assertIn(query, params['data']['query'])
  18. self.assertIn('with_language', params['data'])
  19. self.assertIn('lang_fr', params['data']['with_language'])
  20. def test_response(self):
  21. self.assertRaises(AttributeError, startpage.response, None)
  22. self.assertRaises(AttributeError, startpage.response, [])
  23. self.assertRaises(AttributeError, startpage.response, '')
  24. self.assertRaises(AttributeError, startpage.response, '[]')
  25. response = mock.Mock(text='<html></html>')
  26. self.assertEqual(startpage.response(response), [])
  27. html = """
  28. <div class='result' style=' *width : auto; *margin-right : 10%;'>
  29. <h3>
  30. <a href='http://this.should.be.the.link/' id='title_2' name='title_2' >
  31. This should be the title
  32. </a>
  33. <span id='title_stars_2' name='title_stars_2'> </span>
  34. </h3>
  35. <p class='desc clk'>
  36. This should be the content.
  37. </p>
  38. <p>
  39. <span class='url'>www.speed<b>test</b>.net/fr/
  40. </span>
  41. -
  42. <A class="proxy" id="proxy_link" HREF="https://ixquick-proxy.com/do/spg/proxy?ep=&edata=&ek=&ekdata="
  43. class='proxy'>
  44. Navigation avec Ixquick Proxy
  45. </A>
  46. -
  47. <A HREF="https://ixquick-proxy.com/do/spg/highlight.pl?l=francais&c=hf&cat=web&q=test&rl=NONE&rid=
  48. &hlq=https://startpage.com/do/search&mtabp=-1&mtcmd=process_search&mtlanguage=francais&mtengine0=
  49. &mtcat=web&u=http:%2F%2Fwww.speedtest.net%2Ffr%2F" class='proxy'>
  50. Mis en surbrillance
  51. </A>
  52. </p>
  53. </div>
  54. """
  55. response = mock.Mock(text=html.encode('utf-8'))
  56. results = startpage.response(response)
  57. self.assertEqual(type(results), list)
  58. self.assertEqual(len(results), 1)
  59. self.assertEqual(results[0]['title'], 'This should be the title')
  60. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  61. self.assertEqual(results[0]['content'], 'This should be the content.')
  62. html = """
  63. <div class='result' style=' *width : auto; *margin-right : 10%;'>
  64. <h3>
  65. <a href='http://www.google.com/aclk?sa=l&ai=C' id='title_2' name='title_2' >
  66. This should be the title
  67. </a>
  68. <span id='title_stars_2' name='title_stars_2'> </span>
  69. </h3>
  70. <p class='desc clk'>
  71. This should be the content.
  72. </p>
  73. <p>
  74. <span class='url'>www.speed<b>test</b>.net/fr/
  75. </span>
  76. -
  77. <A class="proxy" id="proxy_link" HREF="https://ixquick-proxy.com/do/spg/proxy?ep=&edata=&ek=&ekdata="
  78. class='proxy'>
  79. Navigation avec Ixquick Proxy
  80. </A>
  81. -
  82. <A HREF="https://ixquick-proxy.com/do/spg/highlight.pl?l=francais&c=hf&cat=web&q=test&rl=NONE&rid=
  83. &hlq=https://startpage.com/do/search&mtabp=-1&mtcmd=process_search&mtlanguage=francais&mtengine0=
  84. &mtcat=web&u=http:%2F%2Fwww.speedtest.net%2Ffr%2F" class='proxy'>
  85. Mis en surbrillance
  86. </A>
  87. </p>
  88. </div>
  89. <div class='result' style=' *width : auto; *margin-right : 10%;'>
  90. <h3>
  91. <span id='title_stars_2' name='title_stars_2'> </span>
  92. </h3>
  93. <p class='desc clk'>
  94. This should be the content.
  95. </p>
  96. <p>
  97. <span class='url'>www.speed<b>test</b>.net/fr/
  98. </span>
  99. </p>
  100. </div>
  101. <div class='result' style=' *width : auto; *margin-right : 10%;'>
  102. <h3>
  103. <a href='http://this.should.be.the.link/' id='title_2' name='title_2' >
  104. This should be the title
  105. </a>
  106. <span id='title_stars_2' name='title_stars_2'> </span>
  107. </h3>
  108. <p>
  109. <span class='url'>www.speed<b>test</b>.net/fr/
  110. </span>
  111. -
  112. <A class="proxy" id="proxy_link" HREF="https://ixquick-proxy.com/do/spg/proxy?ep=&edata=&ek=&ekdata="
  113. class='proxy'>
  114. Navigation avec Ixquick Proxy
  115. </A>
  116. -
  117. <A HREF="https://ixquick-proxy.com/do/spg/highlight.pl?l=francais&c=hf&cat=web&q=test&rl=NONE&rid=
  118. &hlq=https://startpage.com/do/search&mtabp=-1&mtcmd=process_search&mtlanguage=francais&mtengine0=
  119. &mtcat=web&u=http:%2F%2Fwww.speedtest.net%2Ffr%2F" class='proxy'>
  120. Mis en surbrillance
  121. </A>
  122. </p>
  123. </div>
  124. """
  125. response = mock.Mock(text=html.encode('utf-8'))
  126. results = startpage.response(response)
  127. self.assertEqual(type(results), list)
  128. self.assertEqual(len(results), 1)
  129. self.assertEqual(results[0]['content'], '')