test_uri_validate.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import unittest
  2. from oauthlib.uri_validate import is_absolute_uri
  3. from tests.unittest import TestCase
  4. class UriValidateTest(TestCase):
  5. def test_is_absolute_uri(self):
  6. self.assertIsNotNone(is_absolute_uri('schema://example.com/path'))
  7. self.assertIsNotNone(is_absolute_uri('https://example.com/path'))
  8. self.assertIsNotNone(is_absolute_uri('https://example.com'))
  9. self.assertIsNotNone(is_absolute_uri('https://example.com:443/path'))
  10. self.assertIsNotNone(is_absolute_uri('https://example.com:443/'))
  11. self.assertIsNotNone(is_absolute_uri('https://example.com:443'))
  12. self.assertIsNotNone(is_absolute_uri('http://example.com'))
  13. self.assertIsNotNone(is_absolute_uri('http://example.com/path'))
  14. self.assertIsNotNone(is_absolute_uri('http://example.com:80/path'))
  15. def test_query(self):
  16. self.assertIsNotNone(is_absolute_uri('http://example.com:80/path?foo'))
  17. self.assertIsNotNone(is_absolute_uri('http://example.com:80/path?foo=bar'))
  18. self.assertIsNotNone(is_absolute_uri('http://example.com:80/path?foo=bar&fruit=banana'))
  19. def test_fragment_forbidden(self):
  20. self.assertIsNone(is_absolute_uri('http://example.com:80/path#foo'))
  21. self.assertIsNone(is_absolute_uri('http://example.com:80/path#foo=bar'))
  22. self.assertIsNone(is_absolute_uri('http://example.com:80/path#foo=bar&fruit=banana'))
  23. def test_combined_forbidden(self):
  24. self.assertIsNone(is_absolute_uri('http://example.com:80/path?foo#bar'))
  25. self.assertIsNone(is_absolute_uri('http://example.com:80/path?foo&bar#fruit'))
  26. self.assertIsNone(is_absolute_uri('http://example.com:80/path?foo=1&bar#fruit=banana'))
  27. self.assertIsNone(is_absolute_uri('http://example.com:80/path?foo=1&bar=2#fruit=banana&bar=foo'))
  28. def test_custom_scheme(self):
  29. self.assertIsNotNone(is_absolute_uri('com.example.bundle.id://'))
  30. def test_ipv6_bracket(self):
  31. self.assertIsNotNone(is_absolute_uri('http://[::1]:38432/path'))
  32. self.assertIsNotNone(is_absolute_uri('http://[::1]/path'))
  33. self.assertIsNotNone(is_absolute_uri('http://[fd01:0001::1]/path'))
  34. self.assertIsNotNone(is_absolute_uri('http://[fd01:1::1]/path'))
  35. self.assertIsNotNone(is_absolute_uri('http://[0123:4567:89ab:cdef:0123:4567:89ab:cdef]/path'))
  36. self.assertIsNotNone(is_absolute_uri('http://[0123:4567:89ab:cdef:0123:4567:89ab:cdef]:8080/path'))
  37. @unittest.skip("ipv6 edge-cases not supported")
  38. def test_ipv6_edge_cases(self):
  39. self.assertIsNotNone(is_absolute_uri('http://2001:db8::'))
  40. self.assertIsNotNone(is_absolute_uri('http://::1234:5678'))
  41. self.assertIsNotNone(is_absolute_uri('http://2001:db8::1234:5678'))
  42. self.assertIsNotNone(is_absolute_uri('http://2001:db8:3333:4444:5555:6666:7777:8888'))
  43. self.assertIsNotNone(is_absolute_uri('http://2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF'))
  44. self.assertIsNotNone(is_absolute_uri('http://0123:4567:89ab:cdef:0123:4567:89ab:cdef/path'))
  45. self.assertIsNotNone(is_absolute_uri('http://::'))
  46. self.assertIsNotNone(is_absolute_uri('http://2001:0db8:0001:0000:0000:0ab9:C0A8:0102'))
  47. @unittest.skip("ipv6 dual ipv4 not supported")
  48. def test_ipv6_dual(self):
  49. self.assertIsNotNone(is_absolute_uri('http://2001:db8:3333:4444:5555:6666:1.2.3.4'))
  50. self.assertIsNotNone(is_absolute_uri('http://::11.22.33.44'))
  51. self.assertIsNotNone(is_absolute_uri('http://2001:db8::123.123.123.123'))
  52. self.assertIsNotNone(is_absolute_uri('http://::1234:5678:91.123.4.56'))
  53. self.assertIsNotNone(is_absolute_uri('http://::1234:5678:1.2.3.4'))
  54. self.assertIsNotNone(is_absolute_uri('http://2001:db8::1234:5678:5.6.7.8'))
  55. def test_ipv4(self):
  56. self.assertIsNotNone(is_absolute_uri('http://127.0.0.1:38432/'))
  57. self.assertIsNotNone(is_absolute_uri('http://127.0.0.1:38432/'))
  58. self.assertIsNotNone(is_absolute_uri('http://127.1:38432/'))
  59. def test_failures(self):
  60. self.assertIsNone(is_absolute_uri('http://example.com:notaport/path'))
  61. self.assertIsNone(is_absolute_uri('wrong'))
  62. self.assertIsNone(is_absolute_uri('http://[:1]:38432/path'))
  63. self.assertIsNone(is_absolute_uri('http://[abcd:efgh::1]/'))
  64. def test_recursive_regex(self):
  65. from datetime import datetime
  66. t0 = datetime.now()
  67. is_absolute_uri('http://[::::::::::::::::::::::::::]/path')
  68. t1 = datetime.now()
  69. spent = t1 - t0
  70. self.assertGreater(0.1, spent.total_seconds(), "possible recursive loop detected")