Browse Source

[enh] test utils.prettify_url

Adam Tauber 9 years ago
parent
commit
7580852bda
2 changed files with 14 additions and 3 deletions
  1. 10
    0
      searx/tests/test_utils.py
  2. 4
    3
      searx/utils.py

+ 10
- 0
searx/tests/test_utils.py View File

1
+# -*- coding: utf-8 -*-
1
 import mock
2
 import mock
2
 from searx.testing import SearxTestCase
3
 from searx.testing import SearxTestCase
3
 from searx import utils
4
 from searx import utils
51
         self.assertIsNotNone(utils.html_to_text(html))
52
         self.assertIsNotNone(utils.html_to_text(html))
52
         self.assertEqual(utils.html_to_text(html), "Test text")
53
         self.assertEqual(utils.html_to_text(html), "Test text")
53
 
54
 
55
+    def test_prettify_url(self):
56
+        data = (('https://searx.me/', 'https://searx.me/'),
57
+                (u'https://searx.me/ű', u'https://searx.me/ű'),
58
+                ('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'),
59
+                (u'https://searx.me/' + (100 * u'ű'), u'https://searx.me/[...]űűűűűűűűűűűűűűűűű'))
60
+
61
+        for test_url, expected in data:
62
+            self.assertEqual(utils.prettify_url(test_url, max_length=32), expected)
63
+
54
 
64
 
55
 class TestHTMLTextExtractor(SearxTestCase):
65
 class TestHTMLTextExtractor(SearxTestCase):
56
 
66
 

+ 4
- 3
searx/utils.py View File

222
     return result
222
     return result
223
 
223
 
224
 
224
 
225
-def prettify_url(url):
226
-    if len(url) > 74:
227
-        return u'{0}[...]{1}'.format(url[:35], url[-35:])
225
+def prettify_url(url, max_length=74):
226
+    if len(url) > max_length:
227
+        chunk_len = max_length / 2 + 1
228
+        return u'{0}[...]{1}'.format(url[:chunk_len], url[-chunk_len:])
228
     else:
229
     else:
229
         return url
230
         return url
230
 
231