test_xpath.py 5.1 KB

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