Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URLs with query params should pass URL validator #524

Merged
merged 2 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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