test_urlfield.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.forms import URLField, ValidationError
  4. from django.test import SimpleTestCase
  5. from . import FormFieldAssertionsMixin
  6. class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
  7. def test_urlfield_1(self):
  8. f = URLField()
  9. self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" required />')
  10. with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
  11. f.clean('')
  12. with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
  13. f.clean(None)
  14. self.assertEqual('http://localhost', f.clean('http://localhost'))
  15. self.assertEqual('http://example.com', f.clean('http://example.com'))
  16. self.assertEqual('http://example.com.', f.clean('http://example.com.'))
  17. self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
  18. self.assertEqual('http://www.example.com:8000/test', f.clean('http://www.example.com:8000/test'))
  19. self.assertEqual('http://valid-with-hyphens.com', f.clean('valid-with-hyphens.com'))
  20. self.assertEqual('http://subdomain.domain.com', f.clean('subdomain.domain.com'))
  21. self.assertEqual('http://200.8.9.10', f.clean('http://200.8.9.10'))
  22. self.assertEqual('http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test'))
  23. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  24. f.clean('foo')
  25. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  26. f.clean('http://')
  27. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  28. f.clean('http://example')
  29. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  30. f.clean('http://example.')
  31. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  32. f.clean('com.')
  33. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  34. f.clean('.')
  35. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  36. f.clean('http://.com')
  37. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  38. f.clean('http://invalid-.com')
  39. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  40. f.clean('http://-invalid.com')
  41. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  42. f.clean('http://inv-.alid-.com')
  43. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  44. f.clean('http://inv-.-alid.com')
  45. self.assertEqual('http://valid-----hyphens.com', f.clean('http://valid-----hyphens.com'))
  46. self.assertEqual(
  47. 'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah',
  48. f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah')
  49. )
  50. self.assertEqual(
  51. 'http://www.example.com/s/http://code.djangoproject.com/ticket/13804',
  52. f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804')
  53. )
  54. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  55. f.clean('[a')
  56. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  57. f.clean('http://[a')
  58. def test_url_regex_ticket11198(self):
  59. f = URLField()
  60. # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
  61. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  62. f.clean('http://%s' % ("X" * 200,))
  63. # a second test, to make sure the problem is really addressed, even on
  64. # domains that don't fail the domain label length check in the regex
  65. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  66. f.clean('http://%s' % ("X" * 60,))
  67. def test_urlfield_2(self):
  68. f = URLField(required=False)
  69. self.assertEqual('', f.clean(''))
  70. self.assertEqual('', f.clean(None))
  71. self.assertEqual('http://example.com', f.clean('http://example.com'))
  72. self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
  73. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  74. f.clean('foo')
  75. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  76. f.clean('http://')
  77. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  78. f.clean('http://example')
  79. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  80. f.clean('http://example.')
  81. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  82. f.clean('http://.com')
  83. def test_urlfield_5(self):
  84. f = URLField(min_length=15, max_length=20)
  85. self.assertWidgetRendersTo(f, '<input id="id_f" type="url" name="f" maxlength="20" minlength="15" required />')
  86. with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 15 characters (it has 12).'"):
  87. f.clean('http://f.com')
  88. self.assertEqual('http://example.com', f.clean('http://example.com'))
  89. with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 20 characters (it has 37).'"):
  90. f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
  91. def test_urlfield_6(self):
  92. f = URLField(required=False)
  93. self.assertEqual('http://example.com', f.clean('example.com'))
  94. self.assertEqual('', f.clean(''))
  95. self.assertEqual('https://example.com', f.clean('https://example.com'))
  96. def test_urlfield_7(self):
  97. f = URLField()
  98. self.assertEqual('http://example.com', f.clean('http://example.com'))
  99. self.assertEqual('http://example.com/test', f.clean('http://example.com/test'))
  100. self.assertEqual(
  101. 'http://example.com?some_param=some_value',
  102. f.clean('http://example.com?some_param=some_value')
  103. )
  104. def test_urlfield_9(self):
  105. f = URLField()
  106. urls = (
  107. 'http://עברית.idn.icann.org/',
  108. 'http://sãopaulo.com/',
  109. 'http://sãopaulo.com.br/',
  110. 'http://пример.испытание/',
  111. 'http://مثال.إختبار/',
  112. 'http://例子.测试/',
  113. 'http://例子.測試/',
  114. 'http://उदाहरण.परीक्षा/',
  115. 'http://例え.テスト/',
  116. 'http://مثال.آزمایشی/',
  117. 'http://실례.테스트/',
  118. 'http://العربية.idn.icann.org/',
  119. )
  120. for url in urls:
  121. # Valid IDN
  122. self.assertEqual(url, f.clean(url))
  123. def test_urlfield_10(self):
  124. """URLField correctly validates IPv6 (#18779)."""
  125. f = URLField()
  126. urls = (
  127. 'http://[12:34::3a53]/',
  128. 'http://[a34:9238::]:8080/',
  129. )
  130. for url in urls:
  131. self.assertEqual(url, f.clean(url))
  132. def test_urlfield_not_string(self):
  133. f = URLField(required=False)
  134. with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
  135. f.clean(23)
  136. def test_urlfield_normalization(self):
  137. f = URLField()
  138. self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/')