test_startpage.py 5.8KB

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