test_xpath.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. from collections import defaultdict
  4. import mock
  5. from searx.engines import xpath
  6. from searx import logger
  7. from tests import SearxTestCase
  8. logger = logger.getChild('engines')
  9. class TestXpathEngine(SearxTestCase): # pylint: disable=missing-class-docstring
  10. html = """
  11. <div>
  12. <div class="search_result">
  13. <a class="result" href="https://result1.com">Result 1</a>
  14. <p class="content">Content 1</p>
  15. <a class="cached" href="https://cachedresult1.com">Cache</a>
  16. </div>
  17. <div class="search_result">
  18. <a class="result" href="https://result2.com">Result 2</a>
  19. <p class="content">Content 2</p>
  20. <a class="cached" href="https://cachedresult2.com">Cache</a>
  21. </div>
  22. </div>
  23. """
  24. def setUp(self):
  25. xpath.logger = logger.getChild('test_xpath')
  26. def test_request(self):
  27. xpath.search_url = 'https://url.com/{query}'
  28. xpath.categories = []
  29. xpath.paging = False
  30. query = 'test_query'
  31. dicto = defaultdict(dict)
  32. dicto['language'] = 'all'
  33. dicto['pageno'] = 1
  34. params = xpath.request(query, dicto)
  35. self.assertIn('url', params)
  36. self.assertEqual('https://url.com/test_query', params['url'])
  37. xpath.search_url = 'https://url.com/q={query}&p={pageno}'
  38. xpath.paging = True
  39. query = 'test_query'
  40. dicto = defaultdict(dict)
  41. dicto['language'] = 'all'
  42. dicto['pageno'] = 1
  43. params = xpath.request(query, dicto)
  44. self.assertIn('url', params)
  45. self.assertEqual('https://url.com/q=test_query&p=1', params['url'])
  46. def test_response(self):
  47. # without results_xpath
  48. xpath.url_xpath = '//div[@class="search_result"]//a[@class="result"]/@href'
  49. xpath.title_xpath = '//div[@class="search_result"]//a[@class="result"]'
  50. xpath.content_xpath = '//div[@class="search_result"]//p[@class="content"]'
  51. self.assertRaises(AttributeError, xpath.response, None)
  52. self.assertRaises(AttributeError, xpath.response, [])
  53. self.assertRaises(AttributeError, xpath.response, '')
  54. self.assertRaises(AttributeError, xpath.response, '[]')
  55. response = mock.Mock(text='<html></html>', status_code=200)
  56. self.assertEqual(xpath.response(response), [])
  57. response = mock.Mock(text=self.html, status_code=200)
  58. results = xpath.response(response)
  59. self.assertEqual(type(results), list)
  60. self.assertEqual(len(results), 2)
  61. self.assertEqual(results[0]['title'], 'Result 1')
  62. self.assertEqual(results[0]['url'], 'https://result1.com/')
  63. self.assertEqual(results[0]['content'], 'Content 1')
  64. self.assertEqual(results[1]['title'], 'Result 2')
  65. self.assertEqual(results[1]['url'], 'https://result2.com/')
  66. self.assertEqual(results[1]['content'], 'Content 2')
  67. # with cached urls, without results_xpath
  68. xpath.cached_xpath = '//div[@class="search_result"]//a[@class="cached"]/@href'
  69. results = xpath.response(response)
  70. self.assertEqual(type(results), list)
  71. self.assertEqual(len(results), 2)
  72. self.assertEqual(results[0]['cached_url'], 'https://cachedresult1.com')
  73. self.assertEqual(results[1]['cached_url'], 'https://cachedresult2.com')
  74. self.assertFalse(results[0].get('is_onion', False))
  75. # results are onion urls (no results_xpath)
  76. xpath.categories = ['onions']
  77. results = xpath.response(response)
  78. self.assertTrue(results[0]['is_onion'])
  79. def test_response_results_xpath(self):
  80. # with results_xpath
  81. xpath.results_xpath = '//div[@class="search_result"]'
  82. xpath.url_xpath = './/a[@class="result"]/@href'
  83. xpath.title_xpath = './/a[@class="result"]'
  84. xpath.content_xpath = './/p[@class="content"]'
  85. xpath.cached_xpath = None
  86. xpath.categories = []
  87. self.assertRaises(AttributeError, xpath.response, None)
  88. self.assertRaises(AttributeError, xpath.response, [])
  89. self.assertRaises(AttributeError, xpath.response, '')
  90. self.assertRaises(AttributeError, xpath.response, '[]')
  91. response = mock.Mock(text='<html></html>', status_code=200)
  92. self.assertEqual(xpath.response(response), [])
  93. response = mock.Mock(text=self.html, status_code=200)
  94. results = xpath.response(response)
  95. self.assertEqual(type(results), list)
  96. self.assertEqual(len(results), 2)
  97. self.assertEqual(results[0]['title'], 'Result 1')
  98. self.assertEqual(results[0]['url'], 'https://result1.com/')
  99. self.assertEqual(results[0]['content'], 'Content 1')
  100. self.assertEqual(results[1]['title'], 'Result 2')
  101. self.assertEqual(results[1]['url'], 'https://result2.com/')
  102. self.assertEqual(results[1]['content'], 'Content 2')
  103. # with cached urls, with results_xpath
  104. xpath.cached_xpath = './/a[@class="cached"]/@href'
  105. results = xpath.response(response)
  106. self.assertEqual(type(results), list)
  107. self.assertEqual(len(results), 2)
  108. self.assertEqual(results[0]['cached_url'], 'https://cachedresult1.com')
  109. self.assertEqual(results[1]['cached_url'], 'https://cachedresult2.com')
  110. self.assertFalse(results[0].get('is_onion', False))
  111. # results are onion urls (with results_xpath)
  112. xpath.categories = ['onions']
  113. results = xpath.response(response)
  114. self.assertTrue(results[0]['is_onion'])