test_webutils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. import mock
  3. from searx import webutils
  4. from tests import SearxTestCase
  5. class TestWebUtils(SearxTestCase):
  6. def test_prettify_url(self):
  7. data = (
  8. ('https://searx.me/', 'https://searx.me/'),
  9. ('https://searx.me/ű', 'https://searx.me/ű'),
  10. ('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'),
  11. ('https://searx.me/' + (100 * 'ű'), 'https://searx.me/[...]űűűűűűűűűűűűűűűűű'),
  12. )
  13. for test_url, expected in data:
  14. self.assertEqual(webutils.prettify_url(test_url, max_length=32), expected)
  15. def test_highlight_content(self):
  16. self.assertEqual(webutils.highlight_content(0, None), None)
  17. self.assertEqual(webutils.highlight_content(None, None), None)
  18. self.assertEqual(webutils.highlight_content('', None), None)
  19. self.assertEqual(webutils.highlight_content(False, None), None)
  20. contents = ['<html></html>not<']
  21. for content in contents:
  22. self.assertEqual(webutils.highlight_content(content, None), content)
  23. content = 'a'
  24. query = 'test'
  25. self.assertEqual(webutils.highlight_content(content, query), 'a')
  26. query = 'a test'
  27. self.assertEqual(webutils.highlight_content(content, query), '<span class="highlight">a</span>')
  28. data = (
  29. ('" test "', 'a test string', 'a <span class="highlight">test</span> string'),
  30. ('"a"', 'this is a test string', 'this is <span class="highlight">a</span> test string'),
  31. (
  32. 'a test',
  33. 'this is a test string that matches entire query',
  34. 'this is <span class="highlight">a</span> <span class="highlight">test</span> string that matches entire query',
  35. ),
  36. (
  37. 'this a test',
  38. 'this is a string to test.',
  39. (
  40. '<span class="highlight">this</span> is <span class="highlight">a</span> string to <span class="highlight">test</span>.'
  41. ),
  42. ),
  43. (
  44. 'match this "exact phrase"',
  45. 'this string contains the exact phrase we want to match',
  46. ''.join(
  47. [
  48. '<span class="highlight">this</span> string contains the <span class="highlight">exact</span> ',
  49. '<span class="highlight">phrase</span> we want to <span class="highlight">match</span>',
  50. ]
  51. ),
  52. ),
  53. )
  54. for query, content, expected in data:
  55. self.assertEqual(webutils.highlight_content(content, query), expected)
  56. class TestUnicodeWriter(SearxTestCase):
  57. def setUp(self):
  58. self.unicode_writer = webutils.UnicodeWriter(mock.MagicMock())
  59. def test_write_row(self):
  60. row = [1, 2, 3]
  61. self.assertEqual(self.unicode_writer.writerow(row), None)
  62. def test_write_rows(self):
  63. self.unicode_writer.writerow = mock.MagicMock()
  64. rows = [1, 2, 3]
  65. self.unicode_writer.writerows(rows)
  66. self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))
  67. class TestNewHmac(SearxTestCase):
  68. def test_bytes(self):
  69. data = b'http://example.com'
  70. with self.assertRaises(AttributeError):
  71. webutils.new_hmac(b'secret', data)
  72. with self.assertRaises(AttributeError):
  73. webutils.new_hmac(1, data)
  74. res = webutils.new_hmac('secret', data)
  75. self.assertEqual(res, '23e2baa2404012a5cc8e4a18b4aabf0dde4cb9b56f679ddc0fd6d7c24339d819')