diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py
index d61d2574..4c99eac9 100644
--- a/searx/engines/wolframalpha_api.py
+++ b/searx/engines/wolframalpha_api.py
@@ -48,13 +48,16 @@ def response(resp):
if search_results.xpath('/queryresult[attribute::success="false"]'):
return []
- # parse result
- result = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')[0].text
- result = replace_pua_chars(result)
+ # parse answer
+ answer = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')
+ if not answer:
+ return results
+
+ answer = replace_pua_chars(answer[0].text)
# append result
# TODO: shouldn't it bind the source too?
- results.append({'answer': result})
+ results.append({'answer': answer})
# return results
return results
diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py
index 23e912a1..9d3afe65 100644
--- a/searx/engines/wolframalpha_noapi.py
+++ b/searx/engines/wolframalpha_noapi.py
@@ -7,8 +7,8 @@
# @stable no
# @parse answer
-import re
-import json
+from re import search
+from json import loads
from urllib import urlencode
# search-url
@@ -26,6 +26,8 @@ def request(query, params):
# get response from search-request
def response(resp):
results = []
+ webpage = resp.text
+ line = None
# the answer is inside a js function
# answer can be located in different 'pods', although by default it should be in pod_0200
@@ -35,7 +37,7 @@ def response(resp):
# get line that matches the pattern
for pattern in possible_locations:
try:
- line = re.search(pattern, resp.text).group(1)
+ line = search(pattern, webpage).group(1)
break
except AttributeError:
continue
@@ -45,7 +47,7 @@ def response(resp):
# extract answer from json
answer = line[line.find('{'):line.rfind('}')+1]
- answer = json.loads(answer.encode('unicode-escape'))
+ answer = loads(answer.encode('unicode-escape'))
answer = answer['stringified'].decode('unicode-escape')
results.append({'answer': answer})
diff --git a/searx/tests/engines/test_wolframalpha_api.py b/searx/tests/engines/test_wolframalpha_api.py
new file mode 100644
index 00000000..d295cea7
--- /dev/null
+++ b/searx/tests/engines/test_wolframalpha_api.py
@@ -0,0 +1,292 @@
+# -*- coding: utf-8 -*-
+from collections import defaultdict
+import mock
+from searx.engines import wolframalpha_api
+from searx.testing import SearxTestCase
+
+
+class TestWolframAlphaAPIEngine(SearxTestCase):
+
+ def test_request(self):
+ query = 'test_query'
+ api_key = 'XXXXXX-XXXXXXXXXX'
+ dicto = defaultdict(dict)
+ dicto['api_key'] = api_key
+ params = wolframalpha_api.request(query, dicto)
+
+ self.assertIn('url', params)
+ self.assertIn(query, params['url'])
+ self.assertIn('wolframalpha.com', params['url'])
+
+ self.assertIn('api_key', params)
+ self.assertIn(api_key, params['api_key'])
+
+ def test_response(self):
+ self.assertRaises(AttributeError, wolframalpha_api.response, None)
+ self.assertRaises(AttributeError, wolframalpha_api.response, [])
+ self.assertRaises(AttributeError, wolframalpha_api.response, '')
+ self.assertRaises(AttributeError, wolframalpha_api.response, '[]')
+
+ xml = '''
+