search: handle bad JSON engine results as empty (#37174)
This commit is contained in:
parent
ce355fa833
commit
94d25e1cd5
|
@ -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 = {}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue