test_webutils.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 'a class',
  55. 'a string with class.',
  56. '<span class="highlight">a</span> string with <span class="highlight">class</span>.',
  57. ),
  58. )
  59. for query, content, expected in data:
  60. self.assertEqual(webutils.highlight_content(content, query), expected)
  61. class TestUnicodeWriter(SearxTestCase):
  62. def setUp(self):
  63. self.unicode_writer = webutils.CSVWriter(mock.MagicMock())
  64. def test_write_row(self):
  65. row = [1, 2, 3]
  66. self.assertEqual(self.unicode_writer.writerow(row), None)
  67. def test_write_rows(self):
  68. self.unicode_writer.writerow = mock.MagicMock()
  69. rows = [1, 2, 3]
  70. self.unicode_writer.writerows(rows)
  71. self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))
  72. class TestNewHmac(SearxTestCase):
  73. def test_bytes(self):
  74. data = b'http://example.com'
  75. with self.assertRaises(AttributeError):
  76. webutils.new_hmac(b'secret', data)
  77. with self.assertRaises(AttributeError):
  78. webutils.new_hmac(1, data)
  79. res = webutils.new_hmac('secret', data)
  80. self.assertEqual(res, '23e2baa2404012a5cc8e4a18b4aabf0dde4cb9b56f679ddc0fd6d7c24339d819')