123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. import mock
  3. from searx.testing import SearxTestCase
  4. from searx import utils
  5. class TestUtils(SearxTestCase):
  6. def test_gen_useragent(self):
  7. self.assertIsInstance(utils.gen_useragent(), str)
  8. self.assertIsNotNone(utils.gen_useragent())
  9. self.assertTrue(utils.gen_useragent().startswith('Mozilla'))
  10. def test_searx_useragent(self):
  11. self.assertIsInstance(utils.searx_useragent(), str)
  12. self.assertIsNotNone(utils.searx_useragent())
  13. self.assertTrue(utils.searx_useragent().startswith('searx'))
  14. def test_highlight_content(self):
  15. self.assertEqual(utils.highlight_content(0, None), None)
  16. self.assertEqual(utils.highlight_content(None, None), None)
  17. self.assertEqual(utils.highlight_content('', None), None)
  18. self.assertEqual(utils.highlight_content(False, None), None)
  19. contents = [
  20. '<html></html>'
  21. 'not<'
  22. ]
  23. for content in contents:
  24. self.assertEqual(utils.highlight_content(content, None), content)
  25. content = 'a'
  26. query = 'test'
  27. self.assertEqual(utils.highlight_content(content, query), content)
  28. query = 'a test'
  29. self.assertEqual(utils.highlight_content(content, query), content)
  30. def test_html_to_text(self):
  31. html = """
  32. <a href="/testlink" class="link_access_account">
  33. <span class="toto">
  34. <span>
  35. <img src="test.jpg" />
  36. </span>
  37. </span>
  38. <span class="titi">
  39. Test text
  40. </span>
  41. </a>
  42. """
  43. self.assertIsInstance(utils.html_to_text(html), unicode)
  44. self.assertIsNotNone(utils.html_to_text(html))
  45. self.assertEqual(utils.html_to_text(html), "Test text")
  46. def test_prettify_url(self):
  47. data = (('https://searx.me/', 'https://searx.me/'),
  48. (u'https://searx.me/ű', u'https://searx.me/ű'),
  49. ('https://searx.me/' + (100 * 'a'), 'https://searx.me/[...]aaaaaaaaaaaaaaaaa'),
  50. (u'https://searx.me/' + (100 * u'ű'), u'https://searx.me/[...]űűűűűűűűűűűűűűűűű'))
  51. for test_url, expected in data:
  52. self.assertEqual(utils.prettify_url(test_url, max_length=32), expected)
  53. class TestHTMLTextExtractor(SearxTestCase):
  54. def setUp(self):
  55. self.html_text_extractor = utils.HTMLTextExtractor()
  56. def test__init__(self):
  57. self.assertEqual(self.html_text_extractor.result, [])
  58. def test_handle_charref(self):
  59. self.html_text_extractor.handle_charref('xF')
  60. self.assertIn(u'\x0f', self.html_text_extractor.result)
  61. self.html_text_extractor.handle_charref('XF')
  62. self.assertIn(u'\x0f', self.html_text_extractor.result)
  63. self.html_text_extractor.handle_charref('97')
  64. self.assertIn(u'a', self.html_text_extractor.result)
  65. def test_handle_entityref(self):
  66. entity = 'test'
  67. self.html_text_extractor.handle_entityref(entity)
  68. self.assertIn(entity, self.html_text_extractor.result)
  69. class TestUnicodeWriter(SearxTestCase):
  70. def setUp(self):
  71. self.unicode_writer = utils.UnicodeWriter(mock.MagicMock())
  72. def test_write_row(self):
  73. row = [1, 2, 3]
  74. self.assertEqual(self.unicode_writer.writerow(row), None)
  75. def test_write_rows(self):
  76. self.unicode_writer.writerow = mock.MagicMock()
  77. rows = [1, 2, 3]
  78. self.unicode_writer.writerows(rows)
  79. self.assertEqual(self.unicode_writer.writerow.call_count, len(rows))