From f71384a52b606708b3bea5e9fbe75f1de8108701 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 16 Mar 2011 03:38:34 +0000 Subject: [PATCH] Fixed #15229 -- Improved URLValidator to accept ftp:// links. Thanks, codefisher and crayz_train git-svn-id: http://code.djangoproject.com/svn/django/trunk@15847 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/validators.py | 2 +- tests/modeltests/validation/tests.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django/core/validators.py b/django/core/validators.py index d6c4b28743..a40af0c8dd 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -45,7 +45,7 @@ class HeadRequest(urllib2.Request): class URLValidator(RegexValidator): regex = re.compile( - r'^https?://' # http:// or https:// + r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain... r'localhost|' #localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py index 142688f3d3..cbd7cb8034 100644 --- a/tests/modeltests/validation/tests.py +++ b/tests/modeltests/validation/tests.py @@ -61,6 +61,18 @@ class BaseModelValidationTests(ValidationTestCase): mtv = ModelToValidate(number=10, name='Some Name', url='http://www.djangoproject.com/') self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection + def test_correct_https_url_but_nonexisting(self): + mtv = ModelToValidate(number=10, name='Some Name', url='https://www.djangoproject.com/') + self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', [u'This URL appears to be a broken link.']) + + def test_correct_ftp_url_but_nonexisting(self): + mtv = ModelToValidate(number=10, name='Some Name', url='ftp://ftp.google.com/we-love-microsoft.html') + self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', [u'This URL appears to be a broken link.']) + + def test_correct_ftps_url_but_nonexisting(self): + mtv = ModelToValidate(number=10, name='Some Name', url='ftps://ftp.google.com/we-love-microsoft.html') + self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url', [u'This URL appears to be a broken link.']) + def test_text_greater_that_charfields_max_length_raises_erros(self): mtv = ModelToValidate(number=10, name='Some Name'*100) self.assertFailsValidation(mtv.full_clean, ['name',])