4from urllib.parse import ParseResult
6from searx import webapp
7from searx.testing import SearxTestCase
8from searx.search import Search
11class ViewsTestCase(SearxTestCase):
14 webapp.app.config['TESTING'] = True # to get better error messages
15 self.app = webapp.app.test_client()
20 'content': 'first test content',
21 'title': 'First Test',
22 'url': 'http://first.test.xyz',
23 'engines': ['youtube', 'startpage'],
24 'engine': 'startpage',
25 'parsed_url': ParseResult(scheme='http', netloc='first.test.xyz', path='/', params='', query='', fragment=''), # noqa
27 'content': 'second test content',
28 'title': 'Second Test',
29 'url': 'http://second.test.xyz',
30 'engines': ['youtube', 'startpage'],
32 'parsed_url': ParseResult(scheme='http', netloc='second.test.xyz', path='/', params='', query='', fragment=''), # noqa
38 'engine': 'startpage',
49 def search_mock(search_self, *args):
50 search_self.result_container = Mock(get_ordered_results=lambda: test_results,
55 unresponsive_engines=set(),
57 results_number=lambda: 3,
58 results_length=lambda: len(test_results),
59 get_timings=lambda: timings,
63 self.setattr4test(Search, 'search', search_mock)
65 def get_current_theme_name_mock(override=None):
70 self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
72 self.maxDiff = None # to see full diffs
74 def test_index_empty(self):
75 result = self.app.post('/')
76 self.assertEqual(result.status_code, 200)
77 self.assertIn(b'<div class="text-hide center-block" id="main-logo">'
78 + b'<img class="center-block img-responsive" src="/static/themes/oscar/img/logo_searx_a.png"'
79 + b' alt="searx logo" />searx</div>', result.data)
81 def test_index_html_post(self):
82 result = self.app.post('/', data={'q': 'test'})
83 self.assertEqual(result.status_code, 308)
84 self.assertEqual(result.location, 'http://localhost/search')
86 def test_index_html_get(self):
87 result = self.app.post('/?q=test')
88 self.assertEqual(result.status_code, 308)
89 self.assertEqual(result.location, 'http://localhost/search?q=test')
91 def test_search_empty_html(self):
92 result = self.app.post('/search', data={'q': ''})
93 self.assertEqual(result.status_code, 200)
94 self.assertIn(b'<span class="instance pull-left"><a href="/">searx</a></span>', result.data)
96 def test_search_empty_json(self):
97 result = self.app.post('/search', data={'q': '', 'format': 'json'})
98 self.assertEqual(result.status_code, 400)
100 def test_search_empty_csv(self):
101 result = self.app.post('/search', data={'q': '', 'format': 'csv'})
102 self.assertEqual(result.status_code, 400)
104 def test_search_empty_rss(self):
105 result = self.app.post('/search', data={'q': '', 'format': 'rss'})
106 self.assertEqual(result.status_code, 400)
108 def test_search_html(self):
109 result = self.app.post('/search', data={'q': 'test'})
112 b'<h4 class="result_header" id="result-2"><img width="32" height="32" class="favicon"'
113 + b' src="/static/themes/oscar/img/icons/youtube.png" alt="youtube" /><a href="http://second.test.xyz"'
114 + b' rel="noreferrer" aria-labelledby="result-2">Second <span class="highlight">Test</span></a></h4>', # noqa
118 b'<p class="result-content">second <span class="highlight">test</span> content</p>', # noqa
122 def test_index_json(self):
123 result = self.app.post('/', data={'q': 'test', 'format': 'json'})
124 self.assertEqual(result.status_code, 308)
126 def test_search_json(self):
127 result = self.app.post('/search', data={'q': 'test', 'format': 'json'})
128 result_dict = json.loads(result.data.decode())
130 self.assertEqual('test', result_dict['query'])
131 self.assertEqual(len(result_dict['results']), 2)
132 self.assertEqual(result_dict['results'][0]['content'], 'first test content')
133 self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
135 def test_index_csv(self):
136 result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
137 self.assertEqual(result.status_code, 308)
139 def test_search_csv(self):
140 result = self.app.post('/search', data={'q': 'test', 'format': 'csv'})
143 b'title,url,content,host,engine,score,type\r\n'
144 b'First Test,http://first.test.xyz,first test content,first.test.xyz,startpage,,result\r\n' # noqa
145 b'Second Test,http://second.test.xyz,second test content,second.test.xyz,youtube,,result\r\n', # noqa
149 def test_index_rss(self):
150 result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
151 self.assertEqual(result.status_code, 308)
153 def test_search_rss(self):
154 result = self.app.post('/search', data={'q': 'test', 'format': 'rss'})
157 b'<description>Search results for "test" - searx</description>',
162 b'<opensearch:totalResults>3</opensearch:totalResults>',
167 b'<title>First Test</title>',
172 b'<link>http://first.test.xyz</link>',
177 b'<description>first test content</description>',
181 def test_about(self):
182 result = self.app.get('/about')
183 self.assertEqual(result.status_code, 200)
184 self.assertIn(b'<h1>About <a href="/">searx</a></h1>', result.data)
186 def test_preferences(self):
187 result = self.app.get('/preferences')
188 self.assertEqual(result.status_code, 200)
190 b'<form method="post" action="/preferences" id="search_form">',
194 b'<label class="col-sm-3 col-md-2" for="categories">Default categories</label>',
198 b'<label class="col-sm-3 col-md-2" for="locale">Interface language</label>',
202 def test_browser_locale(self):
203 result = self.app.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
204 self.assertEqual(result.status_code, 200)
206 b'<option value="zh_TW" selected="selected">',
208 'Interface locale ignored browser preference.'
211 b'<option value="zh-TW" selected="selected">',
213 'Search language ignored browser preference.'
216 def test_stats(self):
217 result = self.app.get('/stats')
218 self.assertEqual(result.status_code, 200)
219 self.assertIn(b'<h1>Engine stats</h1>', result.data)
221 def test_robots_txt(self):
222 result = self.app.get('/robots.txt')
223 self.assertEqual(result.status_code, 200)
224 self.assertIn(b'Allow: /', result.data)
226 def test_opensearch_xml(self):
227 result = self.app.get('/opensearch.xml')
228 self.assertEqual(result.status_code, 200)
229 self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
231 def test_favicon(self):
232 result = self.app.get('/favicon.ico')
233 self.assertEqual(result.status_code, 200)
235 def test_config(self):
236 result = self.app.get('/config')
237 self.assertEqual(result.status_code, 200)
238 json_result = result.get_json()
239 self.assertTrue(json_result)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list