Skip to content

Commit d0e1512

Browse files
authored
Merge pull request #51 from vladimirdotk/master
fix url validator fails for 'localhost'
2 parents 97f5c59 + fbe1815 commit d0e1512

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎tests/test_url.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
u'http://10.1.1.254',
5151
u'http://127.0.0.1:8080',
5252
u'http://127.0.10.150',
53+
u'http://localhost',
54+
u'http://localhost:8000',
5355
])
5456
def test_returns_true_on_valid_url(address):
5557
assert url(address)
@@ -126,6 +128,8 @@ def test_returns_failed_validation_on_invalid_url(address):
126128
(u'http://127.0.0.1', True),
127129
(u'foo://127.0.0.1', True),
128130
(u'http://username:password@127.0.0.1:8080', True),
131+
(u'http://localhost', True),
132+
(u'http://localhost:8000', True),
129133
130134
])
131135
def test_returns_failed_validation_on_invalid_public_url(address, public):

‎validators/url.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
u"(?:(?:169\.254|192\.168)" + ip_middle_octet + ip_last_octet + u")|"
2020
u"(?:172\.(?:1[6-9]|2\d|3[0-1])" + ip_middle_octet + ip_last_octet + u"))"
2121
u"|"
22+
# private & local hosts
23+
u"(?P<private_host>"
24+
u"(?:localhost))"
25+
u"|"
2226
# IP address dotted notation octets
2327
# excludes loopback network 0.0.0.0
2428
# excludes reserved space >= 224.0.0.0
@@ -98,4 +102,6 @@ def url(value, public=False):
98102
if not public:
99103
return result
100104

101-
return result and not result.groupdict()['private_ip']
105+
return result and not any(
106+
(result.groupdict().get(key) for key in ('private_ip', 'private_host'))
107+
)

0 commit comments

Comments
 (0)