Skip to content

Commit

Permalink
Merge pull request #524 from laurihy/master
Browse files Browse the repository at this point in the history
URLs with query params should pass URL validator
  • Loading branch information
whb07 authored Nov 11, 2019
2 parents 1ef6329 + 7834fac commit 23f730a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,13 @@ class URL(Regexp):
"""

def __init__(self, require_tld=True, message=None):
regex = r"^[a-z]+://(?P<host>[^/:]+)(?P<port>:[0-9]+)?(?P<path>\/.*)?$"
regex = (
r"^[a-z]+://"
r"(?P<host>[^\/\?:]+)"
r"(?P<port>:[0-9]+)?"
r"(?P<path>\/.*?)?"
r"(?P<query>\?.*)?$"
)
super(URL, self).__init__(regex, re.IGNORECASE, message)
self.validate_hostname = HostnameValidation(
require_tld=require_tld, allow_ip=True
Expand Down
17 changes: 15 additions & 2 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ def test_equal_to_raises(
u"http://foobar.dk/",
u"http://foo-bar.dk/",
u"http://foo_bar.dk/",
u"http://foobar.dk?query=param",
u"http://foobar.dk/path?query=param",
u"http://foobar.dk/path?query=param&foo=faa",
u"http://foobar.museum/foobar",
u"http://192.168.0.1/foobar",
u"http://192.168.0.1:9000/fake",
Expand All @@ -400,10 +403,20 @@ def test_valid_url_passes(url_val, dummy_form, dummy_field):
validator(dummy_form, dummy_field)


@pytest.mark.parametrize("url_val", [u"http://localhost/foobar", u"http://foobar"])
@pytest.mark.parametrize(
"url_val",
[
u"http://localhost/foobar",
u"http://foobar",
u"http://foobar?query=param&foo=faa",
u"http://foobar:5000?query=param&foo=faa",
u"http://foobar/path?query=param&foo=faa",
u"http://foobar:1234/path?query=param&foo=faa",
],
)
def test_valid_url_notld_passes(url_val, dummy_form, dummy_field):
"""
Require TLD option se to false, correct URL should pass without raising
Require TLD option set to false, correct URL should pass without raising
"""
validator = url(require_tld=False)
dummy_field.data = url_val
Expand Down

0 comments on commit 23f730a

Please sign in to comment.