test_webutils.py 4.0 KB

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