You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

71 line
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. from validate.links import find_links_in_text
  4. from validate.links import get_host_from_link
  5. class TestValidateLinks(unittest.TestCase):
  6. def test_find_link_in_text(self):
  7. text = """
  8. # this is valid
  9. http://example.com?param1=1&param2=2#anchor
  10. https://www.example.com?param1=1&param2=2#anchor
  11. https://www.example.com.br
  12. https://www.example.com.gov.br
  13. [Example](https://www.example.com?param1=1&param2=2#anchor)
  14. lorem ipsum https://www.example.com?param1=1&param2=2#anchor
  15. https://www.example.com?param1=1&param2=2#anchor lorem ipsum
  16. # this not is valid
  17. example.com
  18. https:example.com
  19. https:/example.com
  20. https//example.com
  21. https//.com
  22. """
  23. links = find_links_in_text(text)
  24. self.assertIsInstance(links, list)
  25. self.assertEqual(len(links), 7)
  26. for link in links:
  27. with self.subTest():
  28. self.assertIsInstance(link, str)
  29. def test_find_link_in_text_with_invalid_argument(self):
  30. with self.assertRaises(TypeError):
  31. find_links_in_text()
  32. find_links_in_text(1)
  33. find_links_in_text(True)
  34. def test_get_host_from_link(self):
  35. links = [
  36. 'example.com',
  37. 'https://example.com',
  38. 'https://www.example.com',
  39. 'https://www.example.com.br',
  40. 'https://www.example.com/route',
  41. 'https://www.example.com?p=1&q=2',
  42. 'https://www.example.com#anchor'
  43. ]
  44. for link in links:
  45. host = get_host_from_link(link)
  46. with self.subTest():
  47. self.assertIsInstance(host, str)
  48. self.assertNotIn('://', host)
  49. self.assertNotIn('/', host)
  50. self.assertNotIn('?', host)
  51. self.assertNotIn('#', host)
  52. with self.assertRaises(TypeError):
  53. get_host_from_link()