test_startpage.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. dicto['language'] = 'all'
  19. params = startpage.request(query, dicto)
  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="w-gl__result">
  29. <a
  30. class="w-gl__result-title"
  31. href="http://this.should.be.the.link/"
  32. data-onw="1"
  33. rel="noopener noreferrer"
  34. target="_blank">
  35. <h3>This should be the title</h3>
  36. </a>
  37. <div class="w-gl__result-second-line-container">
  38. <div class="w-gl__result-url-container">
  39. <a
  40. class="w-gl__result-url"
  41. href="http://this.should.be.the.link/"
  42. rel="noopener noreferrer"
  43. target="_blank">https://www.cnbc.com/2019/10/12/dj-zedd-banned-in-china-for-liking-a-south-park-tweet.html</a>
  44. </div>
  45. <a
  46. class="w-gl__anonymous-view-url"
  47. href="https://eu-browse.startpage.com/do/proxy?ep=556b554d576b6f5054554546423167764b5445616455554d5342675441774659495246304848774f5267385453304941486b5949546c63704e33774f526b705544565647516d4a61554246304847674f4a556f6957415a4f436b455042426b6b4f7a64535a52784a56514a4f45307743446c567250445a4f4c52514e5677554e46776b4b545563704c7931554c5167465467644f42464d4f4255426f4d693152624634525741305845526c595746636b626d67494e42705743466c515252634f4267456e597a7346596b7856435134465345634f564249794b5752785643315863546769515773764a5163494c5877505246315865456f5141426b4f41774167596d6c5a4e30395758773442465251495677596c624770665a6b786344466b4151455663425249794d6a78525a55554157516f4342556766526b51314b57514e&amp;ek=4q58686o5047786n6343527259445247576p6o38&amp;ekdata=84abd523dc13cba5c65164d04d7d7263"
  48. target="_blank">Anonymous View</a>
  49. </div>
  50. <p class="w-gl__description">This should be the content.</p>
  51. </div>
  52. """ # noqa
  53. response = mock.Mock(text=html.encode('utf-8'))
  54. results = startpage.response(response)
  55. self.assertEqual(type(results), list)
  56. self.assertEqual(len(results), 1)
  57. self.assertEqual(results[0]['title'], 'This should be the title')
  58. self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/')
  59. self.assertEqual(results[0]['content'], 'This should be the content.')