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

Stops ParseResult.from_string from accepting ports not preceded by a ':' character #103

Merged
merged 4 commits into from
May 21, 2023
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
2 changes: 1 addition & 1 deletion src/rfc3986/misc.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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