on master help sign in
[fix] fix of / and /search

* URL / : the index page displayed the selected or the default category. * URL / : when the q parameter is set using the URL, the redirect includes the URL query. * URL /search : an empty query doesn't raise an exception.

commitb3a3ccf2dbf93b26c506904f53d03e5f11398aab authorAlexandre Flament <alex@al-f.net> dateFri 6 Nov 2020, 12:11 (6 years ago) parentc03e4c8
2 files changed+29−4
searx/webapp.py+5−3 tests/unit/test_webapp.py+24−1
searx/webapp.py+5−3
@@ -537,10 +537,12 @@ def index():
537537
538538 # redirect to search if there's a query in the request
539539 if request.form.get('q'):
540− return redirect(url_for('search'), 308)
540+ query = ('?' + request.query_string.decode()) if request.query_string else ''
541+ return redirect(url_for('search') + query, 308)
541542
542543 return render(
543544 'index.html',
545+ selected_categories=get_selected_categories(request.preferences, request.form),
544546 )
545547
546548
@@ -556,8 +558,8 @@ def search():
556558 if output_format not in ['html', 'csv', 'json', 'rss']:
557559 output_format = 'html'
558560
559− # check if there is query
560− if request.form.get('q') is None:
561+ # check if there is query (not None and not an empty string)
562+ if not request.form.get('q'):
561563 if output_format == 'html':
562564 return render(
563565 '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'})