test_webutils.py 3.4 KB

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