on master help sign in
Merge pull request #2300 from dalf/fix-webapp-index
ebed146Alexandre Flament
searx/webapp.py+5−3
@@ -547,10 +547,12 @@ def index():
547
547
548 # redirect to search if there's a query in the request
548 # redirect to search if there's a query in the request
549 if request.form.get('q'):
549 if request.form.get('q'):
550+ query = ('?' + request.query_string.decode()) if request.query_string else ''
550− return redirect(url_for('search'), 308)
551+ return redirect(url_for('search') + query, 308)
551
552
552 return render(
553 return render(
553 'index.html',
554 'index.html',
555+ selected_categories=get_selected_categories(request.preferences, request.form),
554 )
556 )
555
557
556
558
@@ -566,8 +568,8 @@ def search():
566 if output_format not in ['html', 'csv', 'json', 'rss']:
568 if output_format not in ['html', 'csv', 'json', 'rss']:
567 output_format = 'html'
569 output_format = 'html'
568
570
569− # check if there is query
571+ # check if there is query (not None and not an empty string)
570− if request.form.get('q') is None:
572+ if not request.form.get('q'):
571 if output_format == 'html':
573 if output_format == 'html':
572 return render(
574 return render(
573 'index.html',
575 'index.html',
tests/unit/test_webapp.py+24−1
@@ -75,9 +75,32 @@ class ViewsTestCase(SearxTestCase):
75 self.assertEqual(result.status_code, 200)
75 self.assertEqual(result.status_code, 200)
76 self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
76 self.assertIn(b'<div class="title"><h1>searx</h1></div>', result.data)
77
77
78− def test_index_html(self):
78+ def test_index_html_post(self):
79 result = self.app.post('/', data={'q': 'test'})
79 result = self.app.post('/', data={'q': 'test'})
80 self.assertEqual(result.status_code, 308)
80 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)
81
104
82 def test_search_html(self):
105 def test_search_html(self):
83 result = self.app.post('/search', data={'q': 'test'})
106 result = self.app.post('/search', data={'q': 'test'})