mirror of
https://github.com/searxng/searxng
synced 2024-01-01 19:24:07 +01:00
Replace Flask by Starlette (2/n)
This commit is contained in:
parent
78561ce7bb
commit
1a3c73cf6f
30 changed files with 994 additions and 2235 deletions
|
|
@ -1,14 +1,28 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
import unittest
|
||||
from urllib.parse import ParseResult
|
||||
from mock import Mock
|
||||
from searx.testing import SearxTestCase
|
||||
# from searx.testing import SearxTestCase
|
||||
from searx.search import Search
|
||||
import searx.search.processors
|
||||
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
class ViewsTestCase(SearxTestCase):
|
||||
|
||||
class ViewsTestCase(unittest.TestCase):
|
||||
|
||||
def setattr4test(self, obj, attr, value):
|
||||
"""
|
||||
setattr(obj, attr, value)
|
||||
but reset to the previous value in the cleanup.
|
||||
"""
|
||||
previous_value = getattr(obj, attr)
|
||||
|
||||
def cleanup_patch():
|
||||
setattr(obj, attr, previous_value)
|
||||
self.addCleanup(cleanup_patch)
|
||||
setattr(obj, attr, value)
|
||||
|
||||
def setUp(self):
|
||||
# skip init function (no external HTTP request)
|
||||
|
|
@ -16,10 +30,8 @@ class ViewsTestCase(SearxTestCase):
|
|||
pass
|
||||
self.setattr4test(searx.search.processors, 'initialize_processor', dummy)
|
||||
|
||||
from searx import webapp # pylint disable=import-outside-toplevel
|
||||
|
||||
webapp.app.config['TESTING'] = True # to get better error messages
|
||||
self.app = webapp.app.test_client()
|
||||
from searx import webapp, templates # pylint disable=import-outside-toplevel
|
||||
self.client = TestClient(webapp.app)
|
||||
|
||||
# set some defaults
|
||||
test_results = [
|
||||
|
|
@ -69,51 +81,51 @@ class ViewsTestCase(SearxTestCase):
|
|||
|
||||
self.setattr4test(Search, 'search', search_mock)
|
||||
|
||||
def get_current_theme_name_mock(override=None):
|
||||
def get_current_theme_name_mock(request, override=None):
|
||||
if override:
|
||||
return override
|
||||
return 'oscar'
|
||||
|
||||
self.setattr4test(webapp, 'get_current_theme_name', get_current_theme_name_mock)
|
||||
self.setattr4test(templates, 'get_current_theme_name', get_current_theme_name_mock)
|
||||
|
||||
self.maxDiff = None # to see full diffs
|
||||
|
||||
def test_index_empty(self):
|
||||
result = self.app.post('/')
|
||||
result = self.client.post('/')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(b'<div class="text-hide center-block" id="main-logo">'
|
||||
+ b'<img class="center-block img-responsive" src="/static/themes/oscar/img/logo_searx_a.png"'
|
||||
+ b' alt="searx logo" />searx</div>', result.data)
|
||||
|
||||
def test_index_html_post(self):
|
||||
result = self.app.post('/', data={'q': 'test'})
|
||||
result = self.client.post('/', data={'q': 'test'})
|
||||
self.assertEqual(result.status_code, 308)
|
||||
self.assertEqual(result.location, 'http://localhost/search')
|
||||
|
||||
def test_index_html_get(self):
|
||||
result = self.app.post('/?q=test')
|
||||
result = self.client.post('/?q=test')
|
||||
self.assertEqual(result.status_code, 308)
|
||||
self.assertEqual(result.location, 'http://localhost/search?q=test')
|
||||
|
||||
def test_search_empty_html(self):
|
||||
result = self.app.post('/search', data={'q': ''})
|
||||
result = self.client.post('/search', data={'q': ''})
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(b'<span class="instance pull-left"><a href="/">searxng</a></span>', result.data)
|
||||
|
||||
def test_search_empty_json(self):
|
||||
result = self.app.post('/search', data={'q': '', 'format': 'json'})
|
||||
result = self.client.post('/search', data={'q': '', 'format': 'json'})
|
||||
self.assertEqual(result.status_code, 400)
|
||||
|
||||
def test_search_empty_csv(self):
|
||||
result = self.app.post('/search', data={'q': '', 'format': 'csv'})
|
||||
result = self.client.post('/search', data={'q': '', 'format': 'csv'})
|
||||
self.assertEqual(result.status_code, 400)
|
||||
|
||||
def test_search_empty_rss(self):
|
||||
result = self.app.post('/search', data={'q': '', 'format': 'rss'})
|
||||
result = self.client.post('/search', data={'q': '', 'format': 'rss'})
|
||||
self.assertEqual(result.status_code, 400)
|
||||
|
||||
def test_search_html(self):
|
||||
result = self.app.post('/search', data={'q': 'test'})
|
||||
result = self.client.post('/search', data={'q': 'test'})
|
||||
|
||||
self.assertIn(
|
||||
b'<h4 class="result_header" id="result-2"><img width="32" height="32" class="favicon"'
|
||||
|
|
@ -127,12 +139,12 @@ class ViewsTestCase(SearxTestCase):
|
|||
)
|
||||
|
||||
def test_index_json(self):
|
||||
result = self.app.post('/', data={'q': 'test', 'format': 'json'})
|
||||
result = self.client.post('/', data={'q': 'test', 'format': 'json'})
|
||||
self.assertEqual(result.status_code, 308)
|
||||
|
||||
def test_search_json(self):
|
||||
result = self.app.post('/search', data={'q': 'test', 'format': 'json'})
|
||||
result_dict = json.loads(result.data.decode())
|
||||
result = self.client.post('/search', data={'q': 'test', 'format': 'json'})
|
||||
result_dict = result.json()
|
||||
|
||||
self.assertEqual('test', result_dict['query'])
|
||||
self.assertEqual(len(result_dict['results']), 2)
|
||||
|
|
@ -140,11 +152,11 @@ class ViewsTestCase(SearxTestCase):
|
|||
self.assertEqual(result_dict['results'][0]['url'], 'http://first.test.xyz')
|
||||
|
||||
def test_index_csv(self):
|
||||
result = self.app.post('/', data={'q': 'test', 'format': 'csv'})
|
||||
result = self.client.post('/', data={'q': 'test', 'format': 'csv'})
|
||||
self.assertEqual(result.status_code, 308)
|
||||
|
||||
def test_search_csv(self):
|
||||
result = self.app.post('/search', data={'q': 'test', 'format': 'csv'})
|
||||
result = self.client.post('/search', data={'q': 'test', 'format': 'csv'})
|
||||
|
||||
self.assertEqual(
|
||||
b'title,url,content,host,engine,score,type\r\n'
|
||||
|
|
@ -154,11 +166,11 @@ class ViewsTestCase(SearxTestCase):
|
|||
)
|
||||
|
||||
def test_index_rss(self):
|
||||
result = self.app.post('/', data={'q': 'test', 'format': 'rss'})
|
||||
result = self.client.post('/', data={'q': 'test', 'format': 'rss'})
|
||||
self.assertEqual(result.status_code, 308)
|
||||
|
||||
def test_search_rss(self):
|
||||
result = self.app.post('/search', data={'q': 'test', 'format': 'rss'})
|
||||
result = self.client.post('/search', data={'q': 'test', 'format': 'rss'})
|
||||
|
||||
self.assertIn(
|
||||
b'<description>Search results for "test" - searx</description>',
|
||||
|
|
@ -186,12 +198,12 @@ class ViewsTestCase(SearxTestCase):
|
|||
)
|
||||
|
||||
def test_about(self):
|
||||
result = self.app.get('/about')
|
||||
result = self.client.get('/about')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(b'<h1>About <a href="/">searxng</a></h1>', result.data)
|
||||
|
||||
def test_preferences(self):
|
||||
result = self.app.get('/preferences')
|
||||
result = self.client.get('/preferences')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(
|
||||
b'<form method="post" action="/preferences" id="search_form">',
|
||||
|
|
@ -207,7 +219,7 @@ class ViewsTestCase(SearxTestCase):
|
|||
)
|
||||
|
||||
def test_browser_locale(self):
|
||||
result = self.app.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
|
||||
result = self.client.get('/preferences', headers={'Accept-Language': 'zh-tw;q=0.8'})
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(
|
||||
b'<option value="zh_TW" selected="selected">',
|
||||
|
|
@ -221,26 +233,26 @@ class ViewsTestCase(SearxTestCase):
|
|||
)
|
||||
|
||||
def test_stats(self):
|
||||
result = self.app.get('/stats')
|
||||
result = self.client.get('/stats')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(b'<h1>Engine stats</h1>', result.data)
|
||||
|
||||
def test_robots_txt(self):
|
||||
result = self.app.get('/robots.txt')
|
||||
result = self.client.get('/robots.txt')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(b'Allow: /', result.data)
|
||||
|
||||
def test_opensearch_xml(self):
|
||||
result = self.app.get('/opensearch.xml')
|
||||
result = self.client.get('/opensearch.xml')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
self.assertIn(b'<Description>a privacy-respecting, hackable metasearch engine</Description>', result.data)
|
||||
|
||||
def test_favicon(self):
|
||||
result = self.app.get('/favicon.ico')
|
||||
result = self.client.get('/favicon.ico')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
|
||||
def test_config(self):
|
||||
result = self.app.get('/config')
|
||||
result = self.client.get('/config')
|
||||
self.assertEqual(result.status_code, 200)
|
||||
json_result = result.get_json()
|
||||
self.assertTrue(json_result)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue