Skip to content

Commit 45377d7

Browse files
authored
Merge pull request #103 from JohnJamesUtley/fix_port_colon
Stops ParseResult.from_string from accepting ports not preceded by a ':' character
2 parents 3c78778 + fb15900 commit 45377d7

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/rfc3986/misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
(
5252
"^(?:(?P<userinfo>{})@)?" # userinfo
5353
"(?P<host>{})" # host
54-
":?(?P<port>{})?$" # port
54+
"(?::(?P<port>{}))?$" # port
5555
).format(
5656
abnf_regexp.USERINFO_RE, abnf_regexp.HOST_PATTERN, abnf_regexp.PORT_RE
5757
)

tests/base.py

+23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from rfc3986 import exceptions as exc
1415

1516

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

138+
def test_handles_colon_but_no_port(self, uri_with_colon_but_no_port):
139+
try:
140+
uri = self.test_class.from_string(uri_with_colon_but_no_port)
141+
except Exception as e:
142+
assert isinstance(e, exc.InvalidAuthority)
143+
else:
144+
if uri.port is not None:
145+
raise AssertionError(
146+
"No error thrown from URI with colon but no port"
147+
)
148+
149+
def test_handles_port_but_no_colon(self, uri_with_port_but_no_colon):
150+
try:
151+
uri = self.test_class.from_string(uri_with_port_but_no_colon)
152+
except Exception as e:
153+
assert isinstance(e, exc.InvalidAuthority)
154+
else:
155+
if uri.port is not None:
156+
raise AssertionError(
157+
"No error thrown from URI with port but no colon"
158+
)
159+
137160
def test_handles_line_terminators_in_fragment(
138161
self, uri_fragment_with_line_terminators
139162
):

tests/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ def uri_without_authority_with_query_only():
116116
return "about:?foo=bar"
117117

118118

119+
@pytest.fixture
120+
def uri_with_colon_but_no_port():
121+
return "scheme://user@[v12.ip]:/path"
122+
123+
124+
@pytest.fixture
125+
def uri_with_port_but_no_colon():
126+
return "scheme://user@[v12.ip]8000/path"
127+
128+
119129
@pytest.fixture(params=valid_hosts)
120130
def relative_uri(request):
121131
return "//%s" % request.param

0 commit comments

Comments
 (0)