Skip to content

Commit

Permalink
Merge pull request #283 from mjtamlyn/ipv6-address-validation
Browse files Browse the repository at this point in the history
Fixed #18779 -- URLValidator can't validate url with ipv6.
  • Loading branch information
andrewgodwin committed Aug 18, 2012
2 parents 16ab519 + bfa9fc6 commit e41c308
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion django/core/validators.py
Expand Up @@ -44,7 +44,8 @@ class URLValidator(RegexValidator):
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
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|' # ...or ipv4
r'\[?[A-F0-9]*:[A-F0-9:]+\]?)' # ...or ipv6
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)

Expand Down
12 changes: 12 additions & 0 deletions tests/regressiontests/forms/tests/fields.py
Expand Up @@ -668,6 +668,18 @@ def test_urlfield_9(self):
# Valid IDN
self.assertEqual(url, f.clean(url))

def test_urlfield_10(self):
"""Test URLField correctly validates IPv6 (#18779)."""
f = URLField()
urls = (
'http://::/',
'http://6:21b4:92/',
'http://[12:34:3a53]/',
'http://[a34:9238::]:8080/',
)
for url in urls:
self.assertEqual(url, f.clean(url))

def test_urlfield_not_string(self):
f = URLField(required=False)
self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'", f.clean, 23)
Expand Down

0 comments on commit e41c308

Please sign in to comment.