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