test_webutils.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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), content)
  26. query = 'a test'
  27. self.assertEqual(webutils.highlight_content(content, query), content)
  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 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>'
  41. 'string to <span class="highlight">test</span>.'
  42. ),
  43. ),
  44. (
  45. 'match this "exact phrase"',
  46. 'this string contains the exact phrase we want to match',
  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. for query, content, expected in data:
  54. self.assertEqual(webutils.highlight_content(content, query), expected)
  55. class TestUnicodeWriter(SearxTestCase):
  56. def setUp(self):
  57. self.unicode_writer = webutils.UnicodeWriter(mock.MagicMock())
  58. def test_write_row(self):
  59. row = [1, 2, 3]
  60. self.assertEqual(self.unicode_writer.writerow(row), None)
  61. def test_write_rows(self):
  62. self.unicode_writer.writerow = mock.MagicMock()
  63. rows = [1, 2, 3]
  64. self.unicode_writer.writerows(rows)
  65. self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))
  66. class TestNewHmac(SearxTestCase):
  67. def test_bytes(self):
  68. data = b'http://example.com'
  69. with self.assertRaises(AttributeError):
  70. webutils.new_hmac(b'secret', data)
  71. with self.assertRaises(AttributeError):
  72. webutils.new_hmac(1, data)
  73. res = webutils.new_hmac('secret', data)
  74. self.assertEqual(res, '23e2baa2404012a5cc8e4a18b4aabf0dde4cb9b56f679ddc0fd6d7c24339d819')