test_xpath.py 5.3 KB

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