mirror of https://github.com/django/django.git
Used subTest() in forms.URLField() tests.
This commit is contained in:
parent
5de12a369a
commit
882647a82c
|
@ -7,150 +7,118 @@ from . import FormFieldAssertionsMixin
|
|||
|
||||
class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
||||
|
||||
def test_urlfield_1(self):
|
||||
def test_urlfield_widget(self):
|
||||
f = URLField()
|
||||
self.assertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" required>')
|
||||
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
||||
f.clean('')
|
||||
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
||||
f.clean(None)
|
||||
self.assertEqual('http://localhost', f.clean('http://localhost'))
|
||||
self.assertEqual('http://example.com', f.clean('http://example.com'))
|
||||
self.assertEqual('http://example.com.', f.clean('http://example.com.'))
|
||||
self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
|
||||
self.assertEqual('http://www.example.com:8000/test', f.clean('http://www.example.com:8000/test'))
|
||||
self.assertEqual('http://valid-with-hyphens.com', f.clean('valid-with-hyphens.com'))
|
||||
self.assertEqual('http://subdomain.domain.com', f.clean('subdomain.domain.com'))
|
||||
self.assertEqual('http://200.8.9.10', f.clean('http://200.8.9.10'))
|
||||
self.assertEqual('http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test'))
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('foo')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://example')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://example.')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('com.')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('.')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://.com')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://invalid-.com')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://-invalid.com')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://inv-.alid-.com')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://inv-.-alid.com')
|
||||
self.assertEqual('http://valid-----hyphens.com', f.clean('http://valid-----hyphens.com'))
|
||||
self.assertEqual(
|
||||
'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah',
|
||||
f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah')
|
||||
)
|
||||
self.assertEqual(
|
||||
'http://www.example.com/s/http://code.djangoproject.com/ticket/13804',
|
||||
f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804')
|
||||
)
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('[a')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://[a')
|
||||
|
||||
def test_url_regex_ticket11198(self):
|
||||
f = URLField()
|
||||
# hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://%s' % ("X" * 200,))
|
||||
|
||||
# a second test, to make sure the problem is really addressed, even on
|
||||
# domains that don't fail the domain label length check in the regex
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://%s' % ("X" * 60,))
|
||||
|
||||
def test_urlfield_2(self):
|
||||
f = URLField(required=False)
|
||||
self.assertEqual('', f.clean(''))
|
||||
self.assertEqual('', f.clean(None))
|
||||
self.assertEqual('http://example.com', f.clean('http://example.com'))
|
||||
self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('foo')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://example')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://example.')
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean('http://.com')
|
||||
|
||||
def test_urlfield_5(self):
|
||||
def test_urlfield_widget_max_min_length(self):
|
||||
f = URLField(min_length=15, max_length=20)
|
||||
self.assertWidgetRendersTo(f, '<input id="id_f" type="url" name="f" maxlength="20" minlength="15" required>')
|
||||
with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 15 characters (it has 12).'"):
|
||||
f.clean('http://f.com')
|
||||
self.assertEqual('http://example.com', f.clean('http://example.com'))
|
||||
with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 20 characters (it has 37).'"):
|
||||
self.assertWidgetRendersTo(
|
||||
f,
|
||||
'<input id="id_f" type="url" name="f" maxlength="20" '
|
||||
'minlength="15" required>',
|
||||
)
|
||||
msg = "'Ensure this value has at least 15 characters (it has 12).'"
|
||||
with self.assertRaisesMessage(ValidationError, msg):
|
||||
f.clean('http://f.com')
|
||||
msg = "'Ensure this value has at most 20 characters (it has 37).'"
|
||||
with self.assertRaisesMessage(ValidationError, msg):
|
||||
f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
|
||||
|
||||
def test_urlfield_6(self):
|
||||
def test_urlfield_clean(self):
|
||||
f = URLField(required=False)
|
||||
self.assertEqual('http://example.com', f.clean('example.com'))
|
||||
self.assertEqual('', f.clean(''))
|
||||
self.assertEqual('https://example.com', f.clean('https://example.com'))
|
||||
|
||||
def test_urlfield_7(self):
|
||||
f = URLField()
|
||||
self.assertEqual('http://example.com', f.clean('http://example.com'))
|
||||
self.assertEqual('http://example.com/test', f.clean('http://example.com/test'))
|
||||
self.assertEqual(
|
||||
'http://example.com?some_param=some_value',
|
||||
f.clean('http://example.com?some_param=some_value')
|
||||
)
|
||||
|
||||
def test_urlfield_9(self):
|
||||
f = URLField()
|
||||
urls = (
|
||||
'http://עברית.idn.icann.org/',
|
||||
'http://sãopaulo.com/',
|
||||
'http://sãopaulo.com.br/',
|
||||
'http://пример.испытание/',
|
||||
'http://مثال.إختبار/',
|
||||
'http://例子.测试/',
|
||||
'http://例子.測試/',
|
||||
'http://उदाहरण.परीक्षा/',
|
||||
'http://例え.テスト/',
|
||||
'http://مثال.آزمایشی/',
|
||||
'http://실례.테스트/',
|
||||
'http://العربية.idn.icann.org/',
|
||||
)
|
||||
for url in urls:
|
||||
tests = [
|
||||
('http://localhost', 'http://localhost'),
|
||||
('http://example.com', 'http://example.com'),
|
||||
('http://example.com/test', 'http://example.com/test'),
|
||||
('http://example.com.', 'http://example.com.'),
|
||||
('http://www.example.com', 'http://www.example.com'),
|
||||
('http://www.example.com:8000/test', 'http://www.example.com:8000/test'),
|
||||
(
|
||||
'http://example.com?some_param=some_value',
|
||||
'http://example.com?some_param=some_value',
|
||||
),
|
||||
('valid-with-hyphens.com', 'http://valid-with-hyphens.com'),
|
||||
('subdomain.domain.com', 'http://subdomain.domain.com'),
|
||||
('http://200.8.9.10', 'http://200.8.9.10'),
|
||||
('http://200.8.9.10:8000/test', 'http://200.8.9.10:8000/test'),
|
||||
('http://valid-----hyphens.com', 'http://valid-----hyphens.com'),
|
||||
(
|
||||
'http://some.idn.xyzäöüßabc.domain.com:123/blah',
|
||||
'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah',
|
||||
),
|
||||
(
|
||||
'www.example.com/s/http://code.djangoproject.com/ticket/13804',
|
||||
'http://www.example.com/s/http://code.djangoproject.com/ticket/13804',
|
||||
),
|
||||
# Normalization.
|
||||
('http://example.com/ ', 'http://example.com/'),
|
||||
# Valid IDN.
|
||||
('http://עברית.idn.icann.org/', 'http://עברית.idn.icann.org/'),
|
||||
('http://sãopaulo.com/', 'http://sãopaulo.com/'),
|
||||
('http://sãopaulo.com.br/', 'http://sãopaulo.com.br/'),
|
||||
('http://пример.испытание/', 'http://пример.испытание/'),
|
||||
('http://مثال.إختبار/', 'http://مثال.إختبار/'),
|
||||
('http://例子.测试/', 'http://例子.测试/'),
|
||||
('http://例子.測試/', 'http://例子.測試/'),
|
||||
('http://उदाहरण.परीक्षा/', 'http://उदाहरण.परीक्षा/',),
|
||||
('http://例え.テスト/', 'http://例え.テスト/'),
|
||||
('http://مثال.آزمایشی/', 'http://مثال.آزمایشی/'),
|
||||
('http://실례.테스트/', 'http://실례.테스트/'),
|
||||
('http://العربية.idn.icann.org/', 'http://العربية.idn.icann.org/'),
|
||||
# IPv6.
|
||||
('http://[12:34::3a53]/', 'http://[12:34::3a53]/'),
|
||||
('http://[a34:9238::]:8080/', 'http://[a34:9238::]:8080/'),
|
||||
]
|
||||
for url, expected in tests:
|
||||
with self.subTest(url=url):
|
||||
# Valid IDN
|
||||
self.assertEqual(url, f.clean(url))
|
||||
self.assertEqual(f.clean(url), expected)
|
||||
|
||||
def test_urlfield_10(self):
|
||||
"""URLField correctly validates IPv6 (#18779)."""
|
||||
def test_urlfield_clean_invalid(self):
|
||||
f = URLField()
|
||||
urls = (
|
||||
'http://[12:34::3a53]/',
|
||||
'http://[a34:9238::]:8080/',
|
||||
)
|
||||
for url in urls:
|
||||
with self.subTest(url=url):
|
||||
self.assertEqual(url, f.clean(url))
|
||||
tests = [
|
||||
'foo',
|
||||
'com.',
|
||||
'.',
|
||||
'http://',
|
||||
'http://example',
|
||||
'http://example.',
|
||||
'http://.com',
|
||||
'http://invalid-.com',
|
||||
'http://-invalid.com',
|
||||
'http://inv-.alid-.com',
|
||||
'http://inv-.-alid.com',
|
||||
'[a',
|
||||
'http://[a',
|
||||
# Non-string.
|
||||
23,
|
||||
# Hangs "forever" before fixing a catastrophic backtracking,
|
||||
# see #11198.
|
||||
'http://%s' % ('X' * 60,),
|
||||
# A second example, to make sure the problem is really addressed,
|
||||
# even on domains that don't fail the domain label length check in
|
||||
# the regex.
|
||||
'http://%s' % ("X" * 200,),
|
||||
]
|
||||
msg = "'Enter a valid URL.'"
|
||||
for value in tests:
|
||||
with self.subTest(value=value):
|
||||
with self.assertRaisesMessage(ValidationError, msg):
|
||||
f.clean(value)
|
||||
|
||||
def test_urlfield_not_string(self):
|
||||
def test_urlfield_clean_required(self):
|
||||
f = URLField()
|
||||
msg = "'This field is required.'"
|
||||
with self.assertRaisesMessage(ValidationError, msg):
|
||||
f.clean(None)
|
||||
with self.assertRaisesMessage(ValidationError, msg):
|
||||
f.clean('')
|
||||
|
||||
def test_urlfield_clean_not_required(self):
|
||||
f = URLField(required=False)
|
||||
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
|
||||
f.clean(23)
|
||||
|
||||
def test_urlfield_normalization(self):
|
||||
f = URLField()
|
||||
self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/')
|
||||
self.assertEqual(f.clean(None), '')
|
||||
self.assertEqual(f.clean(''), '')
|
||||
|
||||
def test_urlfield_strip_on_none_value(self):
|
||||
f = URLField(required=False, empty_value=None)
|
||||
|
|
Loading…
Reference in New Issue