summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas NOEL <tnoel@entrouvert.com>2019-10-24 08:33:30 (GMT)
committerThomas NOEL <tnoel@entrouvert.com>2019-10-24 08:33:30 (GMT)
commit0ac75fcabfe7bddc823f1ccc9eecdaf5a43aaf96 (patch)
tree9b490dd0e5519f94fbd4c1c8dd0d9241a02ac2bf
parent446495b2c2222ab691ea72fbc28982857209fb39 (diff)
downloadcombo-wip/37174-bad-json.zip
combo-wip/37174-bad-json.tar.gz
combo-wip/37174-bad-json.tar.bz2
search: handle bad JSON engine results as empty (#37174)wip/37174-bad-json
-rw-r--r--combo/apps/search/models.py8
-rw-r--r--tests/test_search.py15
2 files changed, 22 insertions, 1 deletions
diff --git a/combo/apps/search/models.py b/combo/apps/search/models.py
index f35e086..4859c4a 100644
--- a/combo/apps/search/models.py
+++ b/combo/apps/search/models.py
@@ -157,7 +157,13 @@ class SearchCell(CellBase):
kwargs['without_user'] = True
# don't send error traces on HTTP errors
kwargs['log_errors'] = 'warn'
- results = requests.get(url, **kwargs).json()
+
+ response = requests.get(url, **kwargs)
+ try:
+ results = response.json()
+ except ValueError:
+ return render_response(service)
+
if service.get('data_key'):
results['data'] = results.get(service['data_key']) or []
hit_templates = {}
diff --git a/tests/test_search.py b/tests/test_search.py
index 2ee67eb..d110596 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -113,6 +113,21 @@ def test_search_cell(app):
assert resp.text.count('<li>') == 1
assert '<li><a href="http://test">barbarbar</a>' in resp.text
+ # search engine does not return valid JSON
+ class FakedResponse(mock.Mock):
+ def json(self):
+ return json.loads(self.content)
+ requests_get.return_value = FakedResponse(content='notjson', status_code=200)
+ resp = app.get('/ajax/search/%s/search_alternate_key/?q=bar' % cell.pk, status=200)
+ assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=bar'
+ assert '<li>' not in resp.text
+ assert 'no result found' in resp.text
+ requests_get.return_value = FakedResponse(content='500withbadjson', status_code=500)
+ resp = app.get('/ajax/search/%s/search_alternate_key/?q=foo' % cell.pk, status=200)
+ assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo'
+ assert '<li>' not in resp.text
+ assert 'no result found' in resp.text
+
with override_settings(TEMPLATE_VARS=TEMPLATE_VARS):
cell._search_services = {'data': ['search_tmpl']}
cell.save()