Ver código fonte

[enh] test utils.prettify_url

Adam Tauber 9 anos atrás
pai
commit
7580852bda
2 arquivos alterados com 14 adições e 3 exclusões
  1. 10
    0
      searx/tests/test_utils.py
  2. 4
    3
      searx/utils.py

+ 10
- 0
searx/tests/test_utils.py Ver arquivo

@@ -1,3 +1,4 @@
1
+# -*- coding: utf-8 -*-
1 2
 import mock
2 3
 from searx.testing import SearxTestCase
3 4
 from searx import utils
@@ -51,6 +52,15 @@ class TestUtils(SearxTestCase):
51 52
         self.assertIsNotNone(utils.html_to_text(html))
52 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 65
 class TestHTMLTextExtractor(SearxTestCase):
56 66
 

+ 4
- 3
searx/utils.py Ver arquivo

@@ -222,9 +222,10 @@ def dict_subset(d, properties):
222 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 229
     else:
229 230
         return url
230 231