on master help sign in
[enh] test utils.prettify_url
commit7580852bda660471f8968b4f14cdf44dad73249f authorAdam Tauber <asciimoo@gmail.com> dateMon 7 Sep 2015, 19:22 (11 years ago) parentda13c3b
2 files changed+14−3
searx/tests/test_utils.py+10 searx/utils.py+4−3
searx/tests/test_utils.py+10
@@ -1,3 +1,4 @@
1+# -*- coding: utf-8 -*-
12import mock
23from searx.testing import SearxTestCase
34from searx import utils
@@ -51,6 +52,15 @@ class TestUtils(SearxTestCase):
5152 self.assertIsNotNone(utils.html_to_text(html))
5253 self.assertEqual(utils.html_to_text(html), "Test text")
5354
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+
5464
5565class TestHTMLTextExtractor(SearxTestCase):
5666
searx/utils.py+4−3
@@ -222,9 +222,10 @@ def dict_subset(d, properties):
222222 return result
223223
224224
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:])
228229 else:
229230 return url
230231