test_xpath.py 5.5 KB

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