From 2a15944b58089d84a930f36b42c6ef60d4e629b3 Mon Sep 17 00:00:00 2001
From: a01200356 <a01200356@itesm.mx>
Date: Sun, 3 Jan 2016 22:03:33 -0600
Subject: [PATCH] [fix] test in wolframalpha_noapi

---
 searx/engines/wolframalpha_noapi.py           |  9 +++--
 .../tests/engines/test_wolframalpha_noapi.py  | 38 ++++++++++++++++++-
 2 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py
index 71ad3b281..442e894b5 100644
--- a/searx/engines/wolframalpha_noapi.py
+++ b/searx/engines/wolframalpha_noapi.py
@@ -41,8 +41,8 @@ def response(resp):
 
     # the answer is inside a js function
     # answer can be located in different 'pods', although by default it should be in pod_0200
-    possible_locations = ['pod_0200\.push(.*)\n',
-                          'pod_0100\.push(.*)\n']
+    possible_locations = ['pod_0200\.push\((.*)',
+                          'pod_0100\.push\((.*)']
 
     # failed result
     if dom.xpath(failure_xpath):
@@ -62,7 +62,10 @@ def response(resp):
     if line:
         # extract answer from json
         answer = line[line.find('{'):line.rfind('}')+1]
-        answer = loads(answer.encode('unicode-escape'))
+        try:
+            answer = loads(answer)
+        except Exception:
+            answer = loads(answer.encode('unicode-escape'))
         answer = answer['stringified']
 
         # clean plaintext answer
diff --git a/searx/tests/engines/test_wolframalpha_noapi.py b/searx/tests/engines/test_wolframalpha_noapi.py
index 3b6314672..cad9593f2 100644
--- a/searx/tests/engines/test_wolframalpha_noapi.py
+++ b/searx/tests/engines/test_wolframalpha_noapi.py
@@ -149,11 +149,45 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
             </body>
         </html>
         """
-        # test output in htmlentity
+        # test output with htmlentity
         response = mock.Mock(text=html)
         results = wolframalpha_noapi.response(response)
         self.assertEqual(type(results), list)
         self.assertEqual(len(results), 2)
-        self.assertIn("¥".decode('utf-8'), results[0]['answer'])
+        self.assertIn('¥'.decode('utf-8'), results[0]['answer'])
         self.assertIn('1 euro to yen - Wolfram|Alpha', results[1]['title'])
         self.assertEquals('http://www.wolframalpha.com/input/?i=+1+euro+to+yen', results[1]['url'])
+
+        html = """
+        <!DOCTYPE html>
+            <title> distance from nairobi to kyoto in inches - Wolfram|Alpha</title>
+            <meta charset="utf-8" />
+            <body>
+                <script type="text/javascript">
+                  try {
+                    if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
+                      context.jsonArray.popups.pod_0100 = [];
+                    }
+[...].pod_0100.push( {"stringified": "convert distance | from | Nairobi, Kenya\nto | Kyoto, Japan to inches"});
+                  } catch(e) { }
+
+                  try {
+                    if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
+                      context.jsonArray.popups.pod_0200 = [];
+                    }
+pod_0200.push({"stringified": "4.295&times;10^8 inches","mOutput": "Quantity[4.295×10^8,&amp;quot;Inches&amp;quot;]"});
+
+                  } catch(e) { }
+                </script>
+            </body>
+        </html>
+        """
+        # test output with utf-8 character
+        response = mock.Mock(text=html)
+        results = wolframalpha_noapi.response(response)
+        self.assertEqual(type(results), list)
+        self.assertEqual(len(results), 2)
+        self.assertIn('4.295×10^8 inches'.decode('utf-8'), results[0]['answer'])
+        self.assertIn('distance from nairobi to kyoto in inches - Wolfram|Alpha', results[1]['title'])
+        self.assertEquals('http://www.wolframalpha.com/input/?i=+distance+from+nairobi+to+kyoto+in+inches',
+                          results[1]['url'])