urlmatch: Improve port error output

This commit is contained in:
Florian Bruhin 2018-02-19 21:50:08 +01:00
parent d6ea9b1e47
commit eda15c53ad
2 changed files with 19 additions and 13 deletions

View File

@ -87,10 +87,8 @@ class UrlPattern:
parsed = urllib.parse.urlparse(pattern) parsed = urllib.parse.urlparse(pattern)
except ValueError as e: except ValueError as e:
raise ParseError(str(e)) raise ParseError(str(e))
# "Changed in version 3.6: Out-of-range port numbers now raise
# ValueError, instead of returning None." assert parsed is not None
if parsed is None:
raise ParseError("Failed to parse {}".format(pattern))
self._init_scheme(parsed) self._init_scheme(parsed)
self._init_host(parsed) self._init_host(parsed)
@ -166,12 +164,12 @@ class UrlPattern:
# We can't access parsed.port as it tries to run int() # We can't access parsed.port as it tries to run int()
self._port = None self._port = None
elif parsed.netloc.endswith(':'): elif parsed.netloc.endswith(':'):
raise ParseError("Empty port") raise ParseError("Invalid port: Port is empty")
else: else:
try: try:
self._port = parsed.port self._port = parsed.port
except ValueError: except ValueError as e:
raise ParseError("Invalid port") raise ParseError("Invalid port: {}".format(e))
if (self._scheme not in list(self.DEFAULT_PORTS) + [None] and if (self._scheme not in list(self.DEFAULT_PORTS) + [None] and
self._port is not None): self._port is not None):

View File

@ -29,6 +29,8 @@ Currently not tested:
- Any other features we don't need, such as .GetAsString() or set operations. - Any other features we don't need, such as .GetAsString() or set operations.
""" """
import re
import sys
import string import string
import pytest import pytest
@ -66,18 +68,24 @@ from qutebrowser.utils import urlmatch
("http://foo.*/bar", "TLD wildcards are not implemented yet"), ("http://foo.*/bar", "TLD wildcards are not implemented yet"),
# Chromium: PARSE_ERROR_INVALID_PORT # Chromium: PARSE_ERROR_INVALID_PORT
("http://foo:/", "Empty port"), ("http://foo:/", "Invalid port: Port is empty"),
("http://*.foo:/", "Empty port"), ("http://*.foo:/", "Invalid port: Port is empty"),
("http://foo:com/", "Invalid port"), ("http://foo:com/",
("http://foo:123456/", "Invalid port"), "Invalid port: invalid literal for int() with base 10: 'com'"),
("http://foo:80:80/monkey", "Invalid port"), pytest.param("http://foo:123456/",
"Invalid port: Port out of range 0-65535",
marks=pytest.mark.skipif(
sys.hexversion < 0x03060000,
reason="Doesn't show an error on Python 3.5")),
("http://foo:80:80/monkey",
"Invalid port: invalid literal for int() with base 10: '80:80'"),
("chrome://foo:1234/bar", "Ports are unsupported with chrome scheme"), ("chrome://foo:1234/bar", "Ports are unsupported with chrome scheme"),
# Additional tests # Additional tests
("http://[", "Invalid IPv6 URL"), ("http://[", "Invalid IPv6 URL"),
]) ])
def test_invalid_patterns(pattern, error): def test_invalid_patterns(pattern, error):
with pytest.raises(urlmatch.ParseError, match=error): with pytest.raises(urlmatch.ParseError, match=re.escape(error)):
urlmatch.UrlPattern(pattern) urlmatch.UrlPattern(pattern)