test_urlfield.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from django.core.exceptions import ValidationError
  2. from django.forms import URLField
  3. from django.test import SimpleTestCase, ignore_warnings
  4. from django.utils.deprecation import RemovedInDjango60Warning
  5. from . import FormFieldAssertionsMixin
  6. @ignore_warnings(category=RemovedInDjango60Warning)
  7. class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
  8. def test_urlfield_widget(self):
  9. f = URLField()
  10. self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" required>')
  11. def test_urlfield_widget_max_min_length(self):
  12. f = URLField(min_length=15, max_length=20)
  13. self.assertEqual("http://example.com", f.clean("http://example.com"))
  14. self.assertWidgetRendersTo(
  15. f,
  16. '<input id="id_f" type="url" name="f" maxlength="20" '
  17. 'minlength="15" required>',
  18. )
  19. msg = "'Ensure this value has at least 15 characters (it has 12).'"
  20. with self.assertRaisesMessage(ValidationError, msg):
  21. f.clean("http://f.com")
  22. msg = "'Ensure this value has at most 20 characters (it has 37).'"
  23. with self.assertRaisesMessage(ValidationError, msg):
  24. f.clean("http://abcdefghijklmnopqrstuvwxyz.com")
  25. def test_urlfield_clean(self):
  26. # RemovedInDjango60Warning: When the deprecation ends, remove the
  27. # assume_scheme argument.
  28. f = URLField(required=False, assume_scheme="https")
  29. tests = [
  30. ("http://localhost", "http://localhost"),
  31. ("http://example.com", "http://example.com"),
  32. ("http://example.com/test", "http://example.com/test"),
  33. ("http://example.com.", "http://example.com."),
  34. ("http://www.example.com", "http://www.example.com"),
  35. ("http://www.example.com:8000/test", "http://www.example.com:8000/test"),
  36. (
  37. "http://example.com?some_param=some_value",
  38. "http://example.com?some_param=some_value",
  39. ),
  40. ("valid-with-hyphens.com", "https://valid-with-hyphens.com"),
  41. ("subdomain.domain.com", "https://subdomain.domain.com"),
  42. ("http://200.8.9.10", "http://200.8.9.10"),
  43. ("http://200.8.9.10:8000/test", "http://200.8.9.10:8000/test"),
  44. ("http://valid-----hyphens.com", "http://valid-----hyphens.com"),
  45. (
  46. "http://some.idn.xyzäöüßabc.domain.com:123/blah",
  47. "http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah",
  48. ),
  49. (
  50. "www.example.com/s/http://code.djangoproject.com/ticket/13804",
  51. "https://www.example.com/s/http://code.djangoproject.com/ticket/13804",
  52. ),
  53. # Normalization.
  54. ("http://example.com/ ", "http://example.com/"),
  55. # Valid IDN.
  56. ("http://עברית.idn.icann.org/", "http://עברית.idn.icann.org/"),
  57. ("http://sãopaulo.com/", "http://sãopaulo.com/"),
  58. ("http://sãopaulo.com.br/", "http://sãopaulo.com.br/"),
  59. ("http://пример.испытание/", "http://пример.испытание/"),
  60. ("http://مثال.إختبار/", "http://مثال.إختبار/"),
  61. ("http://例子.测试/", "http://例子.测试/"),
  62. ("http://例子.測試/", "http://例子.測試/"),
  63. (
  64. "http://उदाहरण.परीक्षा/",
  65. "http://उदाहरण.परीक्षा/",
  66. ),
  67. ("http://例え.テスト/", "http://例え.テスト/"),
  68. ("http://مثال.آزمایشی/", "http://مثال.آزمایشی/"),
  69. ("http://실례.테스트/", "http://실례.테스트/"),
  70. ("http://العربية.idn.icann.org/", "http://العربية.idn.icann.org/"),
  71. # IPv6.
  72. ("http://[12:34::3a53]/", "http://[12:34::3a53]/"),
  73. ("http://[a34:9238::]:8080/", "http://[a34:9238::]:8080/"),
  74. ]
  75. for url, expected in tests:
  76. with self.subTest(url=url):
  77. self.assertEqual(f.clean(url), expected)
  78. def test_urlfield_clean_invalid(self):
  79. f = URLField()
  80. tests = [
  81. "foo",
  82. "com.",
  83. ".",
  84. "http://",
  85. "http://example",
  86. "http://example.",
  87. "http://.com",
  88. "http://invalid-.com",
  89. "http://-invalid.com",
  90. "http://inv-.alid-.com",
  91. "http://inv-.-alid.com",
  92. "[a",
  93. "http://[a",
  94. # Non-string.
  95. 23,
  96. # Hangs "forever" before fixing a catastrophic backtracking,
  97. # see #11198.
  98. "http://%s" % ("X" * 60,),
  99. # A second example, to make sure the problem is really addressed,
  100. # even on domains that don't fail the domain label length check in
  101. # the regex.
  102. "http://%s" % ("X" * 200,),
  103. # urlsplit() raises ValueError.
  104. "////]@N.AN",
  105. # Empty hostname.
  106. "#@A.bO",
  107. ]
  108. msg = "'Enter a valid URL.'"
  109. for value in tests:
  110. with self.subTest(value=value):
  111. with self.assertRaisesMessage(ValidationError, msg):
  112. f.clean(value)
  113. def test_urlfield_clean_required(self):
  114. f = URLField()
  115. msg = "'This field is required.'"
  116. with self.assertRaisesMessage(ValidationError, msg):
  117. f.clean(None)
  118. with self.assertRaisesMessage(ValidationError, msg):
  119. f.clean("")
  120. def test_urlfield_clean_not_required(self):
  121. f = URLField(required=False)
  122. self.assertEqual(f.clean(None), "")
  123. self.assertEqual(f.clean(""), "")
  124. def test_urlfield_strip_on_none_value(self):
  125. f = URLField(required=False, empty_value=None)
  126. self.assertIsNone(f.clean(""))
  127. self.assertIsNone(f.clean(None))
  128. def test_urlfield_unable_to_set_strip_kwarg(self):
  129. msg = "__init__() got multiple values for keyword argument 'strip'"
  130. with self.assertRaisesMessage(TypeError, msg):
  131. URLField(strip=False)
  132. def test_urlfield_assume_scheme(self):
  133. f = URLField()
  134. # RemovedInDjango60Warning: When the deprecation ends, replace with:
  135. # "https://example.com"
  136. self.assertEqual(f.clean("example.com"), "http://example.com")
  137. f = URLField(assume_scheme="http")
  138. self.assertEqual(f.clean("example.com"), "http://example.com")
  139. f = URLField(assume_scheme="https")
  140. self.assertEqual(f.clean("example.com"), "https://example.com")
  141. class URLFieldAssumeSchemeDeprecationTest(FormFieldAssertionsMixin, SimpleTestCase):
  142. def test_urlfield_raises_warning(self):
  143. msg = (
  144. "The default scheme will be changed from 'http' to 'https' in Django 6.0. "
  145. "Pass the forms.URLField.assume_scheme argument to silence this warning."
  146. )
  147. with self.assertWarnsMessage(RemovedInDjango60Warning, msg):
  148. f = URLField()
  149. self.assertEqual(f.clean("example.com"), "http://example.com")