on master help sign in
Merge pull request #2300 from dalf/fix-webapp-index

[fix] fix of / and /search

commitebed1461bc4b635b6dde5d99e53dada6711a6d7a authorAlexandre Flament <alex@al-f.net> dateSat 14 Nov 2020, 13:23 (6 years ago) parent20c9571b3a3ccf
2 files changed+29−4
searx/webapp.py+5−3 tests/unit/test_webapp.py+24−1
searx/webapp.py+5−3
@@ -547,10 +547,12 @@ def index():
547547
548548 # redirect to search if there's a query in the request
549549 if request.form.get('q'):
550− return redirect(url_for('search'), 308)
550+ query = ('?' + request.query_string.decode()) if request.query_string else ''
551+ return redirect(url_for('search') + query, 308)
551552
552553 return render(
553554 'index.html',
555+ selected_categories=get_selected_categories(request.preferences, request.form),
554556 )
555557
556558
@@ -566,8 +568,8 @@ def search():
566568 if output_format not in ['html', 'csv', 'json', 'rss']:
567569 output_format = 'html'
568570
569− # check if there is query
570− if request.form.get('q') is None:
571+ # check if there is query (not None and not an empty string)
572+ if not request.form.get('q'):
571573 if output_format == 'html':
572574 return render(
573575 'index.html',
tests/unit/test_webapp.py+24−1
@@ -75,9 +75,32 @@ class ViewsTestCase(SearxTestCase):
7575 self.assertEqual(result.status_code, 200)
7676 self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
7777
78− def test_index_html(self):
78+ def test_index_html_post(self):
7979 result = self.app.post('/', data={'q': 'test'})
8080 self.assertEqual(result.status_code, 308)
81+ self.assertEqual(result.location, 'http://localhost/search')
82+
83+ def test_index_html_get(self):
84+ result = self.app.post('/?q=test')
85+ self.assertEqual(result.status_code, 308)
86+ self.assertEqual(result.location, 'http://localhost/search?q=test')
87+
88+ def test_search_empty_html(self):
89+ result = self.app.post('/search', data={'q': ''})
90+ self.assertEqual(result.status_code, 200)
91+ self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
92+
93+ def test_search_empty_json(self):
94+ result = self.app.post('/search', data={'q': '', 'format': 'json'})
95+ self.assertEqual(result.status_code, 400)
96+
97+ def test_search_empty_csv(self):
98+ result = self.app.post('/search', data={'q': '', 'format': 'csv'})
99+ self.assertEqual(result.status_code, 400)
100+
101+ def test_search_empty_rss(self):
102+ result = self.app.post('/search', data={'q': '', 'format': 'rss'})
103+ self.assertEqual(result.status_code, 400)
81104
82105 def test_search_html(self):
83106 result = self.app.post('/search', data={'q': 'test'})