Skip to content

Commit

Permalink
URL.build - port can be ""
Browse files Browse the repository at this point in the history
  • Loading branch information
commonism committed Nov 21, 2023
1 parent 8aff6a7 commit 2f95816
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/883.bugfix
@@ -0,0 +1 @@
Fix URL.build with port="" creating invalid URLs
3 changes: 3 additions & 0 deletions tests/test_url_build.py
Expand Up @@ -39,6 +39,9 @@ def test_build_with_port():
u = URL.build(scheme="http", host="127.0.0.1", port=8000)
assert str(u) == "http://127.0.0.1:8000"

u = URL.build(scheme="http", host="127.0.0.1", port="", path="/v1")
assert str(u) == "http://127.0.0.1/v1", str(u)


def test_build_with_user():
u = URL.build(scheme="http", host="127.0.0.1", user="foo")
Expand Down
2 changes: 2 additions & 0 deletions yarl/_url.py
Expand Up @@ -233,6 +233,8 @@ def build(
raise ValueError(
'Can\'t mix "authority" with "user", "password", "host" or "port".'
)
if isinstance(port, str) and port == "":
port = None
if port and not host:
raise ValueError('Can\'t build URL with "port" but without "host".')
if query and query_string:
Expand Down

0 comments on commit 2f95816

Please sign in to comment.