Skip to content

Commit

Permalink
Merge pull request #103 from JohnJamesUtley/fix_port_colon
Browse files Browse the repository at this point in the history
Stops ParseResult.from_string from accepting ports not preceded by a ':' character
  • Loading branch information
sigmavirus24 committed May 21, 2023
2 parents 3c78778 + fb15900 commit 45377d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/rfc3986/misc.py
Expand Up @@ -51,7 +51,7 @@
(
"^(?:(?P<userinfo>{})@)?" # userinfo
"(?P<host>{})" # host
":?(?P<port>{})?$" # port
"(?::(?P<port>{}))?$" # port
).format(
abnf_regexp.USERINFO_RE, abnf_regexp.HOST_PATTERN, abnf_regexp.PORT_RE
)
Expand Down
23 changes: 23 additions & 0 deletions tests/base.py
Expand Up @@ -11,6 +11,7 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from rfc3986 import exceptions as exc


class BaseTestParsesURIs:
Expand Down Expand Up @@ -134,6 +135,28 @@ def test_handles_percent_in_fragment(self, uri_fragment_with_percent):
uri = self.test_class.from_string(uri_fragment_with_percent)
assert uri.fragment == "perc%25ent"

def test_handles_colon_but_no_port(self, uri_with_colon_but_no_port):
try:
uri = self.test_class.from_string(uri_with_colon_but_no_port)
except Exception as e:
assert isinstance(e, exc.InvalidAuthority)
else:
if uri.port is not None:
raise AssertionError(
"No error thrown from URI with colon but no port"
)

def test_handles_port_but_no_colon(self, uri_with_port_but_no_colon):
try:
uri = self.test_class.from_string(uri_with_port_but_no_colon)
except Exception as e:
assert isinstance(e, exc.InvalidAuthority)
else:
if uri.port is not None:
raise AssertionError(
"No error thrown from URI with port but no colon"
)

def test_handles_line_terminators_in_fragment(
self, uri_fragment_with_line_terminators
):
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Expand Up @@ -116,6 +116,16 @@ def uri_without_authority_with_query_only():
return "about:?foo=bar"


@pytest.fixture
def uri_with_colon_but_no_port():
return "scheme://user@[v12.ip]:/path"


@pytest.fixture
def uri_with_port_but_no_colon():
return "scheme://user@[v12.ip]8000/path"


@pytest.fixture(params=valid_hosts)
def relative_uri(request):
return "//%s" % request.param
Expand Down

0 comments on commit 45377d7

Please sign in to comment.